ruby-changes:30284
From: glass <ko1@a...>
Date: Fri, 2 Aug 2013 23:56:49 +0900 (JST)
Subject: [ruby-changes:30284] glass:r42336 (trunk): * array.c (rb_ary_zip): performance improvement by avoiding
glass 2013-08-02 23:56:39 +0900 (Fri, 02 Aug 2013) New Revision: 42336 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=42336 Log: * array.c (rb_ary_zip): performance improvement by avoiding array creation if rb_block_arity() > 1. Modified files: trunk/ChangeLog trunk/array.c Index: array.c =================================================================== --- array.c (revision 42335) +++ array.c (revision 42336) @@ -3146,7 +3146,7 @@ take_items(VALUE obj, long n) https://github.com/ruby/ruby/blob/trunk/array.c#L3146 static VALUE rb_ary_zip(int argc, VALUE *argv, VALUE ary) { - int i, j; + int i, j, block_given, arity = 0; long len; VALUE result = Qnil; @@ -3154,24 +3154,40 @@ rb_ary_zip(int argc, VALUE *argv, VALUE https://github.com/ruby/ruby/blob/trunk/array.c#L3154 for (i=0; i<argc; i++) { argv[i] = take_items(argv[i], len); } - if (!rb_block_given_p()) { + + block_given = rb_block_given_p(); + if (block_given) + arity = rb_block_arity(); + else result = rb_ary_new2(len); - } - for (i=0; i<RARRAY_LEN(ary); i++) { - VALUE tmp = rb_ary_new2(argc+1); + if (block_given && arity > 1) { + int yield_argc = argc + 1; + VALUE *yield_argv = ALLOC_N(VALUE, yield_argc); - rb_ary_push(tmp, rb_ary_elt(ary, i)); - for (j=0; j<argc; j++) { - rb_ary_push(tmp, rb_ary_elt(argv[j], i)); - } - if (NIL_P(result)) { - rb_yield(tmp); + for (i=0; i<RARRAY_LEN(ary); i++) { + yield_argv[0] = RARRAY_AREF(ary, i); + for (j=0; j<argc; j++) { + yield_argv[j+1] = rb_ary_elt(argv[j], i); + } + rb_yield_values2(yield_argc, yield_argv); } - else { - rb_ary_push(result, tmp); + } + else { + for (i=0; i<RARRAY_LEN(ary); i++) { + VALUE tmp = rb_ary_new2(argc+1); + + rb_ary_push(tmp, RARRAY_AREF(ary, i)); + for (j=0; j<argc; j++) { + rb_ary_push(tmp, rb_ary_elt(argv[j], i)); + } + if (block_given) + rb_yield(tmp); + else + rb_ary_push(result, tmp); } } + return result; } Index: ChangeLog =================================================================== --- ChangeLog (revision 42335) +++ ChangeLog (revision 42336) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Fri Aug 2 23:54:11 2013 Masaki Matsushita <glass.saga@g...> + + * array.c (rb_ary_zip): performance improvement by avoiding + array creation if rb_block_arity() > 1. + Fri Aug 2 23:50:53 2013 Tanaka Akira <akr@f...> * bignum.c (power_cache_get_power): Appry bigtrunc to the result of -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/