Skip to content

Commit 275e9dd

Browse files
committed
increase perf of charsearcher for single ascii characters
1 parent 283db70 commit 275e9dd

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

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: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,23 @@ 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+
// SAFETY: invariant
436+
return match unsafe {
437+
self.haystack.as_bytes().get_unchecked(self.finger..self.finger_back)
438+
}
439+
.iter()
440+
.position(|x| *x == self.utf8_encoded[0])
441+
{
442+
Some(x) => {
443+
self.finger += x + 1;
444+
Some((self.finger - 1, self.finger))
445+
}
446+
None => None,
447+
};
448+
}
434449
loop {
435450
// get the haystack after the last character found
436451
let bytes = self.haystack.as_bytes().get(self.finger..self.finger_back)?;
@@ -498,6 +513,23 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
498513
}
499514
#[inline]
500515
fn next_match_back(&mut self) -> Option<(usize, usize)> {
516+
if self.utf8_size == 1 {
517+
// SAFETY: invariant
518+
return match unsafe {
519+
self.haystack
520+
.get_unchecked(self.finger..self.finger_back)
521+
.as_bytes()
522+
.iter()
523+
.rposition(|&x| x == self.utf8_encoded[0])
524+
} {
525+
Some(x) => {
526+
let index = self.finger + x;
527+
self.finger_back = index;
528+
Some((self.finger_back, self.finger_back + 1))
529+
}
530+
None => None,
531+
};
532+
}
501533
let haystack = self.haystack.as_bytes();
502534
loop {
503535
// get the haystack up to but not including the last character searched

0 commit comments

Comments
 (0)