puzpuzpuz/xsync

In v2 a default hasher (of strings) is missing

Closed this issue · 5 comments

shmul commented

Hi,

The xsync.StrHash64 function of v1 was very helpful and it is a shamed it is missing from v2. Any reason why you decided not to make hashString public?
I'll be happy to issue a pull request with such a change.

Cheers,
Shmul

That function was removed in favor of more flexible hash/maphash-based hash functions. See an example of such function here:

xsync/example_test.go

Lines 21 to 32 in ace0f0f

age := xsync.NewTypedMapOf[Person, int](func(seed maphash.Seed, p Person) uint64 {
var h maphash.Hash
h.SetSeed(seed)
h.WriteString(p.GivenName)
hash := h.Sum64()
h.Reset()
h.WriteString(p.FamilyName)
hash = 31*hash + h.Sum64()
h.Reset()
binary.Write(&h, binary.LittleEndian, p.YearOfBirth)
return 31*hash + h.Sum64()
})

If you have string keys, it's trivial to implement a hash function with maphash.Hash:

func(seed maphash.Seed, s string) uint64 {
	var h maphash.Hash
	h.SetSeed(seed)
	h.WriteString(s)
	return h.Sum64()
}
shmul commented

Thanks for the reply. It is well understood and seems right. All I'm asking for is to make it a public utility function as string keys are so common.

There are NewMapOf and NewMapOfPresized built-in constructors for typed maps with string keys, so a separate function is not needed. Sorry for not mentioning this initially.

shmul commented

Perfect. Many thanks.

No problem at all. Feel free to reopen this issue or create a separate one if you find that the library API could be improved.