Skip to content

Commit 2a3f91a

Browse files
committed
Specialize for needles up to length 16
LLVM has specialized code for memcmp up to length 32, moreover the size of `DynamicAvx2Searcher` is not impacted by needles up to length 16. Benchmarks results summary: * `short_haystack`: +1% instructions * `long_haystack`: -1% instructions * `random_haystack`: -0.5% instructions
1 parent 0fc32d9 commit 2a3f91a

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/x86.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ impl<N: Needle> Avx2Searcher<N> {
296296
Some(11) => memcmp::specialized::<10>(chunk, needle),
297297
Some(12) => memcmp::specialized::<11>(chunk, needle),
298298
Some(13) => memcmp::specialized::<12>(chunk, needle),
299+
Some(14) => memcmp::specialized::<13>(chunk, needle),
300+
Some(15) => memcmp::specialized::<14>(chunk, needle),
301+
Some(16) => memcmp::specialized::<15>(chunk, needle),
299302
_ => memcmp::generic(chunk, needle, self.needle.size() - 1),
300303
};
301304
if equal {
@@ -412,6 +415,12 @@ pub enum DynamicAvx2Searcher<N: Needle> {
412415
N12(Avx2Searcher<[u8; 12]>),
413416
/// Specialization for needles with length 13.
414417
N13(Avx2Searcher<[u8; 13]>),
418+
/// Specialization for needles with length 14.
419+
N14(Avx2Searcher<[u8; 14]>),
420+
/// Specialization for needles with length 15.
421+
N15(Avx2Searcher<[u8; 15]>),
422+
/// Specialization for needles with length 16.
423+
N16(Avx2Searcher<[u8; 16]>),
415424
/// Fallback implementation for needles of any size.
416425
N(Avx2Searcher<N>),
417426
}
@@ -460,6 +469,9 @@ impl<N: Needle> DynamicAvx2Searcher<N> {
460469
array!(c, 11) => Self::N11(Avx2Searcher::with_position(array!(c, 11), position)),
461470
array!(c, 12) => Self::N12(Avx2Searcher::with_position(array!(c, 12), position)),
462471
array!(c, 13) => Self::N13(Avx2Searcher::with_position(array!(c, 13), position)),
472+
array!(c, 14) => Self::N14(Avx2Searcher::with_position(array!(c, 14), position)),
473+
array!(c, 15) => Self::N15(Avx2Searcher::with_position(array!(c, 15), position)),
474+
array!(c, 16) => Self::N16(Avx2Searcher::with_position(array!(c, 16), position)),
463475
_ => Self::N(Avx2Searcher::with_position(needle, position)),
464476
}
465477
}
@@ -483,6 +495,9 @@ impl<N: Needle> DynamicAvx2Searcher<N> {
483495
Self::N11(searcher) => searcher.inlined_search_in(haystack),
484496
Self::N12(searcher) => searcher.inlined_search_in(haystack),
485497
Self::N13(searcher) => searcher.inlined_search_in(haystack),
498+
Self::N14(searcher) => searcher.inlined_search_in(haystack),
499+
Self::N15(searcher) => searcher.inlined_search_in(haystack),
500+
Self::N16(searcher) => searcher.inlined_search_in(haystack),
486501
Self::N(searcher) => searcher.inlined_search_in(haystack),
487502
}
488503
}

0 commit comments

Comments
 (0)