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

ruby-changes:29975

From: glass <ko1@a...>
Date: Wed, 17 Jul 2013 22:17:14 +0900 (JST)
Subject: [ruby-changes:29975] glass:r42027 (trunk): * hash.c (rb_hash_replace): performance improvement by using

glass	2013-07-17 22:17:01 +0900 (Wed, 17 Jul 2013)

  New Revision: 42027

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

  Log:
    * hash.c (rb_hash_replace): performance improvement by using
      st_copy().

  Modified files:
    trunk/ChangeLog
    trunk/hash.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 42026)
+++ ChangeLog	(revision 42027)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Jul 17 22:13:26 2013  Masaki Matsushita  <glass.saga@g...>
+
+	* hash.c (rb_hash_replace): performance improvement by using
+	  st_copy().
+
 Wed Jul 17 17:19:54 2013  Koichi Sasada  <ko1@a...>
 
 	* gc.c: rename heap management functions with prefix "heap_".
Index: hash.c
===================================================================
--- hash.c	(revision 42026)
+++ hash.c	(revision 42027)
@@ -1342,21 +1342,35 @@ rb_hash_initialize_copy(VALUE hash, VALU https://github.com/ruby/ruby/blob/trunk/hash.c#L1342
 static VALUE
 rb_hash_replace(VALUE hash, VALUE hash2)
 {
+    st_table *table2;
+
     rb_hash_modify_check(hash);
-    hash2 = to_hash(hash2);
     if (hash == hash2) return hash;
-    rb_hash_clear(hash);
-    if (RHASH(hash2)->ntbl) {
-	hash_tbl(hash);
-	RHASH(hash)->ntbl->type = RHASH(hash2)->ntbl->type;
-    }
-    rb_hash_foreach(hash2, replace_i, hash);
+    hash2 = to_hash(hash2);
+
     RHASH_SET_IFNONE(hash, RHASH_IFNONE(hash2));
-    if (FL_TEST(hash2, HASH_PROC_DEFAULT)) {
+    if (FL_TEST(hash2, HASH_PROC_DEFAULT))
 	FL_SET(hash, HASH_PROC_DEFAULT);
+    else
+	FL_UNSET(hash, HASH_PROC_DEFAULT);
+
+    table2 = RHASH(hash2)->ntbl;
+
+    if (RHASH_EMPTY_P(hash2)) {
+	rb_hash_clear(hash);
+	if (table2) hash_tbl(hash)->type = table2->type;
+	return hash;
+    }
+
+    if (RHASH_ITER_LEV(hash) > 0) {
+	rb_hash_clear(hash);
+	hash_tbl(hash)->type = table2->type;
+	rb_hash_foreach(hash2, replace_i, hash);
     }
     else {
-	FL_UNSET(hash, HASH_PROC_DEFAULT);
+	st_table *old_table = RHASH(hash)->ntbl;
+	if (old_table) st_free_table(old_table);
+	RHASH(hash)->ntbl = st_copy(table2);
     }
 
     return hash;

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

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