puzpuzpuz / xsync

Concurrent data structures for Go
Apache License 2.0
1.13k stars 40 forks source link

Add NewMapOfWithHasher function #137

Closed puzpuzpuz closed 4 months ago

puzpuzpuz commented 5 months ago

Adds NewMapOfWithHasher function to support custom hash functions:

m := NewMapOfWithHasher[int, int](func(i int, _ uint64) uint64 {
    // Murmur3 finalizer. No DDOS protection as it does not support seed.
    h := uint64(i)
    h = (h ^ (h >> 33)) * 0xff51afd7ed558ccd
    h = (h ^ (h >> 33)) * 0xc4ceb9fe1a85ec53
    return h ^ (h >> 33)
})

Some custom hash functions may be faster than the built-in function if the lack of DDOS protection is fine.

Murmur3 finalizer:

BenchmarkMapOfInt_Murmur3Finalizer_WarmUp/reads=100%-8          525864650            2.240 ns/op     446360938 ops/s           0 B/op          0 allocs/op
BenchmarkMapOfInt_Murmur3Finalizer_WarmUp/reads=99%-8           383333918            3.127 ns/op     319827294 ops/s           0 B/op          0 allocs/op
BenchmarkMapOfInt_Murmur3Finalizer_WarmUp/reads=90%-8           267635385            4.535 ns/op     220506863 ops/s           0 B/op          0 allocs/op
BenchmarkMapOfInt_Murmur3Finalizer_WarmUp/reads=75%-8           181292007            6.448 ns/op     155092697 ops/s           2 B/op          0 allocs/op

Built-in hash function:

BenchmarkMapOfInt_WarmUp/reads=100%-8           431097415            2.858 ns/op     349837551 ops/s           0 B/op          0 allocs/op
BenchmarkMapOfInt_WarmUp/reads=99%-8            307244330            3.951 ns/op     253072903 ops/s           0 B/op          0 allocs/op
BenchmarkMapOfInt_WarmUp/reads=90%-8            226392990            5.306 ns/op     188477583 ops/s           0 B/op          0 allocs/op
BenchmarkMapOfInt_WarmUp/reads=75%-8            159236962            7.513 ns/op     133108546 ops/s           2 B/op          0 allocs/op
codecov-commenter commented 4 months ago

:warning: Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 98.55%. Comparing base (55a8a3a) to head (03661d7).

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #137 +/- ## ========================================== + Coverage 97.87% 98.55% +0.68% ========================================== Files 8 8 Lines 1174 1174 ========================================== + Hits 1149 1157 +8 + Misses 21 14 -7 + Partials 4 3 -1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.