From 29ca20de2d998d21c0d41224799182020311ea76 Mon Sep 17 00:00:00 2001
From: shyouhei <shyouhei@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
Date: Thu, 27 Apr 2017 04:21:04 +0000
Subject: refactor newhash (revision 58463 another try) [fix GH-1600]

	* st.c (rb_hash_bulk_insert): new API to bulk insert entries
	  into a hash. Given arguments are first inserted into the
	  table at once, then reindexed. This is faster than inserting
	  things using rb_hash_aset() one by one.

	  This arrangement (rb_ prefixed function placed in st.c) is
	  unavoidable because it both touches table internal and write
	  barrier at once.

	* internal.h: delcare the new function.

	* hash.c (rb_hash_s_create): use the new function.

	* vm.c (core_hash_merge): ditto.

	* insns.def (newhash): ditto.

	* test/ruby/test_hash.rb: more coverage on hash creation.

	* test/ruby/test_literal.rb: ditto.

-----------------------------------------------------------
benchmark results:
minimum results in each 7 measurements.
Execution time (sec)
name    before  after
loop_whileloop2  0.136  0.137
vm2_bighash*     1.249  0.623

Speedup ratio: compare with the result of `before' (greater is better)
name    after
loop_whileloop2 0.996
vm2_bighash*    2.004



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58492 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
---
 insns.def | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

(limited to 'insns.def')

diff --git a/insns.def b/insns.def
index a056b221d5..5f1ff62d1a 100644
--- a/insns.def
+++ b/insns.def
@@ -492,16 +492,12 @@ newhash
 (...)
 (VALUE val) // inc += 1 - num;
 {
-    rb_num_t i;
-
     RUBY_DTRACE_CREATE_HOOK(HASH, num);
 
     val = rb_hash_new();
 
-    for (i = num; i > 0; i -= 2) {
-	const VALUE v = TOPN(i - 2);
-	const VALUE k = TOPN(i - 1);
-	rb_hash_aset(val, k, v);
+    if (num) {
+        rb_hash_bulk_insert(num, STACK_ADDR_FROM_TOP(num), val);
     }
     POPN(num);
 }
-- 
cgit v1.2.3

