Project

General

Profile

Bug #13344

Improve performance of Array#sort with block

Added by watson1978 (Shizuo Fujita) over 3 years ago. Updated over 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-dev:50027]

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

#1

Updated by mrkn (Kenta Murata) over 3 years ago

  • Status changed from Open to Closed

Applied in changeset trunk|r58339.


array.c: improve performance of Array#sort with block

  • array.c (sort_1): improve performance of Array#sort with block

  • benchmark/bm_array_sort_block.rb: added for Array#sort with block

[Bug #13344]
[ruby-dev:50027]
[Fix GH-1544]

Also available in: Atom PDF