Encourage `Hash#compare_by_identity`
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 25/100
- Issue 类型
- 功能
- 描述清晰度
- 需要澄清
- 活跃度
- 停滞
- 技术栈
- ruby
- 领域
- performance
调研方向
该 issue 未指定任何仓库文件或测试。首先复现所提供的 Ruby 基准测试,涵盖普通 Hash 查找和使用 identity 比较的 Hash 查找,然后检查项目现有的基准测试约定。要视为完成,需要有一个范围明确的优化目标,以及能够显示预期改进的基准测试证据。
由索引模型根据 Issue 内容生成。
描述
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.
- 主要语言
- Ruby
- 星标
- 5.7k
- 派生
- 370
- PR 合并指标
- 30 天内没有已合并 PR
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
fastruby/fast-ruby 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 52/100
-
难度 4/5 3-5 天 新手友好度 32/100
-
难度 3/5 1-2 天 新手友好度 35/100
-
难度 2/5 1-3 小时 新手友好度 45/100
-
难度 4/5 3-5 天 新手友好度 42/100
查看 fastruby/fast-ruby 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 75/100
simp/pupmod-simp-simp#395 ·
-
难度 2/5 1-3 小时 新手友好度 80/100
simp/pupmod-simp-rsyslog#219 ·
-
难度 1/5 1 小时以内 新手友好度 90/100
simp/pupmod-simp-pupmod#256 ·
-
难度 1/5 1 小时以内 新手友好度 90/100
simp/pupmod-simp-sudo#150 ·
-
难度 1/5 1 小时以内 新手友好度 90/100