diff options
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | array.c | 13 | ||||
-rw-r--r-- | benchmark/bm_array_shift.rb | 14 | ||||
-rw-r--r-- | test/ruby/test_array.rb | 7 |
4 files changed, 38 insertions, 3 deletions
@@ -1,3 +1,10 @@ +Sat Jan 3 11:27:46 2015 SHIBATA Hiroshi <shibata.hiroshi@gmail.com> + + * array.c: Improve performance of Array#shift. use shared instead of + MEMMOVE if with arguments. Patch by @ksss [fix GH-537] + * test/ruby/test_array.rb: ditto. + * benchmark/bm_array_shift.rb: Added benchmark of GH-537 issue. + Sat Jan 3 10:38:52 2015 SHIBATA Hiroshi <shibata.hiroshi@gmail.com> * lib/net/http.rb: More descriptive error message when net/http fails @@ -1066,14 +1066,21 @@ rb_ary_shift_m(int argc, VALUE *argv, VALUE ary) n = RARRAY_LEN(result); if (ARY_SHARED_P(ary)) { if (ARY_SHARED_OCCUPIED(ARY_SHARED(ary))) { + setup_occupied_shared: ary_mem_clear(ary, 0, n); } ARY_INCREASE_PTR(ary, n); } else { - RARRAY_PTR_USE(ary, ptr, { - MEMMOVE(ptr, ptr + n, VALUE, RARRAY_LEN(ary)-n); - }); /* WB: no new reference */ + if (RARRAY_LEN(ary) < ARY_DEFAULT_SIZE) { + RARRAY_PTR_USE(ary, ptr, { + MEMMOVE(ptr, ptr+n, VALUE, RARRAY_LEN(ary)-n); + }); /* WB: no new reference */ + } + else { + ary_make_shared(ary); + goto setup_occupied_shared; + } } ARY_INCREASE_LEN(ary, -n); diff --git a/benchmark/bm_array_shift.rb b/benchmark/bm_array_shift.rb new file mode 100644 index 0000000000..798bb9e3f4 --- /dev/null +++ b/benchmark/bm_array_shift.rb @@ -0,0 +1,14 @@ +require 'benchmark' + +Benchmark.bm do |x| + [10_000,1_000_000,100_000_000].each do |n| + ary = Array.new(n,0) + GC.start + x.report("#{n}:shift"){ ary.shift } + (0..4).each do |i| + ary = Array.new(n,0) + GC.start + x.report("#{n}:shift(#{i})"){ ary.shift(i) } + end + end +end diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb index 0a4b1379f1..04e66cce11 100644 --- a/test/ruby/test_array.rb +++ b/test/ruby/test_array.rb @@ -1904,6 +1904,8 @@ class TestArray < Test::Unit::TestCase a[3] = 3 a.shift(2) assert_equal([2, 3], a) + + assert_equal([1,1,1], ([1] * 100).shift(3)) end def test_unshift_error @@ -2356,6 +2358,11 @@ class TestArray < Test::Unit::TestCase b.replace(a) assert_equal((1..10).to_a, a.shift(10)) assert_equal((11..100).to_a, a) + + a = (1..30).to_a + assert_equal((1..3).to_a, a.shift(3)) + # occupied + assert_equal((4..6).to_a, a.shift(3)) end def test_replace_shared_ary |