Function simd_contains

Source
fn simd_contains(needle: &str, haystack: &str) -> Option<bool>
🔬This is a nightly-only experimental API. (pattern #27721)
Expand description

SIMD search for short needles based on Wojciech Muła’s “SIMD-friendly algorithms for substring searching”0

It skips ahead by the vector width on each iteration (rather than the needle length as two-way does) by probing the first and last byte of the needle for the whole vector width and only doing full needle comparisons when the vectorized probe indicated potential matches.

Since the x86_64 baseline only offers SSE2 we only use u8x16 here. If we ever ship std with for x86-64-v3 or adapt this for other platforms then wider vectors should be evaluated.

Similarly, on LoongArch the 128-bit LSX vector extension is the baseline, so we also use u8x16 there. Wider vector widths may be considered for future LoongArch extensions (e.g., LASX).

For haystacks smaller than vector-size + needle length it falls back to a naive O(n*m) search so this implementation should not be called on larger needles.