Skip to content

Commit

Permalink
Merge pull request #319 from epage/perf2
Browse files Browse the repository at this point in the history
perf: Backport 0.5 improvements
  • Loading branch information
epage authored Aug 17, 2023
2 parents 7c16264 + 7736667 commit 5f23a9e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,13 @@ impl<E> ErrMode<E> {
}

impl<I, E: ParseError<I>> ParseError<I> for ErrMode<E> {
#[inline]
#[inline(always)]
fn from_error_kind(input: I, kind: ErrorKind) -> Self {
ErrMode::Backtrack(E::from_error_kind(input, kind))
}

#[cfg_attr(debug_assertions, track_caller)]
#[inline]
#[inline(always)]
fn assert(input: I, message: &'static str) -> Self
where
I: crate::lib::std::fmt::Debug,
Expand Down Expand Up @@ -295,7 +295,7 @@ impl<I, EXT, E> FromExternalError<I, EXT> for ErrMode<E>
where
E: FromExternalError<I, EXT>,
{
#[inline]
#[inline(always)]
fn from_external_error(input: I, kind: ErrorKind, e: EXT) -> Self {
ErrMode::Backtrack(E::from_external_error(input, kind, e))
}
Expand Down
30 changes: 29 additions & 1 deletion src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2636,7 +2636,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 5f23a9e

Please sign in to comment.