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

array.c: linear performance

* array.c (rb_ary_select_bang, ary_reject_bang): linear
  performance.  [ruby-core:67418] [Feature #10714]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49255 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-01-15 01:44:57 +00:00
parent e3c4c7e13b
commit 5ec029d1ea
4 changed files with 84 additions and 30 deletions

View file

@ -1,3 +1,8 @@
Thu Jan 15 10:45:04 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* array.c (rb_ary_select_bang, ary_reject_bang): linear
performance. [ruby-core:67418] [Feature #10714]
Wed Jan 14 18:06:06 2015 Martin Duerst <duerst@it.aoyama.ac.jp>
* lib/uri/mailto.rb: raising URI::InvalidComponentError instead

5
NEWS
View file

@ -17,6 +17,11 @@ with all sufficient information, see the ChangeLog file.
=== Core classes compatibility issues (excluding feature bug fixes)
* Array
* Array#select!, Array#keep_if, Array#reject!, and Array#delete_if
no longer changes the receiver array instantly every time the
block is called.
=== Stdlib updates (outstanding ones only)
=== Stdlib compatibility issues (excluding feature bug fixes)

100
array.c
View file

@ -2822,6 +2822,48 @@ rb_ary_select(VALUE ary)
return result;
}
struct select_bang_arg {
VALUE ary;
long len[2];
};
static VALUE
select_bang_i(VALUE a)
{
volatile struct select_bang_arg *arg = (void *)a;
VALUE ary = arg->ary;
long i1, i2;
for (i1 = i2 = 0; i1 < RARRAY_LEN(ary); arg->len[0] = ++i1) {
VALUE v = RARRAY_AREF(ary, i1);
if (!RTEST(rb_yield(v))) continue;
if (i1 != i2) {
rb_ary_store(ary, i2, v);
}
arg->len[1] = ++i2;
}
return (i1 == i2) ? Qnil : ary;
}
static VALUE
select_bang_ensure(VALUE a)
{
volatile struct select_bang_arg *arg = (void *)a;
VALUE ary = arg->ary;
long len = RARRAY_LEN(ary);
long i1 = arg->len[0], i2 = arg->len[1];
if (i2 < i1) {
if (i1 < len) {
RARRAY_PTR_USE(ary, ptr, {
MEMMOVE(ptr + i2, ptr + i1, VALUE, len - i1);
});
}
ARY_SET_LEN(ary, len - i1 + i2);
}
return ary;
}
/*
* call-seq:
* ary.select! {|item| block } -> ary or nil
@ -2830,6 +2872,8 @@ rb_ary_select(VALUE ary)
* Invokes the given block passing in successive elements from +self+,
* deleting elements for which the block returns a +false+ value.
*
* The array may not be changed instantly every time the block is called.
*
* If changes were made, it will return +self+, otherwise it returns +nil+.
*
* See also Array#keep_if
@ -2841,22 +2885,14 @@ rb_ary_select(VALUE ary)
static VALUE
rb_ary_select_bang(VALUE ary)
{
long i;
VALUE result = Qnil;
struct select_bang_arg args;
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
rb_ary_modify(ary);
for (i = 0; i < RARRAY_LEN(ary); ) {
VALUE v = RARRAY_AREF(ary, i);
if (!RTEST(rb_yield(v))) {
rb_ary_delete_at(ary, i);
result = ary;
}
else {
i++;
}
}
return result;
args.ary = ary;
args.len[0] = args.len[1] = 0;
return rb_ensure(select_bang_i, (VALUE)&args, select_bang_ensure, (VALUE)&args);
}
/*
@ -3098,24 +3134,33 @@ ary_reject(VALUE orig, VALUE result)
return result;
}
static VALUE
reject_bang_i(VALUE a)
{
volatile struct select_bang_arg *arg = (void *)a;
VALUE ary = arg->ary;
long i1, i2;
for (i1 = i2 = 0; i1 < RARRAY_LEN(ary); arg->len[0] = ++i1) {
VALUE v = RARRAY_AREF(ary, i1);
if (RTEST(rb_yield(v))) continue;
if (i1 != i2) {
rb_ary_store(ary, i2, v);
}
arg->len[1] = ++i2;
}
return (i1 == i2) ? Qnil : ary;
}
static VALUE
ary_reject_bang(VALUE ary)
{
long i;
VALUE result = Qnil;
struct select_bang_arg args;
rb_ary_modify_check(ary);
for (i = 0; i < RARRAY_LEN(ary); ) {
VALUE v = RARRAY_AREF(ary, i);
if (RTEST(rb_yield(v))) {
rb_ary_delete_at(ary, i);
result = ary;
}
else {
i++;
}
}
return result;
args.ary = ary;
args.len[0] = args.len[1] = 0;
return rb_ensure(reject_bang_i, (VALUE)&args, select_bang_ensure, (VALUE)&args);
}
/*
@ -3126,8 +3171,7 @@ ary_reject_bang(VALUE ary)
* Equivalent to Array#delete_if, deleting elements from +self+ for which the
* block evaluates to +true+, but returns +nil+ if no changes were made.
*
* The array is changed instantly every time the block is called, not after
* the iteration is over.
* The array may not be changed instantly every time the block is called.
*
* See also Enumerable#reject and Array#delete_if.
*

View file

@ -661,7 +661,7 @@ class TestArray < Test::Unit::TestCase
bug2545 = '[ruby-core:27366]'
a = @cls[ 5, 6, 7, 8, 9, 10 ]
assert_equal(9, a.delete_if {|i| break i if i > 8; assert_equal(a[0], i) || true if i < 7})
assert_equal(9, a.delete_if {|i| break i if i > 8; i < 7})
assert_equal(@cls[7, 8, 9, 10], a, bug2545)
end
@ -1160,7 +1160,7 @@ class TestArray < Test::Unit::TestCase
bug2545 = '[ruby-core:27366]'
a = @cls[ 5, 6, 7, 8, 9, 10 ]
assert_equal(9, a.reject! {|i| break i if i > 8; assert_equal(a[0], i) || true if i < 7})
assert_equal(9, a.reject! {|i| break i if i > 8; i < 7})
assert_equal(@cls[7, 8, 9, 10], a, bug2545)
end