Skip to content

faster charsearcher #141808

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

Closed
Closed
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
22 changes: 21 additions & 1 deletion library/core/src/str/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,18 @@ unsafe impl<'a> Searcher<'a> for CharSearcher<'a> {
SearchStep::Done
}
}
#[inline]
#[inline(always)]
fn next_match(&mut self) -> Option<(usize, usize)> {
if self.utf8_size == 1 {
let find = |haystack: &[u8]| memchr::memchr(self.utf8_encoded[0], haystack);
return match find(self.haystack.as_bytes().get(self.finger..self.finger_back)?) {
Some(x) => {
self.finger += x + 1;
Some((self.finger - 1, self.finger))
}
None => None,
};
}
loop {
// get the haystack after the last character found
let bytes = self.haystack.as_bytes().get(self.finger..self.finger_back)?;
Expand Down Expand Up @@ -498,6 +508,16 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
}
#[inline]
fn next_match_back(&mut self) -> Option<(usize, usize)> {
if self.utf8_size == 1 {
let find = |haystack: &[u8]| memchr::memrchr(self.utf8_encoded[0], haystack);
return match find(self.haystack.as_bytes().get(self.finger..self.finger_back)?) {
Some(x) => {
self.finger_back = self.finger + x;
Some((self.finger_back, self.finger_back + 1))
}
None => None,
};
}
let haystack = self.haystack.as_bytes();
loop {
// get the haystack up to but not including the last character searched
Expand Down
24 changes: 24 additions & 0 deletions library/coretests/benches/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,27 @@ fn ends_with_str(b: &mut Bencher) {
}
})
}

#[bench]
fn splitn_on_http_response(b: &mut Bencher) {
fn parse_http(s: &str) -> Result<(&str, &str, &str), &str> {
let mut parts = s.splitn(3, ' ');
let version = parts.next().ok_or("No version")?;
let code = parts.next().ok_or("No status code")?;
let description = parts.next().ok_or("No description")?;
Ok((version, code, description))
}

let response = String::from("HTTP/1.1 418 I'm a teapot\r\n");
let mut res: (&str, &str, &str) = ("", "", "");
b.iter(|| {
for _ in 0..1024 {
res = black_box(match parse_http(black_box(&response)) {
Ok(data) => data,
Err(_) => {
continue;
}
})
}
})
}
Loading