Skip to content

Make DynamicAvx2Searcher generic over it's needle type #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions bench/benches/i386.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ fn search_short_haystack<M: Measurement>(c: &mut Criterion<M>) {
group.bench_function("DynamicAvx2Searcher::search_in", |b| {
let searchers = needles
.iter()
.map(|&needle| unsafe {
DynamicAvx2Searcher::new(needle.as_bytes().to_owned().into_boxed_slice())
})
.map(|&needle| unsafe { DynamicAvx2Searcher::new(needle.as_bytes()) })
.collect::<Vec<_>>();

b.iter(|| {
Expand Down Expand Up @@ -198,9 +196,7 @@ fn search_haystack<M: Measurement>(
group.bench_function("DynamicAvx2Searcher::search_in", |b| {
let searchers = needles
.iter()
.map(|needle| unsafe {
DynamicAvx2Searcher::new(needle.as_bytes().to_owned().into_boxed_slice())
})
.map(|needle| unsafe { DynamicAvx2Searcher::new(needle.as_bytes()) })
.collect::<Vec<_>>();

b.iter(|| {
Expand Down
4 changes: 1 addition & 3 deletions bench/benches/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ fn search<M: Measurement>(c: &mut Criterion<M>) {
BenchmarkId::new("DynamicAvx2Searcher::search_in", parameter),
&size,
|b, _| {
let searcher = unsafe {
DynamicAvx2Searcher::new(needle.to_owned().into_boxed_slice())
};
let searcher = unsafe { DynamicAvx2Searcher::new(needle) };
b.iter(|| black_box(unsafe { searcher.search_in(haystack) }));
},
);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! ```
//! use sliceslice::x86::DynamicAvx2Searcher;
//!
//! let searcher = unsafe { DynamicAvx2Searcher::new(b"ipsum".to_owned().into()) };
//! let searcher = unsafe { DynamicAvx2Searcher::new(b"ipsum") };
//!
//! assert!(unsafe {
//! searcher.search_in(b"Lorem ipsum dolor sit amet, consectetur adipiscing elit")
Expand Down
19 changes: 11 additions & 8 deletions src/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ impl<N: Needle> Avx2Searcher<N> {
/// up to a length of thirteen it uses specialized versions of `Avx2Searcher`,
/// finally falling back to the generic version of `Avx2Searcher` for longer
/// needles.
pub enum DynamicAvx2Searcher {
pub enum DynamicAvx2Searcher<N: Needle> {
/// Specialization for needles with length 0.
N0,
/// Specialization for needles with length 1.
Expand Down Expand Up @@ -412,17 +412,17 @@ pub enum DynamicAvx2Searcher {
/// Specialization for needles with length 13.
N13(Avx2Searcher<[u8; 13]>),
/// Fallback implementation for needles of any size.
N(Avx2Searcher<Box<[u8]>>),
N(Avx2Searcher<N>),
}

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

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

assert_eq!(size_of::<DynamicAvx2Searcher>(), 160);
assert_eq!(size_of::<DynamicAvx2Searcher::<&[u8]>>(), 160);
assert_eq!(size_of::<DynamicAvx2Searcher::<[u8; 0]>>(), 160);
assert_eq!(size_of::<DynamicAvx2Searcher::<[u8; 16]>>(), 160);
assert_eq!(size_of::<DynamicAvx2Searcher::<Box<[u8]>>>(), 160);
}
}