Function core::str::pattern::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.

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.