Rolling hash; Rabin-Karp string search
yurichev.com
yurichev.com
I'm guessing it's using an overly small value because it's based on java code and java is a little brain-damaged about unsigned numbers. :)
You could get a better use of your comparison register by using the syndrome of BCH code over GF(256)-- e.g. the full 64-bits for a 64-bit register instead of 56, though given how fast multipliers are these days using a prime modulus is probably fastest (in hardware or maybe in SIMD a characteristic-2 BCH code would almost certainly be faster and better!).
The code here also has the issues that permutations of the characters of the needle will match-- this may fairly bad performance for some plausible inputs. A BCH code would be less bad in that respect.
With the right choice of BCH code, you could be guaranteed that any false positive would differ in at least X characters out of Y, if Y is equal to or less than your window size, that may reduce the amount of comparisons you do for false positives (mostly interesting for very small hashes, I suppose).
They are really fast on a single thread, but at least on Xeons I use the mul and modulo seems to have a weak spot with thread parallelism though.
To fix a set of threading scaling issue with LIKE "%pattern%" SQL queries (something like TPC-H Query13), I spent a week digging through the algorithms which work better when all the threads are doing the same thing.
I narrowed down on BoyerMooreHorspool[1] as the algorithm which doesn't seem to be hit by other threads running the same algorithm (of course, everything utf-8 matching turns unicode char patterns into exact byte lookups).
[1] - https://github.com/apache/hive/blob/master/storage-api/src/j...
If the modulo is a compile time constant it should get converted into a multiply. If its not, for whatever reason the performance will be less than amazing.
https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-...
I don't see how that applies to the problem of computing a modulo while avoiding expensive cpu instructions.
This is the relevant reference:
Arch D. Robison, "{N}-bit Unsigned Division via {N}-bit Multiply-Add" (2005)
https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.51...
is that only with hyperthreading enabled or mul/modulo affect other unrelated cores as well?
> Only one byte is loaded for each character of string.
right after he calls strlen on the input on the first line of his function :P
String APIs are critical to the discussion of algorithm costs, e.g. removing the final character from a string is O(n) if they are null terminated, or O(1) if the string stores its size. Assuming the best of both worlds by ignoring strlen costs is hand-waving away a lot of the real world performance.
If you wanna be annoying about this, just imagine he's implementing `strnstr` instead of `strstr`.