-
Notifications
You must be signed in to change notification settings - Fork 181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Performance optimizations for my Array#to_proc #375
Comments
Hello,
I would propose as the first step to write a benchmark of a few examples you care about like the ones above using |
Thank you. Here is a benchmark. The results on MRI 2.4:
|
We got interesting results! The first change I made to the benchmark is to allow it to run for longer, by adding x.iterations = 5 to both Benchmark.ips blocks. Let's run each Benchmark.ips group one by one. First benchmark: include?
So that's like your results, there is a significant overhead on MRI.
First benchmark: instead of (#to_proc, block) 7/8.5 iterations/s, TruffleRuby runs 455/273 iterations/s. Actually there is a reason for that! array_of_numbers = 1..1_000_000
range = 20..40_000
Benchmark.ips do |x|
x.iterations = 5
x.report '#to_proc include?' do
array_of_numbers.map &[range, :include?]
end
x.report 'block include?' do
array_of_numbers.map { |number| range.include? number }
end
end But with Array#to_proc we actually read it only once per iteration,
Now there is no significant difference between the two versions, and we are Second benchmark: appendThis benchmark has the same problem as the one above,
That's a pretty big overhead.
No significant difference. Here again Array#to_proc has no overhead! The benchmark files I used are in this gist. |
Thank you. I really appreciate it. Very informative and educational. |
… to master * commit 'd7a53f2f534774892f4bce2178b0a2a9e7c56053': Remove unused core strings. Simplify rescue spec. Remove unnecessary profile. Use the correct superclass for `ExceptionNodes` primitives.
I wrote the following Array#to_proc. It makes Ruby more concise and pleasant to me.
Unfortunately, it leads to a performance hit on MRI(at the moment). I wonder if it can be optimized on TruffleRuby. Could it possibly become a zero-cost abstraction?
Here is an example:
I am looking for the ways to contribute to TruffleRuby(probably after JVM 9 released). Performance optimizations for my
Array#to_proc
use case is one of the topics that interest me the most, at the moment.I would be grateful if you could point me to the relevant parts of the codebase and relevant materials.
The text was updated successfully, but these errors were encountered: