From 574422733dd11b242ba04c02579710e42fa58a77 Mon Sep 17 00:00:00 2001 From: hsbt Date: Sat, 3 Jan 2015 02:27:50 +0000 Subject: [PATCH] * 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49114 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ array.c | 13 ++++++++++--- benchmark/bm_array_shift.rb | 14 ++++++++++++++ test/ruby/test_array.rb | 7 +++++++ 4 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 benchmark/bm_array_shift.rb diff --git a/ChangeLog b/ChangeLog index d6e9d901af..f4fd304ace 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Sat Jan 3 11:27:46 2015 SHIBATA Hiroshi + + * 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 * lib/net/http.rb: More descriptive error message when net/http fails diff --git a/array.c b/array.c index 6babe97ef9..08958aeb65 100644 --- a/array.c +++ b/array.c @@ -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