I am using concurrent map from this repo which only uses string as the key and it doesn't have any implementation for key as integer so I tried implementing it by just replacing all string into int64 and modify the hashing function.
Here is the gist for that where key is integer. Below is how I am hashing int64 keys. Is this the right way to hash an int64 to get the right shard?
// GetShard returns shard under given key
func (m ConcurrentMap[V]) GetShard(key int64) *ConcurrentMapShared[V] {
// I assume the hashing function of Go's map is good enough
return m[key%int64(SHARD_COUNT)]
}
When I run this code I am getting - panic: runtime error: index out of range [-7] on my above return line in GetShard function.
Is there anything wrong in my hashing function implementation? Any example on what hashing algorithm to use here with my code will help me understand better.
Do I need to use murmurhash3 here on the key and then do mod on that? If yes, any example will be appreciated.
Update
So I should change my above GetShard method to like this -
// GetShard returns shard under given key
func (m ConcurrentMap) GetShard(key int64) *ConcurrentMapShared {
var h maphash.Hash
// what is the seed value I should pass here?
h.SetSeed(seed)
binary.Write(&h, binary.LittleEndian, key)
return m[h.Sum64()%uint64(SHARD_COUNT)]
}
You can check out for comparison
puzpuzpuz/xsync#MapIts
hashUint64calculates a hash ofK(IntegerConstraint) with the given seed.Used by
NewTypedMapOf[K comparable, V any](hasher func(maphash.Seed, K) uint64) *MapOf[K, V]