1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
hsbt 2015-01-03 02:27:50 +00:00
parent a84bfcbf00
commit 574422733d
4 changed files with 38 additions and 3 deletions

View file

@ -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

13
array.c
View file

@ -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);

View file

@ -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

View file

@ -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