ruby-changes:37033
From: hsbt <ko1@a...>
Date: Sat, 3 Jan 2015 11:28:05 +0900 (JST)
Subject: [ruby-changes:37033] hsbt:r49114 (trunk): * array.c: Improve performance of Array#shift. use shared instead of
hsbt 2015-01-03 11:27:50 +0900 (Sat, 03 Jan 2015) New Revision: 49114 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=49114 Log: * 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. Added files: trunk/benchmark/bm_array_shift.rb Modified files: trunk/ChangeLog trunk/array.c trunk/test/ruby/test_array.rb Index: array.c =================================================================== --- array.c (revision 49113) +++ array.c (revision 49114) @@ -1066,14 +1066,21 @@ rb_ary_shift_m(int argc, VALUE *argv, VA https://github.com/ruby/ruby/blob/trunk/array.c#L1066 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); Index: ChangeLog =================================================================== --- ChangeLog (revision 49113) +++ ChangeLog (revision 49114) @@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sat Jan 3 11:27:46 2015 SHIBATA Hiroshi <shibata.hiroshi@g...> + + * 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@g...> * lib/net/http.rb: More descriptive error message when net/http fails Index: benchmark/bm_array_shift.rb =================================================================== --- benchmark/bm_array_shift.rb (revision 0) +++ benchmark/bm_array_shift.rb (revision 49114) @@ -0,0 +1,14 @@ https://github.com/ruby/ruby/blob/trunk/benchmark/bm_array_shift.rb#L1 +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 Index: test/ruby/test_array.rb =================================================================== --- test/ruby/test_array.rb (revision 49113) +++ test/ruby/test_array.rb (revision 49114) @@ -1904,6 +1904,8 @@ class TestArray < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L1904 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_array.rb#L2358 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/