Optimizing Rabin-Karp Hashing
mattsills.github.io
Optimizing Rabin-Karp Hashing
1–10 of 10 posts
Re: Optimizing Rabin-Karp Hashing
#2Re: Optimizing Rabin-Karp Hashing
#3Re: Optimizing Rabin-Karp Hashing
#4Re: Optimizing Rabin-Karp Hashing
#5Are there actually practical cases where Rabin-Karp hashing is what dominates the running time of an application? The naive implementation already gives you 0.75 GB/s. Seems pretty fast.
Re: Optimizing Rabin-Karp Hashing
#6Are there actually practical cases where Rabin-Karp hashing is what dominates the running time of an application? The naive implementation already gives you 0.75 GB/s. Seems pretty fast.
In optimized implementations, Rabin-Karp is likely to be the bottleneck. See for instance https://github.com/facebook/zstd/pull/2483 which replaces a Rabin-Karp variant by a >2x faster Gear-Hashing.
Re: Optimizing Rabin-Karp Hashing
#7Are there actually practical cases where Rabin-Karp hashing is what dominates the running time of an application? The naive implementation already gives you 0.75 GB/s. Seems pretty fast.
Re: Optimizing Rabin-Karp Hashing
#8`_mm256_cmpeq_epi32` produces `0xFFFFFFFF`, the article suggests shifting to produce a `1` and then add.
Instead you can interpret `0xFFFFFFFF` as negative one, and subtract. That saves a shift.
Flip the sign when accumulating.
In general I think this is a pretty common counting trick. I don't think those shift operations even exists for epi8, so there you really need to use it to avoid reduction to a narrow register. Also, in the case of epi8 you need to deal with overflow, so the pattern is like this in pseudo code:
v[1:32] = 0
total = 0
for j = 0 to N / 256
for i = 0 to 255
v[1:32] -= cmpeq(..., ...)
end
total += sum(v)
endRe: Optimizing Rabin-Karp Hashing
#9Re: Optimizing Rabin-Karp Hashing
#10Regarding "A final optimization", there's another way: `_mm256_cmpeq_epi32` produces `0xFFFFFFFF`, the article suggests shifting to produce a `1` and then add. Instead you can interpret `0xFFFFFFFF` as negative one, and subtract. That saves a shift. Flip the sign when accumulating. In general I think this is a pretty common counting trick. I don't think those shift operations even exists for epi8, so there you really…