Encourage `Hash#compare_by_identity`
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- ruby
- Domain
- performance
Research direction
The issue names no repository file or test. Start by reproducing the supplied Ruby benchmark for ordinary and identity-comparison Hash lookups, then inspect the project’s existing benchmark conventions. Done would require a clearly scoped optimization target and benchmark evidence showing the intended improvement.
Written by the indexing model from the issue text.
Description
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.new
CACHE = {KEY => :foo}
CACHE_BY_ID = {KEY => :foo}.compare_by_identity
def fast
CACHE_BY_ID[KEY]
end
def slow
CACHE[KEY]
end
Benchmark.ips do |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...)
I hope we can optimize this a bit.
- Dominant language
- Ruby
- Stars
- 5.7k
- Forks
- 370
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from fastruby/fast-ruby
-
Difficulty 2/5 1-3 hours Newbie friendliness 52/100
-
Difficulty 4/5 3-5 days Newbie friendliness 32/100
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
All issues in fastruby/fast-ruby
Similar issues
-
SyncEm always forwards a dummy block, so wrapped methods lose their no-block/Enumerator behavior Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
bug P2
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
dependabot
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
githubnext/gh-aw-cao#13349 ·
-
user-reported
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Kong/developer.konghq.com#7316 ·