puzpuzpuz / xsync

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

In v2 a default hasher (of strings) is missing #105

Closed shmul closed 1 year ago

shmul commented 1 year ago

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

puzpuzpuz commented 1 year ago

That function was removed in favor of more flexible hash/maphash-based hash functions. See an example of such function here: https://github.com/puzpuzpuz/xsync/blob/ace0f0f74f9efcf6f83a907113446b7497eab72d/example_test.go#L21-L32

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 1 year ago

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.

puzpuzpuz commented 1 year ago

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 1 year ago

Perfect. Many thanks.

puzpuzpuz commented 1 year ago

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.