@@ -37,19 +37,6 @@ type hashmapIterator struct {
3737 bucketIndex uint8
3838}
3939
40- // Get FNV-1a hash of this key.
41- //
42- // https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV-1a_hash
43- func hashmapHash (ptr unsafe.Pointer , n uintptr ) uint32 {
44- var result uint32 = 2166136261 // FNV offset basis
45- for i := uintptr (0 ); i < n ; i ++ {
46- c := * (* uint8 )(unsafe .Pointer (uintptr (ptr ) + i ))
47- result ^= uint32 (c ) // XOR with byte
48- result *= 16777619 // FNV prime
49- }
50- return result
51- }
52-
5340// Get the topmost 8 bits of the hash, without using a special value (like 0).
5441func hashmapTopHash (hash uint32 ) uint8 {
5542 tophash := uint8 (hash >> 24 )
@@ -308,7 +295,7 @@ func hashmapNext(m *hashmap, it *hashmapIterator, key, value unsafe.Pointer) boo
308295
309296func hashmapBinarySet (m * hashmap , key , value unsafe.Pointer ) {
310297 // TODO: detect nil map here and throw a better panic message?
311- hash := hashmapHash (key , uintptr (m .keySize ))
298+ hash := hash32 (key , uintptr (m .keySize ))
312299 hashmapSet (m , key , value , hash , memequal )
313300}
314301
@@ -317,15 +304,15 @@ func hashmapBinaryGet(m *hashmap, key, value unsafe.Pointer, valueSize uintptr)
317304 memzero (value , uintptr (valueSize ))
318305 return false
319306 }
320- hash := hashmapHash (key , uintptr (m .keySize ))
307+ hash := hash32 (key , uintptr (m .keySize ))
321308 return hashmapGet (m , key , value , valueSize , hash , memequal )
322309}
323310
324311func hashmapBinaryDelete (m * hashmap , key unsafe.Pointer ) {
325312 if m == nil {
326313 return
327314 }
328- hash := hashmapHash (key , uintptr (m .keySize ))
315+ hash := hash32 (key , uintptr (m .keySize ))
329316 hashmapDelete (m , key , hash , memequal )
330317}
331318
@@ -337,7 +324,7 @@ func hashmapStringEqual(x, y unsafe.Pointer, n uintptr) bool {
337324
338325func hashmapStringHash (s string ) uint32 {
339326 _s := (* _string )(unsafe .Pointer (& s ))
340- return hashmapHash (unsafe .Pointer (_s .ptr ), uintptr (_s .length ))
327+ return hash32 (unsafe .Pointer (_s .ptr ), uintptr (_s .length ))
341328}
342329
343330func hashmapStringSet (m * hashmap , key string , value unsafe.Pointer ) {
@@ -370,7 +357,7 @@ func hashmapFloat32Hash(ptr unsafe.Pointer) uint32 {
370357 // convert -0 to 0 for hashing
371358 f = 0
372359 }
373- return hashmapHash (unsafe .Pointer (& f ), 4 )
360+ return hash32 (unsafe .Pointer (& f ), 4 )
374361}
375362
376363func hashmapFloat64Hash (ptr unsafe.Pointer ) uint32 {
@@ -379,7 +366,7 @@ func hashmapFloat64Hash(ptr unsafe.Pointer) uint32 {
379366 // convert -0 to 0 for hashing
380367 f = 0
381368 }
382- return hashmapHash (unsafe .Pointer (& f ), 8 )
369+ return hash32 (unsafe .Pointer (& f ), 8 )
383370}
384371
385372func hashmapInterfaceHash (itf interface {}) uint32 {
@@ -397,9 +384,9 @@ func hashmapInterfaceHash(itf interface{}) uint32 {
397384
398385 switch x .RawType ().Kind () {
399386 case reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 :
400- return hashmapHash (ptr , x .RawType ().Size ())
387+ return hash32 (ptr , x .RawType ().Size ())
401388 case reflect .Bool , reflect .Uint , reflect .Uint8 , reflect .Uint16 , reflect .Uint32 , reflect .Uint64 , reflect .Uintptr :
402- return hashmapHash (ptr , x .RawType ().Size ())
389+ return hash32 (ptr , x .RawType ().Size ())
403390 case reflect .Float32 :
404391 // It should be possible to just has the contents. However, NaN != NaN
405392 // so if you're using lots of NaNs as map keys (you shouldn't) then hash
@@ -421,7 +408,7 @@ func hashmapInterfaceHash(itf interface{}) uint32 {
421408 // It might seem better to just return the pointer, but that won't
422409 // result in an evenly distributed hashmap. Instead, hash the pointer
423410 // like most other types.
424- return hashmapHash (ptr , x .RawType ().Size ())
411+ return hash32 (ptr , x .RawType ().Size ())
425412 case reflect .Array :
426413 var hash uint32
427414 for i := 0 ; i < x .Len (); i ++ {
0 commit comments