patrickmn / go-cache

An in-memory key:value store/cache (similar to Memcached) library for Go, suitable for single-machine applications.
https://patrickmn.com/projects/go-cache/
MIT License
8.16k stars 874 forks source link

// Why is all this 5x faster than a for loop? #105

Open blinkinglight opened 5 years ago

blinkinglight commented 5 years ago

https://github.com/patrickmn/go-cache/blob/5633e0862627c011927fa39556acae8b1f1df58a/sharded.go#L40

can you benchmark this?

func djb33(seed uint32, k string) uint32 {
    var (
        l = uint32(len(k))
        d = 5381 + seed + l
    )
    if l > 0 {
        d = djb33_loop(d, k)
    }
    return d ^ (d >> 16)
}

func djb33_loop(d uint32, k string) uint32 {
    var (
        l = uint32(len(k))
        i = uint32(0)
    )
loop:
    if i >= l-1 {
        goto exit
    }
    d = (d * 33) ^ uint32(k[i])
    i++
    goto loop
exit:
    return d
}
bazuker commented 4 years ago

http://www.cse.yorku.ca/~oz/hash.html

this algorithm (k=33) was first reported by dan bernstein many years ago in comp.lang.c. another version of this algorithm (now favored by bernstein) uses xor: hash(i) = hash(i - 1) * 33 ^ str[i]; the magic of number 33 (why it works better than many other constants, prime or not) has never been adequately explained.