Skip to content

Commit 6de4f9c

Browse files
committed
Make DynamicAvx2Searcher generic over it's needle type
1 parent 1a84cd8 commit 6de4f9c

File tree

4 files changed

+15
-18
lines changed

4 files changed

+15
-18
lines changed

bench/benches/i386.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ fn search_short_haystack<M: Measurement>(c: &mut Criterion<M>) {
101101
group.bench_function("DynamicAvx2Searcher::search_in", |b| {
102102
let searchers = needles
103103
.iter()
104-
.map(|&needle| unsafe {
105-
DynamicAvx2Searcher::new(needle.as_bytes().to_owned().into_boxed_slice())
106-
})
104+
.map(|&needle| unsafe { DynamicAvx2Searcher::new(needle.as_bytes()) })
107105
.collect::<Vec<_>>();
108106

109107
b.iter(|| {
@@ -198,9 +196,7 @@ fn search_haystack<M: Measurement>(
198196
group.bench_function("DynamicAvx2Searcher::search_in", |b| {
199197
let searchers = needles
200198
.iter()
201-
.map(|needle| unsafe {
202-
DynamicAvx2Searcher::new(needle.as_bytes().to_owned().into_boxed_slice())
203-
})
199+
.map(|needle| unsafe { DynamicAvx2Searcher::new(needle.as_bytes()) })
204200
.collect::<Vec<_>>();
205201

206202
b.iter(|| {

bench/benches/random.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ fn search<M: Measurement>(c: &mut Criterion<M>) {
8585
BenchmarkId::new("DynamicAvx2Searcher::search_in", parameter),
8686
&size,
8787
|b, _| {
88-
let searcher = unsafe {
89-
DynamicAvx2Searcher::new(needle.to_owned().into_boxed_slice())
90-
};
88+
let searcher = unsafe { DynamicAvx2Searcher::new(needle) };
9189
b.iter(|| black_box(unsafe { searcher.search_in(haystack) }));
9290
},
9391
);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//! ```
1010
//! use sliceslice::x86::DynamicAvx2Searcher;
1111
//!
12-
//! let searcher = unsafe { DynamicAvx2Searcher::new(b"ipsum".to_owned().into()) };
12+
//! let searcher = unsafe { DynamicAvx2Searcher::new(b"ipsum") };
1313
//!
1414
//! assert!(unsafe {
1515
//! searcher.search_in(b"Lorem ipsum dolor sit amet, consectetur adipiscing elit")

src/x86.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ impl<N: Needle> Avx2Searcher<N> {
382382
/// up to a length of thirteen it uses specialized versions of `Avx2Searcher`,
383383
/// finally falling back to the generic version of `Avx2Searcher` for longer
384384
/// needles.
385-
pub enum DynamicAvx2Searcher {
385+
pub enum DynamicAvx2Searcher<N: Needle> {
386386
/// Specialization for needles with length 0.
387387
N0,
388388
/// Specialization for needles with length 1.
@@ -412,17 +412,17 @@ pub enum DynamicAvx2Searcher {
412412
/// Specialization for needles with length 13.
413413
N13(Avx2Searcher<[u8; 13]>),
414414
/// Fallback implementation for needles of any size.
415-
N(Avx2Searcher<Box<[u8]>>),
415+
N(Avx2Searcher<N>),
416416
}
417417

418-
impl DynamicAvx2Searcher {
418+
impl<N: Needle> DynamicAvx2Searcher<N> {
419419
/// Creates a new searcher for `needle`. By default, `position` is set to
420420
/// the last character in the needle.
421421
#[target_feature(enable = "avx2")]
422-
pub unsafe fn new(needle: Box<[u8]>) -> Self {
422+
pub unsafe fn new(needle: N) -> Self {
423423
// Wrapping prevents panicking on unsigned integer underflow when
424424
// `needle` is empty.
425-
let position = needle.len().wrapping_sub(1);
425+
let position = needle.as_bytes().len().wrapping_sub(1);
426426
Self::with_position(needle, position)
427427
}
428428

@@ -433,8 +433,8 @@ impl DynamicAvx2Searcher {
433433
/// When `needle` is not empty, panics if `position` is not a valid index
434434
/// for `needle`.
435435
#[target_feature(enable = "avx2")]
436-
pub unsafe fn with_position(needle: Box<[u8]>, position: usize) -> Self {
437-
match *needle {
436+
pub unsafe fn with_position(needle: N, position: usize) -> Self {
437+
match *needle.as_bytes() {
438438
[] => Self::N0,
439439
[c0] => {
440440
// Check that `position` is set correctly for consistency.
@@ -722,6 +722,9 @@ mod tests {
722722
fn size_of_dynamic_avx2_searcher() {
723723
use std::mem::size_of;
724724

725-
assert_eq!(size_of::<DynamicAvx2Searcher>(), 160);
725+
assert_eq!(size_of::<DynamicAvx2Searcher::<&[u8]>>(), 160);
726+
assert_eq!(size_of::<DynamicAvx2Searcher::<[u8; 0]>>(), 160);
727+
assert_eq!(size_of::<DynamicAvx2Searcher::<[u8; 16]>>(), 160);
728+
assert_eq!(size_of::<DynamicAvx2Searcher::<Box<[u8]>>>(), 160);
726729
}
727730
}

0 commit comments

Comments
 (0)