This project is archived and is in readonly mode.

#382 ✓resolved
Grant Hollingworth

faster Hash#except

Reported by Grant Hollingworth | June 10th, 2008 @ 05:02 PM

The current version of Hash#except uses include?, which is incredibly slow for a long list of keys.

This patch is based on a similar one for Merb. The Merb ticket includes benchmarks.

http://merb.lighthouseapp.com/pr...

Comments and changes to this ticket

  • Jeremy Kemper

    Jeremy Kemper June 10th, 2008 @ 10:24 PM

    • State changed from “new” to “open”
    • Assigned user set to “Jeremy Kemper”

    It already uses Set#include?, not Array#include?, so the benchmark is not equivalent.

    Could you base a patch off the recent change made to Hash#slice?

  • Grant Hollingworth

    Grant Hollingworth June 11th, 2008 @ 04:49 AM

    Okay, updated benchmarks:

    except: small hash, 100 000 times

                         user     system      total        real

    except_reject    1.250000   0.010000   1.260000 (  1.318881)

    except_each      0.440000   0.000000   0.440000 (  0.454850)

    except: small hash, 100 000 times

                         user     system      total        real

    except_reject!   1.310000   0.020000   1.330000 (  1.358561)

    except_each!     0.150000   0.000000   0.150000 (  0.155681)

    big hash, 1000 times

                         user     system      total        real

    except_reject    6.550000   0.050000   6.600000 (  6.696907)

    except_each      1.720000   0.020000   1.740000 (  1.780731)

    big hash, 1000 times

                         user     system      total        real

    except_reject!   8.710000   0.100000   8.810000 (  9.683930)

    except_each!     0.080000   0.000000   0.080000 (  0.105305)

    I changed the keys from a Set to an Array, like in Hash#slice. Is that what you meant?

  • Grant Hollingworth
  • Repository

    Repository June 11th, 2008 @ 08:47 AM

    • State changed from “open” to “resolved”

    (from [a797375c99efc88ee1438925cdc016305c66166c]) Performance: speed up Hash#except. [#382 state:resolved]

    http://github.com/rails/rails/co...

  • Repository

    Repository June 11th, 2008 @ 08:47 AM

    (from [634e462a0b70ddae2f21dbddddd07e7b340bb69c]) Performance: speed up Hash#except. [#382 state:resolved]

    http://github.com/rails/rails/co...

  • Mislav

    Mislav September 13th, 2008 @ 07:38 PM

    • Tag set to activesupport, core_ext, patch, tested

    This performance optimization breaks because it's using "clone" instead of "dup".

    Consider these scenarios which are valid, but which both fail now:

    
    obj1.freeze
    obj2 = obj1.except(...)
    
    # Mocha expectations
    obj1.expects(:delete).never
    obj2 = obj1.except(...)
    

    Using "dup" in place of "clone" solves both problems. Patch attached

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>

Pages