Skip to content

Commit bd36a7c

Browse files
Auto merge of #141808 - bend-n:faster_charsearcher_take2_with_memchr, r=<try>
faster charsearcher attempt to do #141516 better resolves #82471 r? `@workingjubilee`
2 parents 792fc2b + 99e141c commit bd36a7c

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

library/core/src/slice/memchr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const fn memchr_naive(x: u8, text: &[u8]) -> Option<usize> {
4848
}
4949

5050
#[rustc_allow_const_fn_unstable(const_eval_select)] // fallback impl has same behavior
51+
#[inline]
5152
const fn memchr_aligned(x: u8, text: &[u8]) -> Option<usize> {
5253
// The runtime version behaves the same as the compiletime version, it's
5354
// just more optimized.

library/core/src/str/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ impl<'a, P: Pattern> SplitInternal<'a, P> {
656656
None
657657
}
658658

659-
#[inline]
659+
#[inline(always)]
660660
fn next(&mut self) -> Option<&'a str> {
661661
if self.finished {
662662
return None;

library/core/src/str/pattern.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,18 @@ unsafe impl<'a> Searcher<'a> for CharSearcher<'a> {
429429
SearchStep::Done
430430
}
431431
}
432-
#[inline]
432+
#[inline(always)]
433433
fn next_match(&mut self) -> Option<(usize, usize)> {
434+
if self.utf8_size == 1 {
435+
let find = |haystack: &[u8]| memchr::memchr(self.utf8_encoded[0], haystack);
436+
return match find(self.haystack.as_bytes().get(self.finger..self.finger_back)?) {
437+
Some(x) => {
438+
self.finger += x + 1;
439+
Some((self.finger - 1, self.finger))
440+
}
441+
None => None,
442+
};
443+
}
434444
loop {
435445
// get the haystack after the last character found
436446
let bytes = self.haystack.as_bytes().get(self.finger..self.finger_back)?;
@@ -498,6 +508,16 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
498508
}
499509
#[inline]
500510
fn next_match_back(&mut self) -> Option<(usize, usize)> {
511+
if self.utf8_size == 1 {
512+
let find = |haystack: &[u8]| memchr::memrchr(self.utf8_encoded[0], haystack);
513+
return match find(self.haystack.as_bytes().get(self.finger..self.finger_back)?) {
514+
Some(x) => {
515+
self.finger_back = self.finger + x;
516+
Some((self.finger_back, self.finger_back + 1))
517+
}
518+
None => None,
519+
};
520+
}
501521
let haystack = self.haystack.as_bytes();
502522
loop {
503523
// get the haystack up to but not including the last character searched

0 commit comments

Comments
 (0)