mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* enumerator.c: Optimize Lazy#zip when passed only arrays
[Bug #7706] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38925 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
cc6e6de22f
commit
1af390b1ea
3 changed files with 54 additions and 3 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
Thu Jan 24 16:05:08 2013 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
|
||||||
|
|
||||||
|
* enumerator.c: Optimize Lazy#zip when passed only arrays
|
||||||
|
[Bug #7706]
|
||||||
|
|
||||||
Thu Jan 24 15:21:17 2013 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
|
Thu Jan 24 15:21:17 2013 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
|
||||||
|
|
||||||
* enumerator.c: Fix state handling for Lazy#zip,{drop_take}{_while}
|
* enumerator.c: Fix state handling for Lazy#zip,{drop_take}{_while}
|
||||||
|
|
38
enumerator.c
38
enumerator.c
|
@ -1601,6 +1601,26 @@ next_stopped(VALUE obj)
|
||||||
return Qnil;
|
return Qnil;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
lazy_zip_arrays_func(VALUE val, VALUE arrays, int argc, VALUE *argv)
|
||||||
|
{
|
||||||
|
VALUE yielder, ary, memo;
|
||||||
|
long i, count;
|
||||||
|
|
||||||
|
yielder = argv[0];
|
||||||
|
memo = rb_ivar_get(yielder, id_memo);
|
||||||
|
count = NIL_P(memo) ? 0 : NUM2LONG(memo);
|
||||||
|
|
||||||
|
ary = rb_ary_new2(RARRAY_LEN(arrays) + 1);
|
||||||
|
rb_ary_push(ary, argv[1]);
|
||||||
|
for (i = 0; i < RARRAY_LEN(arrays); i++) {
|
||||||
|
rb_ary_push(ary, rb_ary_entry(RARRAY_PTR(arrays)[i], count));
|
||||||
|
}
|
||||||
|
rb_funcall(yielder, id_yield, 1, ary);
|
||||||
|
rb_ivar_set(yielder, id_memo, LONG2NUM(++count));
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
lazy_zip_func(VALUE val, VALUE zip_args, int argc, VALUE *argv)
|
lazy_zip_func(VALUE val, VALUE zip_args, int argc, VALUE *argv)
|
||||||
{
|
{
|
||||||
|
@ -1631,15 +1651,27 @@ lazy_zip_func(VALUE val, VALUE zip_args, int argc, VALUE *argv)
|
||||||
static VALUE
|
static VALUE
|
||||||
lazy_zip(int argc, VALUE *argv, VALUE obj)
|
lazy_zip(int argc, VALUE *argv, VALUE obj)
|
||||||
{
|
{
|
||||||
VALUE ary;
|
VALUE ary, v;
|
||||||
|
long i;
|
||||||
|
rb_block_call_func *func = lazy_zip_arrays_func;
|
||||||
|
|
||||||
if (rb_block_given_p()) {
|
if (rb_block_given_p()) {
|
||||||
return rb_call_super(argc, argv);
|
return rb_call_super(argc, argv);
|
||||||
}
|
}
|
||||||
ary = rb_ary_new4(argc, argv);
|
|
||||||
|
ary = rb_ary_new2(argc);
|
||||||
|
for (i = 0; i < argc; i++) {
|
||||||
|
v = rb_check_array_type(argv[i]);
|
||||||
|
if (NIL_P(v)) {
|
||||||
|
ary = rb_ary_new4(argc, argv);
|
||||||
|
func = lazy_zip_func;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
rb_ary_push(ary, v);
|
||||||
|
}
|
||||||
|
|
||||||
return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
|
return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
|
||||||
lazy_zip_func, ary),
|
func, ary),
|
||||||
ary, lazy_receiver_size);
|
ary, lazy_receiver_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -272,6 +272,20 @@ class TestLazyEnumerator < Test::Unit::TestCase
|
||||||
assert_equal([[1, 'a', 'a'], [2, 'b', 'b'], [3, 'c', 'c']]*3, zip.flat_map{zip}.force, bug7696)
|
assert_equal([[1, 'a', 'a'], [2, 'b', 'b'], [3, 'c', 'c']]*3, zip.flat_map{zip}.force, bug7696)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_zip_lazy_on_args
|
||||||
|
zip = Step.new(1..2).lazy.zip(42..Float::INFINITY)
|
||||||
|
assert_equal [[1, 42], [2, 43]], zip.force
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_zip_efficient_on_array_args
|
||||||
|
ary = [42, :foo]
|
||||||
|
%i[to_enum enum_for lazy each].each do |forbid|
|
||||||
|
ary.define_singleton_method(forbid){ fail "#{forbid} was called"}
|
||||||
|
end
|
||||||
|
zip = Step.new(1..2).lazy.zip(ary)
|
||||||
|
assert_equal [[1, 42], [2, :foo]], zip.force
|
||||||
|
end
|
||||||
|
|
||||||
def test_take_rewound
|
def test_take_rewound
|
||||||
bug7696 = '[ruby-core:51470]'
|
bug7696 = '[ruby-core:51470]'
|
||||||
e=(1..42).lazy.take(2)
|
e=(1..42).lazy.take(2)
|
||||||
|
|
Loading…
Reference in a new issue