Skip to content

Commit

Permalink
perf(otelstorage): optimize allocations in Hash
Browse files Browse the repository at this point in the history
Use stack-allocated slice, if possible.

```
Hash-32   767.7n ± 1%   684.8n ± 2%  -10.80% (p=0.000 n=15)

Hash-32   384.0 ± 0%   0.0 ± 0%  -100.00% (p=0.000 n=15)

Hash-32   1.000 ± 0%   0.000 ± 0%  -100.00% (p=0.000 n=15)
```
  • Loading branch information
tdakkota committed Jan 17, 2024
1 parent 5206cfa commit 60b8d63
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions internal/otelstorage/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ func AttrHash(m pcommon.Map) Hash {
return h.Sum128().Bytes()
}

// StrHash computes string hash.
func StrHash(s string) Hash {
h := xxh3.New()
_, _ = h.WriteString(s)
return h.Sum128().Bytes()
}

func hashValue(h *xxh3.Hasher, val pcommon.Value) {
var buf [8]byte

Expand Down Expand Up @@ -76,7 +69,12 @@ func hashMap(h *xxh3.Hasher, m pcommon.Map) {
key string
value pcommon.Value
}
pairs := make([]pair, 0, m.Len())
var pairs []pair
if l := m.Len(); l < 16 {
pairs = make([]pair, 0, 16)
} else {
pairs = make([]pair, 0, m.Len())
}
m.Range(func(k string, v pcommon.Value) bool {
pairs = append(pairs, pair{
key: k,
Expand Down

0 comments on commit 60b8d63

Please sign in to comment.