[前][次][番号順一覧][スレッド一覧]

ruby-changes:47981

From: glass <ko1@a...>
Date: Mon, 2 Oct 2017 14:29:15 +0900 (JST)
Subject: [ruby-changes:47981] glass:r60095 (trunk): vm_eval.c: add rb_yield_assoc_or_values()

glass	2017-10-02 14:29:11 +0900 (Mon, 02 Oct 2017)

  New Revision: 60095

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60095

  Log:
    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

  Modified files:
    trunk/hash.c
    trunk/internal.h
    trunk/struct.c
    trunk/vm_eval.c
Index: hash.c
===================================================================
--- hash.c	(revision 60094)
+++ hash.c	(revision 60095)
@@ -1775,17 +1775,7 @@ rb_hash_each_key(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L1775
 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 https://github.com/ruby/ruby/blob/trunk/hash.c#L1805
 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 https://github.com/ruby/ruby/blob/trunk/hash.c#L2896
 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) https://github.com/ruby/ruby/blob/trunk/hash.c#L2921
 	/* 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) https://github.com/ruby/ruby/blob/trunk/hash.c#L3767
     }
     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;
 }
Index: struct.c
===================================================================
--- struct.c	(revision 60094)
+++ struct.c	(revision 60095)
@@ -686,19 +686,10 @@ rb_struct_each_pair(VALUE s) https://github.com/ruby/ruby/blob/trunk/struct.c#L686
 
     RETURN_SIZED_ENUMERATOR(s, 0, 0, struct_enum_size);
     members = rb_struct_members(s);
-    if (rb_block_arity() > 1) {
-	for (i=0; i<RSTRUCT_LEN(s); i++) {
-	    VALUE key = rb_ary_entry(members, i);
-	    VALUE value = RSTRUCT_GET(s, i);
-	    rb_yield_values(2, key, value);
-	}
-    }
-    else {
-	for (i=0; i<RSTRUCT_LEN(s); i++) {
-	    VALUE key = rb_ary_entry(members, i);
-	    VALUE value = RSTRUCT_GET(s, i);
-	    rb_yield(rb_assoc_new(key, value));
-	}
+    for (i=0; i<RSTRUCT_LEN(s); i++) {
+	VALUE key = rb_ary_entry(members, i);
+	VALUE value = RSTRUCT_GET(s, i);
+	rb_yield_assoc_or_values(key, value);
     }
     return s;
 }
Index: internal.h
===================================================================
--- internal.h	(revision 60094)
+++ internal.h	(revision 60095)
@@ -1768,6 +1768,7 @@ VALUE rb_check_funcall_with_hook(VALUE r https://github.com/ruby/ruby/blob/trunk/internal.h#L1768
 VALUE rb_check_funcall_default(VALUE, ID, int, const VALUE *, VALUE);
 VALUE rb_yield_1(VALUE val);
 VALUE rb_yield_force_blockarg(VALUE values);
+VALUE rb_yield_assoc_or_values(VALUE key, VALUE value);
 VALUE rb_lambda_call(VALUE obj, ID mid, int argc, const VALUE *argv,
 		     rb_block_call_func_t bl_proc, int min_argc, int max_argc,
 		     VALUE data2);
Index: vm_eval.c
===================================================================
--- vm_eval.c	(revision 60094)
+++ vm_eval.c	(revision 60095)
@@ -1010,6 +1010,18 @@ rb_yield_values2(int argc, const VALUE * https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L1010
 }
 
 VALUE
+rb_yield_assoc_or_values(VALUE key, VALUE value)
+{
+    if (rb_block_arity() > 1) {
+	VALUE argv[2] = { key, value };
+	return rb_yield_0(2, argv);
+    }
+    else {
+	return rb_yield_1(rb_assoc_new(key, value));
+    }
+}
+
+VALUE
 rb_yield_splat(VALUE values)
 {
     VALUE tmp = rb_check_array_type(values);

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]