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.

faster Hash#except

#382

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

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

State: resolved
Milestone: none
Assigned to: Jeremy Kemper Jeremy Kemper
Importance: none

Activity

  1. Jeremy Kemper
    Jeremy Kemper
    • 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?

    June 10th, 2008 @ 10:24 PM

  2. Grant Hollingworth
    Grant Hollingworth

    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?

    June 11th, 2008 @ 04:49 AM

  3. Grant Hollingworth
  4. Repository
    Repository
    • State changed from open to resolved

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

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

    June 11th, 2008 @ 08:47 AM

  5. Repository
    Repository

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

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

    June 11th, 2008 @ 08:47 AM

  6. Mislav
    Mislav
    • 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

    September 13th, 2008 @ 07:38 PM