Okay, gave FxHash a go. Implemented it like this:
import bitops
from hashes import Hash
const
rotate = 5
seed = 0x517cc1b727220a95'u64
proc hashWord(x: var Hash, word: uint64) =
x = cast[Hash]((x.uint64.rotateLeftBits(rotate) xor word) * seed)
proc hash(x: string): Hash =
var pos = 0
while x.high - pos > 8:
result.hashWord(cast[ptr uint64](x[pos].addr)[])
pos += 8
for pos in pos..x.high:
result.hashWord(x[pos].uint64)
import std/monotimes
import hashes
let s1 = getMonoTime()
var h1: Hash
for i in 0..100:
h1 = h1 !& fxhash.hash("Hello world")
echo getMonoTime() - s1
echo h1
let s2 = getMonoTime()
var h2: Hash
for i in 0..100:
h2 = h2 !& hashes.hash("Hello world")
echo getMonoTime() - s2
echo h2
Run
Runs the FxHash block in 267 nanoseconds and the built-in hash in 1058
nanoseconds.
Tried it in the benchmark and it dropped the time from ~75ms to ~53ms, so
definitely a speedup!