From: "gregolsen (Innokenty Mikhailov)" Date: 2012-04-03T17:43:20+09:00 Subject: [ruby-core:44101] [ruby-trunk - Bug #6183] Enumerator::Lazy performance issue Issue #6183 has been updated by gregolsen (Innokenty Mikhailov). File bench.rb added File lazy_enumerator.diff added File code added =begin Finally come up with a concrete idea how to "fix" (based on my first PR https://github.com/ruby/ruby/pull/100). The idea is to keep all blocks (passed with lazy methods like map or select) as (({Proc})) objects inside the enumerator and apply them one by one when value requested ((({to_a})), (({next})), etc) This strategy avoids enumerator chaining on each lazy method call and eliminates fair amount of 'calling the block' with (({rb_block_call})) operations. Here's benchmark results: 2.0.0| ~/projects/ruby(trunk)$ rvm ruby-head 2.0.0| ~/projects/ruby(trunk)$ ruby bench.rb user system total real Lazy enumerator 1.460000 0.000000 1.460000 ( 1.465739) Simple array 0.420000 0.000000 0.420000 ( 0.421446) 0.287671 NaN NaN ( 0.287531) 2.0.0| ~/projects/ruby(trunk)$ rvm system 2.0.0| ~/projects/ruby(trunk)$ ruby bench.rb user system total real Lazy enumerator 0.770000 0.000000 0.770000 ( 0.764750) Simple array 0.370000 0.000000 0.370000 ( 0.382653) 0.480519 NaN NaN ( 0.500364) ruby-head is current trunk compiled, and system ruby - is the same trunk but with my patch applied. Last row in results is ratio between 'Simple array' time and 'Lazy Enumerator' time. So, as you can see, with this patch lazy enumerator becomes almost 2 times faster. It's a 'proof of concept' patch (only map and select added) - let me know if it makes sense. I believe that using this approach and with your help lazy enumerator performance can be improved significantly. I'm attaching the diff along with the main part of the source code just in case it's hard to follow the diff. =end ---------------------------------------- Bug #6183: Enumerator::Lazy performance issue https://bugs.ruby-lang.org/issues/6183#change-25630 Author: gregolsen (Innokenty Mikhailov) Status: Rejected Priority: Normal Assignee: Category: Target version: ruby -v: ruby 2.0.0dev (2012-03-17 trunk 35075) [x86_64-linux] I benchmarked Enumerator::Lazy and that's what I got: user system total real Lazy: 0.690000 0.010000 0.700000 ( 0.733160) Normal: 0.160000 0.010000 0.170000 ( 0.186695) It seems like even with 4 chain links and 3000 elements in initial array, Lazy enumerator is almost 4(!) times slower than the normal case. Instead of performance benefit we've got 4 times performance drawback. See test file attached. -- http://bugs.ruby-lang.org/