-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace FNV-1a with memhash in Map (#14)
Includes the following changes: * Change hash64 return type It makes no sense to trim the returned result to uint32 in hash64. Moreover, that may make hash code distribution worse. * Replace FNV-1a hash function with memhash Replaces FNV-1a 32-bit function with faster built-in 64-bit memhash function which is internally used in Golang standard library. Co-authored-by: mert <[email protected]>
- Loading branch information
Showing
5 changed files
with
34 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,37 @@ | ||
package xsync | ||
|
||
import ( | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
const ( | ||
// used in paddings to prevent false sharing; | ||
// 64B are used instead of 128B as a compromise between | ||
// memory footprint and performance; 128B usage may give ~30% | ||
// improvement on NUMA machines | ||
cacheLineSize = 64 | ||
fnv32Offset = uint32(2166136261) | ||
fnv32Prime = uint32(16777619) | ||
// the seed value is of an absolutely arbitrary choice | ||
maphashSeed = 42 | ||
) | ||
|
||
// murmurhash3 64-bit finalizer | ||
func hash64(x uintptr) uint32 { | ||
func hash64(x uintptr) uint64 { | ||
x = ((x >> 33) ^ x) * 0xff51afd7ed558ccd | ||
x = ((x >> 33) ^ x) * 0xc4ceb9fe1a85ec53 | ||
x = (x >> 33) ^ x | ||
return uint32(x) | ||
return uint64(x) | ||
} | ||
|
||
// FNV-1a 32-bit hash | ||
func fnv32(key string) uint32 { | ||
hash := fnv32Offset | ||
keyLen := len(key) | ||
for i := 0; i < keyLen; i++ { | ||
hash ^= uint32(key[i]) | ||
hash *= fnv32Prime | ||
// exposes the built-in memhash function | ||
func maphash64(s string) uint64 { | ||
if s == "" { | ||
return maphashSeed | ||
} | ||
return hash | ||
strh := (*reflect.StringHeader)(unsafe.Pointer(&s)) | ||
return uint64(memhash(unsafe.Pointer(strh.Data), maphashSeed, uintptr(strh.Len))) | ||
} | ||
|
||
//go:noescape | ||
//go:linkname memhash runtime.memhash | ||
func memhash(p unsafe.Pointer, h, s uintptr) uintptr | ||
This comment has been minimized.
Sorry, something went wrong. |
maybe instead of converting , just link it with memhash32
https://github.com/golang/go/blob/0d0193409492b96881be6407ad50123e3557fdfb/src/runtime/alg.go#L48