Skip to content

Commit bcf58c0

Browse files
dgryskideadprogram
authored andcommitted
runtime: add comments about the hash functions
1 parent 7a61cb1 commit bcf58c0

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/runtime/memhash_leveldb.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
//go:build runtime_memhash_leveldb
22
// +build runtime_memhash_leveldb
33

4+
// This is the hash function from Google's leveldb key-value storage system. It
5+
// processes 4 bytes at a time making it faster than the FNV hash for buffer
6+
// lengths > 16 bytes.
7+
8+
// https://github.com/google/leveldb
9+
// https://en.wikipedia.org/wiki/LevelDB
10+
411
package runtime
512

613
import (
@@ -57,6 +64,8 @@ func hash32(ptr unsafe.Pointer, n, seed uintptr) uint32 {
5764
return h
5865
}
5966

67+
// hash64finalizer is a 64-bit integer mixing function from
68+
// https://web.archive.org/web/20120720045250/http://www.cris.com/~Ttwang/tech/inthash.htm
6069
func hash64finalizer(key uint64) uint64 {
6170
key = ^key + (key << 21) // key = (key << 21) - key - 1;
6271
key = key ^ (key >> 24)
@@ -68,6 +77,9 @@ func hash64finalizer(key uint64) uint64 {
6877
return key
6978
}
7079

80+
// hash64 turns hash32 into a 64-bit hash, by use hash64finalizer to
81+
// mix the result of hash32 function combined with an xorshifted version of
82+
// the seed.
7183
func hash64(ptr unsafe.Pointer, n, seed uintptr) uint64 {
7284
h32 := hash32(ptr, n, seed)
7385
return hash64finalizer((uint64(h32^xorshift32(uint32(seed))) << 32) | uint64(h32))

src/runtime/memhash_tsip.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
//go:build runtime_memhash_tsip
22
// +build runtime_memhash_tsip
33

4+
// This is the tsip hash developed by Damian Gryski, based on ideas from SipHash.
5+
// It is slower than leveldb's hash, but should be "stronger".
6+
7+
// https://en.wikipedia.org/wiki/SipHash
8+
// https://github.com/veorq/SipHash
9+
// https://github.com/dgryski/tsip
10+
411
package runtime
512

613
import (
@@ -26,8 +33,6 @@ func ptrToSlice(ptr unsafe.Pointer, n uintptr) []byte {
2633
return p
2734
}
2835

29-
// tsip hash -- github.com/dgryski/tsip
30-
3136
type sip struct {
3237
v0, v1 uint64
3338
}

0 commit comments

Comments
 (0)