3 ms·
See also http://0x80.pl/articles/simd-strfind.html http://0x80.pl/articles/simd-strfind.html The basic idea there is: - mask_start = (64 chars from idx i) & (
by stabbles 4y ago
See also http://0x80.pl/articles/simd-strfind.html http://0x80.pl/articles/simd-strfind.html
The basic idea there is:
- mask_start = (64 chars from idx i) & (first letter)
- mask_end = (64 chars from idx i + len - 1) & (last letter)
if mask_start & mask_end is nonzero, you can now memcmp at the nonzero mask indices (which could also be done with AVX512 if the needle length is big enough)
That should give better performance for long needles, since you can immediately check 64 chars of the needle against the first character match, whereas OP's version does one load per needle character (ok, it does check multiple matches in a 64-char window in parallel though).
I wonder though if Boyer-Moore type of optimizations are possible (e.g. if the match breaks at substring length i, that means you can skip forward j chars from a precomputed table)
- the8472 4y agoMany string search impls are adaptive and use different algorithms depending on needle and haystack lengths and possibly also contents of the needle. E.g. https://github.com/BurntSushi/memchr#user-content-algorithms-used https://github.com/BurntSushi/memchr#user-content-algorithms...