This project is archived and is in readonly mode.
Hash#symbolize_keys! performance
Reported by Lourens Naudé | February 7th, 2010 @ 11:27 PM
Hi,
Attached is a patch to Hash#symbolize_keys! that yields a 5 to 9x faster runtime for my test cases ( http://gist.github.com/297755 ) :
methodmissing:~ lourens$ ruby hash_bench.rb
Rehearsal
-----------------------------------------------------------------------
Hash#orig_symbolize_keys! , nil key 3.510000 0.170000 3.680000 (
3.720439)
Hash#symbolize_keys! , nil key 0.360000 0.000000 0.360000 (
0.392987)
Hash#orig_symbolize_keys! 2.020000 0.080000 2.100000 (
2.168747)
Hash#symbolize_keys! 0.360000 0.010000 0.370000 ( 0.371135)
--------------------------------------------------------------
total: 6.510000sec
user system total real
Hash#orig_symbolize_keys! , nil key 3.390000 0.160000 3.550000 (
3.551547)
Hash#symbolize_keys! , nil key 0.350000 0.000000 0.350000 (
0.361843)
Hash#orig_symbolize_keys! 3.400000 0.160000 3.560000 (
3.571425)
Hash#symbolize_keys! 0.360000 0.000000 0.360000 ( 0.362906)
- Lourens
Comments and changes to this ticket
-
Lourens Naudé February 8th, 2010 @ 12:09 AM
Updated (http://gist.github.com/297772) to handle failures in the following test cases :
- test_symbolize_keys_preserves_keys_that_cant_be_symbolized
- test_symbolize_keys_preserves_fixnum_keys
methodmissing:~ lourens$ ruby hash_bench.rb
Rehearsal -----------------------------------------------------------------------
Hash#orig_symbolize_keys! , nil key 3.500000 0.160000 3.660000 ( 3.686203)
Hash#symbolize_keys! , nil key 0.510000 0.000000 0.510000 ( 0.506390)
Hash#orig_symbolize_keys! 2.000000 0.090000 2.090000 ( 2.096217)
Hash#symbolize_keys! 0.470000 0.000000 0.470000 ( 0.468481)
-------------------------------------------------------------- total: 6.730000secuser system total real
Hash#orig_symbolize_keys! , nil key 3.520000 0.160000 3.680000 ( 3.753897)
Hash#symbolize_keys! , nil key 0.510000 0.000000 0.510000 ( 0.513103)
Hash#orig_symbolize_keys! 2.020000 0.090000 2.110000 ( 2.115938)
Hash#symbolize_keys! 0.470000 0.000000 0.470000 ( 0.481901) -
Repository March 1st, 2010 @ 07:24 PM
- State changed from new to committed
(from [2060977b767061a42eb8db2d5c3a30d205a94123]) Hash#symbolize_keys(!) optimizations
[#3891 state:committed]
Signed-off-by: Jeremy Kemper jeremy@bitsweat.net
http://github.com/rails/rails/commit/2060977b767061a42eb8db2d5c3a30... -
tribalvibes March 29th, 2010 @ 10:23 PM
Could you please show the data that generates those dubious benchmarks ?
For the (typical?) case where the keys are all strings to be symbolized, and not Fixnums, the original is about 25% faster for my tests (which do not factor out the overhead of duplicating the hash for each iteration of the destructive test.) -
Lourens Naudé March 29th, 2010 @ 11:40 PM
Hey,
Thanks for spotting this and apologies for the oversight.
I updated the gist in the original post ( http://gist.github.com/297755 ) with string hash cases and your spot on.
I'll ping Jeremy to revert in favor of the common case.
- Lourens
-
Repository March 29th, 2010 @ 11:50 PM
- State changed from committed to open
(from [e30363617cea5b51de2bb2e535c70092554514d0]) Revert "Hash#symbolize_keys(!) optimizations"
Was slower in common case.
[#3891 state:open]
This reverts commit 2060977b767061a42eb8db2d5c3a30d205a94123.
http://github.com/rails/rails/commit/e30363617cea5b51de2bb2e535c700... -
Yehuda Katz (wycats) March 30th, 2010 @ 07:26 PM
- State changed from open to invalid
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 3891 Hash#symbolize_keys! performance [#3891 state:committed]
- 3891 Hash#symbolize_keys! performance [#3891 state:open]