Skip to content

Commit

Permalink
perf(token): Speed up take_until under simd
Browse files Browse the repository at this point in the history
This reverts commit 489aa2a.

PR winnow-rs#86 "FindSubstring<&[u8]>: make use of `memchr::memmem`"

When testing under gitoxide, it turned out this slowed down
gitoxide-specific micro-benchmarks from ~170ns to ~240ns
  • Loading branch information
epage committed Aug 17, 2023
1 parent 15e8ddb commit c33acf5
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2576,7 +2576,35 @@ fn memchr(token: u8, slice: &[u8]) -> Option<usize> {
#[cfg(feature = "simd")]
#[inline(always)]
fn memmem(slice: &[u8], tag: &[u8]) -> Option<usize> {
memchr::memmem::find(slice, tag)
if tag.len() > slice.len() {
return None;
}

let (&substr_first, substr_rest) = match tag.split_first() {
Some(split) => split,
// an empty substring is found at position 0
// This matches the behavior of str.find("").
None => return Some(0),
};

if substr_rest.is_empty() {
return memchr::memchr(substr_first, slice);
}

let mut offset = 0;
let haystack = &slice[..slice.len() - substr_rest.len()];

while let Some(position) = memchr::memchr(substr_first, &haystack[offset..]) {
offset += position;
let next_offset = offset + 1;
if &slice[next_offset..][..substr_rest.len()] == substr_rest {
return Some(offset);
}

offset = next_offset;
}

None
}

#[cfg(not(feature = "simd"))]
Expand Down

0 comments on commit c33acf5

Please sign in to comment.