Home > Software design >  Is golang's native string hash function a perfect one?
Is golang's native string hash function a perfect one?

Time:10-09

I've found that function in the golang's source code and want to know whether it's truly a perfect hash function or not. Is it the correct way to test that?


package main

import (
    "fmt"
    "strconv"
    "unsafe"
)

//go:linkname strhash runtime.strhash
func strhash(p unsafe.Pointer, h uintptr) uintptr

const seed = 666
func main() {
    m := make(map[uintptr]string)
    for i := 0; i < 1000000000; i   {
        key := strconv.Itoa(i)
        hash := strhash(unsafe.Pointer(&key), seed)
        _, exist := m[hash]
        if exist {
            fmt.Println("collision")
            break
        }
        m[hash] = key
    }

    fmt.Println("finish")
}

CodePudding user response:

As far as I know/can tell, it is not. It uses the AES instructions to create the hash. You might want to check out something like https://github.com/cespare/mph.

  • Related