fastruby / fastruby/fast-ruby

Encourage `Hash#compare_by_identity`

オープン
#189 コメント 2 件 リアクション 2 件 担当者 0 名 GitHub で見る
主要言語
Ruby
スター
5.7k
フォーク
371
PR マージ指標
30日以内にマージされた PR はありません

説明

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.

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

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。