You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If one is using keys that can be compared by identity, and if those keys are not already optimized (symbols are optimized, not sure what else is), using compare_by_identity can yield a 4x speedup.
require'benchmark/ips'KEY=Class.newCACHE={KEY=>:foo}CACHE_BY_ID={KEY=>:foo}.compare_by_identitydeffastCACHE_BY_ID[KEY]enddefslowCACHE[KEY]endBenchmark.ipsdo |x|
x.report('lookup with compare_by_identity'){fast}x.report('lookup'){slow}x.compare!end
lookup with compare_by_identity
10.781M (± 3.3%) i/s - 54.709M in 5.081038s
lookup 2.571M (± 3.2%) i/s - 13.089M in 5.097409s
Comparison:
lookup with compare_by_identity: 10781059.3 i/s
lookup: 2570641.2 i/s - 4.19x (± 0.00) slower
Same result if lookup is not successful (as long as the hash is not empty...)
Activity
[ruby/psych] Optimize cache with `compare_by_identity`
[ruby/psych] Optimize cache with `compare_by_identity`
[ruby/psych] Optimize cache with `compare_by_identity`
[ruby/psych] Optimize cache with `compare_by_identity`
[ruby/psych] Optimize cache with `compare_by_identity`
marcandre commentedon Dec 23, 2020
Can be optimized... ruby/ruby#3987
marcandre commentedon Dec 29, 2020
In Ruby 3.0+, the performance difference will still exist but won't be as noticeable.