ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: shevegen@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:90298] [Ruby trunk Feature#15380] faster method lookup for Array#all? #none? #one?
Date: Wed, 05 Dec 2018 06:23:47 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-75405.20181205062346.126f1a857c43bb54@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15380.20181205042657@ruby-lang.org

Issue #15380 has been updated by shevegen (Robert A. Heiler).


While I agree with Koichi in regards to benchmarks and more code 
meaning more maintenance work (naturally), I think it should still
be considered for the trade off being potentially worth it. If there
is time for discussion at the next developer meeting perhaps matz
could be asked.

----------------------------------------
Feature #15380: faster method lookup for Array#all? #none?  #one?
https://bugs.ruby-lang.org/issues/15380#change-75405

* Author: fursich (Onishi Koji)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
## summary

This PR proposes Array-specific implementations for `#all?` , `#none?` and `#one?` to enable faster method lookup.

Before this patch `Array#all?` was not implemented in Array class, so alternatively, `Enumerable#all?` was used each time the method is called.

On the other hand, `#any?` has its own method entry in Array class for faster method calls.

This patch provides above three methods with Array-specific implementations that are equivalent to what `Array#any?` has.


https://github.com/ruby/ruby/pull/2041



## benchmark


~~~ text
**********************************************************************************
benchmarking Array#all?
**********************************************************************************

Calculating -------------------------------------
    Array#all? (new)          421.298 i/s -      1.000k times in 2.373616s (2.37ms/i)
    Array#all? (old)          335.364 i/s -      1.000k times in 2.981838s (2.98ms/i)

Comparison:
    Array#all? (new):       421.3 i/s
    Array#all? (old):       335.4 i/s - 1.26x  slower

<running with --jit>
Calculating -------------------------------------
    Array#all? (new)                244.929 i/s -      1.000k times in 4.082823s (4.08ms/i)
    Array#all? (old)                210.354 i/s -      1.000k times in 4.753895s (4.75ms/i)

Comparison:
    Array#all? (new):       244.9 i/s
    Array#all? (old):       210.4 i/s - 1.16x  slower
~~~

Attached benchmark shows the full benchmark results:

https://gist.github.com/fursich/1d1bad353ddc2f4b510b34e3191fd302


Each method gets approx. 10-20% faster with repeated calls.
It only impacts on method lookup (not execution itself), but at least it should make` Array#all?` work as just efficiently as `Array#any?` does.


## estimate of impact

Just to provide a rough picture on how frequently these methods are used in real world app, here shows a quick-and-dirty investigations I did with [rails](https://github.com/rails/rails) (using its latest master as of Dec 5):

~~~ text
rails (master)$ git grep '\.all?' | wc -l
      80
rails (master)$ git grep '\.one?' | wc -l
      13
rails (master)$ git grep '\.none?' | wc -l
      25
~~~

while
~~~ text
rails (master)$ git grep '\.any?' | wc -l
~~~

(* the result includes non-Array method. the intention here is just to give rough estimate on how frequently these methods are used compared with each other)

It's probably fair to say the use of the three methods (118 lines in total here) are *not* ignorably rare compared to `#all?`


## motivation behind it

In developing Ruby apps we encounter (often non essential) discussion around 'which method call is faster?', 'should we use this method for efficiency?'.

As Ruby lover I really hope to pick methods based on pure readability and Ruby-ness, 

Hopefully it helps Ruby become faster even at slightest level :)




-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2018-12-05  6:23 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15380.20181205042657@ruby-lang.org>
2018-12-05  4:26 ` [ruby-core:90294] [Ruby trunk Feature#15380] faster method lookup for Array#all? #none? #one? fursich0
2018-12-05  4:55 ` [ruby-core:90295] " ko1
2018-12-05  6:23 ` shevegen [this message]
2018-12-05  6:54 ` [ruby-core:90299] " hanmac
2018-12-05 22:11 ` [ruby-core:90330] " hanmac

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-75405.20181205062346.126f1a857c43bb54@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).