From: "matz (Yukihiro Matsumoto)" Date: 2012-10-17T23:22:29+09:00 Subject: [ruby-core:48051] [ruby-trunk - Bug #7166] Speed up Hash#dup by patching Hash#initialize_copy Issue #7166 has been updated by matz (Yukihiro Matsumoto). Compatibility is important. Call rehash in initialize_copy and see how performance changes. Matz. ---------------------------------------- Bug #7166: Speed up Hash#dup by patching Hash#initialize_copy https://bugs.ruby-lang.org/issues/7166#change-30968 Author: tenderlovemaking (Aaron Patterson) Status: Assigned Priority: Normal Assignee: tenderlovemaking (Aaron Patterson) Category: Target version: ruby -v: ruby 2.0.0dev (2012-10-15 trunk 37193) [x86_64-darwin12.2.0] Hash#dup can be sped up. Hash#initialize_copy will iterate over each pair in the hash, inserting in to the new hash. I think we can speed up hash duping by using st_copy and copying the underlying hash table. Here is the benchmark I've been using: https://gist.github.com/3893852 When you pass a hash to Hash.[], it just uses st_copy to copy the hash. If you run the benchmark, you'll see a fairly large difference between the performance of using Hash#dup and copying the hash via Hash.[]. I've attached a patch that changes Hash#initialize_copy to use st_copy. Here is a plot of the performance difference: http://i.imgur.com/ai9Am.png The blue line is the old Hash#dup, the green line is Hash#dup after my patch is applied, and the red line is copying via Hash.[]. I'm not 100% confident in this patch, so I hope someone can review more closely before applying (or rejecting!). Thanks. -- http://bugs.ruby-lang.org/