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

ruby-changes:46696

From: watson1978 <ko1@a...>
Date: Sat, 20 May 2017 18:23:53 +0900 (JST)
Subject: [ruby-changes:46696] watson1978:r58811 (trunk): Improve Hash#merge performance

watson1978	2017-05-20 18:23:46 +0900 (Sat, 20 May 2017)

  New Revision: 58811

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

  Log:
    Improve Hash#merge performance
    
    * hash.c (rb_hash_merge): use rb_hash_dup() instead of rb_obj_dup() to duplicate
        Hash object. rb_hash_dup() is faster duplicating function for Hash object
        which got rid of Hash#initialize_dup method calling.
    
        Hash#merge will be faster around 60%.
        [ruby-dev:50026] [Bug #13343] [Fix GH-1533]
    
    ### Before
                     user     system      total        real
    Hash#merge   0.160000   0.020000   0.180000 (  0.182357)
    
    ### After
                     user     system      total        real
    Hash#merge   0.110000   0.010000   0.120000 (  0.114404)
    
    ### Test code
    require 'benchmark'
    
    Benchmark.bmbm do |x|
      hash1 = {}
      100.times { |i| hash1[i.to_s] = i }
      hash2 = {}
      100.times { |i| hash2[(i*2).to_s] = i*2 }
    
      x.report "Hash#merge" do
        10000.times do
          hash1.merge(hash2)
        end
      end
    end

  Modified files:
    trunk/hash.c
Index: hash.c
===================================================================
--- hash.c	(revision 58810)
+++ hash.c	(revision 58811)
@@ -2496,7 +2496,7 @@ rb_hash_update_by(VALUE hash1, VALUE has https://github.com/ruby/ruby/blob/trunk/hash.c#L2496
 static VALUE
 rb_hash_merge(VALUE hash1, VALUE hash2)
 {
-    return rb_hash_update(rb_obj_dup(hash1), hash2);
+    return rb_hash_update(rb_hash_dup(hash1), hash2);
 }
 
 static int

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

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