diff options
author | glass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-10-02 05:29:11 +0000 |
---|---|---|
committer | glass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-10-02 05:29:11 +0000 |
commit | 7ae65b24c26bc2dc7a0bf2fe829e3062f58e063a (patch) | |
tree | b765ebc3636c4e8691c60436db1f325a03f63e8f /hash.c | |
parent | 487dfb9d2266e1e21fa35ad6933883a99ec7a35f (diff) |
vm_eval.c: add rb_yield_assoc_or_values()
The new function rb_yield_assoc_or_values() will reduce branching.
* vm_eval.c: add rb_yield_assoc_or_values()
* internal.h: ditto
* hash.c: use rb_yield_assoc_or_values()
* struct.c: ditto
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60095 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'hash.c')
-rw-r--r-- | hash.c | 46 |
1 files changed, 6 insertions, 40 deletions
@@ -1775,17 +1775,7 @@ rb_hash_each_key(VALUE hash) static int each_pair_i(VALUE key, VALUE value) { - rb_yield(rb_assoc_new(key, value)); - return ST_CONTINUE; -} - -static int -each_pair_i_fast(VALUE key, VALUE value) -{ - VALUE argv[2]; - argv[0] = key; - argv[1] = value; - rb_yield_values2(2, argv); + rb_yield_assoc_or_values(key, value); return ST_CONTINUE; } @@ -1815,10 +1805,7 @@ static VALUE rb_hash_each_pair(VALUE hash) { RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); - if (rb_block_arity() > 1) - rb_hash_foreach(hash, each_pair_i_fast, 0); - else - rb_hash_foreach(hash, each_pair_i, 0); + rb_hash_foreach(hash, each_pair_i, 0); return hash; } @@ -2909,18 +2896,7 @@ rb_init_identtable_with_size(st_index_t size) static int any_p_i(VALUE key, VALUE value, VALUE arg) { - VALUE ret = rb_yield(rb_assoc_new(key, value)); - if (RTEST(ret)) { - *(VALUE *)arg = Qtrue; - return ST_STOP; - } - return ST_CONTINUE; -} - -static int -any_p_i_fast(VALUE key, VALUE value, VALUE arg) -{ - VALUE ret = rb_yield_values(2, key, value); + VALUE ret = rb_yield_assoc_or_values(key, value); if (RTEST(ret)) { *(VALUE *)arg = Qtrue; return ST_STOP; @@ -2945,10 +2921,7 @@ rb_hash_any_p(VALUE hash) /* yields pairs, never false */ return Qtrue; } - if (rb_block_arity() > 1) - rb_hash_foreach(hash, any_p_i_fast, (VALUE)&ret); - else - rb_hash_foreach(hash, any_p_i, (VALUE)&ret); + rb_hash_foreach(hash, any_p_i, (VALUE)&ret); return ret; } @@ -3794,15 +3767,8 @@ env_each_pair(VALUE ehash) } FREE_ENVIRON(environ); - if (rb_block_arity() > 1) { - for (i=0; i<RARRAY_LEN(ary); i+=2) { - rb_yield_values(2, RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1)); - } - } - else { - for (i=0; i<RARRAY_LEN(ary); i+=2) { - rb_yield(rb_assoc_new(RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1))); - } + for (i=0; i<RARRAY_LEN(ary); i+=2) { + rb_yield_assoc_or_values(RARRAY_AREF(ary, i), RARRAY_AREF(ary, i+1)); } return ehash; } |