Skip to content

Commit

Permalink
Replace original searchers with submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
zakcutner committed Aug 28, 2020
1 parent 8375986 commit 700f13f
Show file tree
Hide file tree
Showing 21 changed files with 229 additions and 453 deletions.
13 changes: 9 additions & 4 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ jobs:
cargo:
name: Cargo
runs-on: ubuntu-latest
env:
CXX: clang++
LLVM_CONFIG_PATH: /usr/lib/llvm-9/bin/llvm-config
steps:
- uses: actions/checkout@v2
with:
submodules: true
- uses: actions-rs/toolchain@v1
with:
components: clippy, rustfmt
default: true
- run: cargo fmt -- --check
- run: cargo build --all-targets --all-features
- run: cargo clippy --all-targets --all-features -- -D warnings
- run: cargo test --all-features
- run: cargo fmt --all -- --check
- run: cargo build --workspace --all-targets --all-features
- run: cargo clippy --workspace --all-targets --all-features -- -D warnings
- run: cargo test --workspace --all-features
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/target
target/
Cargo.lock
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "sse4-strstr/src/sse4-strstr"]
path = sse4-strstr/src/sse4-strstr
url = https://github.com/WojciechMula/sse4-strstr.git
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ memchr = "2.3"
[dev-dependencies]
criterion = "0.3"
memmem = "0.1"
sse4-strstr = { path = "sse4-strstr" }
twoway = "0.2"

[[bench]]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A fast implementation of single-pattern substring search using SIMD acceleration
## Example

```rust
use sliceslice::x86::avx2::DynamicAvx2Searcher;
use sliceslice::x86::DynamicAvx2Searcher;

fn main() {
let searcher = unsafe { DynamicAvx2Searcher::new(b"ipsum".to_owned().into()) };
Expand Down
38 changes: 9 additions & 29 deletions benches/i386.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#![allow(deprecated)]

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use memmem::{Searcher, TwoWaySearcher};
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
use sliceslice::x86::avx2::{deprecated::*, *};
use std::{
fs::{self, File},
io::{BufRead, BufReader},
Expand All @@ -29,7 +25,7 @@ fn search_short_haystack(c: &mut Criterion) {
});
});

group.bench_function("TwoWaySearcher::search_in", |b| {
group.bench_function("memmem::TwoWaySearcher::search_in", |b| {
let searchers = needles
.iter()
.map(|needle| TwoWaySearcher::new(needle.as_bytes()))
Expand All @@ -56,24 +52,14 @@ fn search_short_haystack(c: &mut Criterion) {

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
group.bench_function("strstr_avx2_original", |b| {
b.iter(|| {
for (i, needle) in needles.iter().enumerate() {
for haystack in &needles[i..] {
black_box(unsafe {
strstr_avx2_original(haystack.as_bytes(), needle.as_bytes())
});
}
}
});
});
use sliceslice::x86::DynamicAvx2Searcher;

group.bench_function("strstr_avx2_rust", |b| {
group.bench_function("sse4_strstr::avx2_strstr_v2", |b| {
b.iter(|| {
for (i, needle) in needles.iter().enumerate() {
for haystack in &needles[i..] {
black_box(unsafe {
strstr_avx2_rust(haystack.as_bytes(), needle.as_bytes())
sse4_strstr::avx2_strstr_v2(haystack.as_bytes(), needle.as_bytes())
});
}
}
Expand Down Expand Up @@ -120,7 +106,7 @@ fn search_long_haystack(c: &mut Criterion) {
});
});

group.bench_function("TwoWaySearcher::search_in", |b| {
group.bench_function("memmem::TwoWaySearcher::search_in", |b| {
let searchers = needles
.iter()
.map(|needle| TwoWaySearcher::new(needle.as_bytes()))
Expand All @@ -143,24 +129,18 @@ fn search_long_haystack(c: &mut Criterion) {

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
group.bench_function("strstr_avx2_original", |b| {
use sliceslice::x86::DynamicAvx2Searcher;

group.bench_function("sse4_strstr::avx2_strstr_v2", |b| {
b.iter(|| {
for needle in &needles {
black_box(unsafe {
strstr_avx2_original(haystack.as_bytes(), needle.as_bytes())
sse4_strstr::avx2_strstr_v2(haystack.as_bytes(), needle.as_bytes())
});
}
});
});

group.bench_function("strstr_avx2_rust", |b| {
b.iter(|| {
for needle in &needles {
black_box(unsafe { strstr_avx2_rust(haystack.as_bytes(), needle.as_bytes()) });
}
});
});

group.bench_function("DynamicAvx2Searcher::search_in", |b| {
let searchers = needles
.iter()
Expand Down
19 changes: 6 additions & 13 deletions benches/random.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#![allow(deprecated)]

use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use memmem::{Searcher, TwoWaySearcher};
use sliceslice::x86::avx2::{deprecated::*, *};

fn search(c: &mut Criterion) {
let haystack = include_str!("../data/haystack");
Expand Down Expand Up @@ -30,7 +27,7 @@ fn search(c: &mut Criterion) {
let needle = needle.as_bytes();

group.bench_with_input(
BenchmarkId::new("TwoWaySearcher::search_in", parameter),
BenchmarkId::new("memmem::TwoWaySearcher::search_in", parameter),
&size,
|b, _| {
let searcher = TwoWaySearcher::new(needle);
Expand All @@ -48,19 +45,15 @@ fn search(c: &mut Criterion) {

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
{
group.bench_with_input(
BenchmarkId::new("strstr_avx2_original", parameter),
&size,
|b, _| {
b.iter(|| black_box(unsafe { strstr_avx2_original(haystack, needle) }));
},
);
use sliceslice::x86::DynamicAvx2Searcher;

group.bench_with_input(
BenchmarkId::new("strstr_avx2_rust", parameter),
BenchmarkId::new("sse4_strstr::avx2_strstr_v2", parameter),
&size,
|b, _| {
b.iter(|| black_box(unsafe { strstr_avx2_rust(haystack, needle) }));
b.iter(|| {
black_box(unsafe { sse4_strstr::avx2_strstr_v2(haystack, needle) })
});
},
);

Expand Down
72 changes: 67 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! # Example
//!
//! ```
//! use sliceslice::x86::avx2::DynamicAvx2Searcher;
//! use sliceslice::x86::DynamicAvx2Searcher;
//!
//! let searcher = unsafe { DynamicAvx2Searcher::new(b"ipsum".to_owned().into()) };
//!
Expand All @@ -19,15 +19,77 @@
//! searcher.search_in(b"foo bar baz qux quux quuz corge grault garply waldo fred")
//! });

#![allow(deprecated)]
#![warn(missing_docs)]

/// Substring search implementations using the `memchr` function.
pub mod memchr;

/// Substring search implementations using x86 architecture features.
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub mod x86;

mod bits;
mod memcmp;

use memchr::memchr;

/// Single-byte searcher using `memchr` for faster matching.
pub struct MemchrSearcher(u8);

impl MemchrSearcher {
/// Creates a new searcher for `needle`.
pub fn new(needle: u8) -> Self {
Self(needle)
}

/// Inlined version of `search_in` for hot call sites.
#[inline]
pub fn inlined_search_in(&self, haystack: &[u8]) -> bool {
if haystack.is_empty() {
return false;
}

memchr(self.0, haystack).is_some()
}

/// Performs a substring search for the `needle` within `haystack`.
pub fn search_in(&self, haystack: &[u8]) -> bool {
self.inlined_search_in(haystack)
}
}

#[cfg(test)]
mod tests {
use super::MemchrSearcher;

fn memchr_search(haystack: &[u8], needle: &[u8]) -> bool {
MemchrSearcher::new(needle[0]).search_in(haystack)
}

#[test]
fn memchr_search_same() {
assert!(memchr_search(b"f", b"f"));
}

#[test]
fn memchr_search_different() {
assert!(!memchr_search(b"foo", b"b"));
}

#[test]
fn memchr_search_prefix() {
assert!(memchr_search(b"foobar", b"f"));
}

#[test]
fn memchr_search_suffix() {
assert!(memchr_search(b"foobar", b"r"));
}

#[test]
fn memchr_search_mutiple() {
assert!(memchr_search(b"foobarfoo", b"o"));
}

#[test]
fn memchr_search_middle() {
assert!(memchr_search(b"foobarfoo", b"b"));
}
}
65 changes: 0 additions & 65 deletions src/memchr.rs

This file was deleted.

Loading

0 comments on commit 700f13f

Please sign in to comment.