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

ruby-changes:31190

From: nobu <ko1@a...>
Date: Sun, 13 Oct 2013 12:17:02 +0900 (JST)
Subject: [ruby-changes:31190] nobu:r43269 (trunk): compile.c, vm.c: reduce hash merge

nobu	2013-10-13 12:16:54 +0900 (Sun, 13 Oct 2013)

  New Revision: 43269

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=43269

  Log:
    compile.c, vm.c: reduce hash merge
    
    * compile.c (compile_array_): no hash to merge if it is empty.
    * vm.c (m_core_hash_merge_kwd): just check keys if only one argument
      is given, without merging.

  Modified files:
    trunk/ChangeLog
    trunk/compile.c
    trunk/vm.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43268)
+++ ChangeLog	(revision 43269)
@@ -1,3 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+2013-10-13  Nobuyoshi Nakada  <nobu@r...>
+
+	* compile.c (compile_array_): no hash to merge if it is empty.
+
+	* vm.c (m_core_hash_merge_kwd): just check keys if only one argument
+	  is given, without merging.
+
 2013-10-11  Eric Hodel  <drbrain@s...>
 
 	* lib/rake:  Update to rake 10.1.0
Index: compile.c
===================================================================
--- compile.c	(revision 43268)
+++ compile.c	(revision 43269)
@@ -2455,23 +2455,26 @@ compile_array_(rb_iseq_t *iseq, LINK_ANC https://github.com/ruby/ruby/blob/trunk/compile.c#L2455
 			APPEND_LIST(ret, anchor);
 			break;
 		      case COMPILE_ARRAY_TYPE_HASH:
-			if (first) {
-			    first = 0;
-			    ADD_INSN1(anchor, line, newhash, INT2FIX(i));
-			    APPEND_LIST(ret, anchor);
-			}
-			else if (i > 0) {
-			    ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
-			    ADD_INSN(ret, line, swap);
-			    APPEND_LIST(ret, anchor);
-			    ADD_SEND(ret, line, ID2SYM(id_core_hash_merge_ptr), INT2FIX(i + 1));
+			if (i > 0) {
+			    if (first) {
+				ADD_INSN1(anchor, line, newhash, INT2FIX(i));
+				APPEND_LIST(ret, anchor);
+			    }
+			    else {
+				ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
+				ADD_INSN(ret, line, swap);
+				APPEND_LIST(ret, anchor);
+				ADD_SEND(ret, line, ID2SYM(id_core_hash_merge_ptr), INT2FIX(i + 1));
+			    }
 			}
 			if (kw) {
+			    VALUE nhash = (i > 0 || !first) ? INT2FIX(2) : INT2FIX(1);
 			    ADD_INSN1(ret, line, putspecialobject, INT2FIX(VM_SPECIAL_OBJECT_VMCORE));
-			    ADD_INSN(ret, line, swap);
+			    if (i > 0 || !first) ADD_INSN(ret, line, swap);
 			    COMPILE(ret, "keyword splat", kw);
-			    ADD_SEND(ret, line, ID2SYM(id_core_hash_merge_kwd), INT2FIX(2));
+			    ADD_SEND(ret, line, ID2SYM(id_core_hash_merge_kwd), nhash);
 			}
+			first = 0;
 			break;
 		      case COMPILE_ARRAY_TYPE_ARGS:
 			APPEND_LIST(ret, anchor);
Index: vm.c
===================================================================
--- vm.c	(revision 43268)
+++ vm.c	(revision 43269)
@@ -2221,11 +2221,22 @@ kwmerge_i(VALUE key, VALUE value, VALUE https://github.com/ruby/ruby/blob/trunk/vm.c#L2221
     return ST_CONTINUE;
 }
 
+static int
+kwcheck_i(VALUE key, VALUE value, VALUE hash)
+{
+    if (!SYMBOL_P(key)) Check_Type(key, T_SYMBOL);
+    return ST_CONTINUE;
+}
+
 static VALUE
-m_core_hash_merge_kwd(VALUE recv, VALUE hash, VALUE kw)
+m_core_hash_merge_kwd(int argc, VALUE *argv, VALUE recv)
 {
+    VALUE hash, kw;
+    rb_check_arity(argc, 1, 2);
+    hash = argv[0];
+    kw = argv[argc-1];
     kw = rb_convert_type(kw, T_HASH, "Hash", "to_hash");
-    rb_hash_foreach(kw, kwmerge_i, hash);
+    rb_hash_foreach(kw, argc < 2 ? kwcheck_i : kwmerge_i, hash);
     return hash;
 }
 
@@ -2301,7 +2312,7 @@ Init_VM(void) https://github.com/ruby/ruby/blob/trunk/vm.c#L2312
     rb_define_method_id(klass, id_core_hash_from_ary, m_core_hash_from_ary, 1);
     rb_define_method_id(klass, id_core_hash_merge_ary, m_core_hash_merge_ary, 2);
     rb_define_method_id(klass, id_core_hash_merge_ptr, m_core_hash_merge_ptr, -1);
-    rb_define_method_id(klass, id_core_hash_merge_kwd, m_core_hash_merge_kwd, 2);
+    rb_define_method_id(klass, id_core_hash_merge_kwd, m_core_hash_merge_kwd, -1);
     rb_define_method_id(klass, idProc, rb_block_proc, 0);
     rb_define_method_id(klass, idLambda, rb_block_lambda, 0);
     rb_obj_freeze(fcore);

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

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