Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Hash#symbolize_keys! performance

#3891

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

Reported by Lourens Naudé · February 7th, 2010 @ 11:27 PM

State: invalid
Milestone: none
Assigned to: Carl Lerche Carl Lerche
Importance: none

Activity

  1. Lourens Naudé
    Lourens Naudé

    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.730000sec

                                          user     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)

    February 8th, 2010 @ 12:09 AM

  2. Repository
  3. tribalvibes
    tribalvibes

    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.)

    March 29th, 2010 @ 10:23 PM

  4. Lourens Naudé
    Lourens Naudé

    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

    March 29th, 2010 @ 11:40 PM

  5. Repository
    Repository
    • State changed from committed to open

    (from [e30363617cea5b51de2bb2e535c70092554514d0]) Revert "Hash#symbolize_keys(!) optimizations"

    Was slower in common case.

    [#3891 Hash#symbolize_keys! performance state:open]

    This reverts commit 2060977b767061a42eb8db2d5c3a30d205a94123.
    http://github.com/rails/rails/commit/e30363617cea5b51de2bb2e535c700...

    March 29th, 2010 @ 11:50 PM

  6. Yehuda Katz (wycats)
    Yehuda Katz (wycats)
    • State changed from open to invalid

    March 30th, 2010 @ 07:26 PM