Actions
Bug #13344
closedImprove performance of Array#sort with block
    Bug #13344:
    Improve performance of Array#sort with block
  
Description
Array#sort with block will be ~10% faster.
Seems that a performance was reduced at
https://github.com/ruby/ruby/blob/976becf7eb18aa1592c703ac4d86a2cf9dfa701e/vm_eval.c#L1042-L1046
So by this patch, a small argument will be given directly into rb_yield_values2()
Before¶
       user     system      total        real
   0.860000   0.000000   0.860000 (  0.869366)
After¶
       user     system      total        real
   0.790000   0.000000   0.790000 (  0.791408)
Test code¶
require 'benchmark'
Benchmark.bmbm do |x|
  ary = []
  1000.times { |i| ary << Random.rand(1000) }
  x.report do
    1000.times do
      ary.sort{ |a,b| a<=>b}
    end
  end
end
Patch¶
The patch is in https://github.com/ruby/ruby/pull/1544
Actions