Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8b87fef

Browse files
committedMar 16, 2025
Auto merge of rust-lang#138537 - yotamofek:pr/lib/multi-char-pattern, r=jhpratt
Optimize multi-char string patterns Uses specialization for `[T]::contains` from rust-lang#130991 to optimize multi-char patterns in string searches. Requesting a perf run to see if this actually has an effect 🙏 (I think that adding `char` to the list of types for which the `SliceContains` is specialized is a good idea, even if it doesn't show up on perf - might be helpful for downstream users)
2 parents d497e43 + bfe5363 commit 8b87fef

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed
 

‎library/core/src/slice/cmp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,4 @@ macro_rules! impl_slice_contains {
282282
};
283283
}
284284

285-
impl_slice_contains!(u16, u32, u64, i16, i32, i64, f32, f64, usize, isize);
285+
impl_slice_contains!(u16, u32, u64, i16, i32, i64, f32, f64, usize, isize, char);

‎library/core/src/str/pattern.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -644,21 +644,21 @@ where
644644
impl<const N: usize> MultiCharEq for [char; N] {
645645
#[inline]
646646
fn matches(&mut self, c: char) -> bool {
647-
self.iter().any(|&m| m == c)
647+
self.contains(&c)
648648
}
649649
}
650650

651651
impl<const N: usize> MultiCharEq for &[char; N] {
652652
#[inline]
653653
fn matches(&mut self, c: char) -> bool {
654-
self.iter().any(|&m| m == c)
654+
self.contains(&c)
655655
}
656656
}
657657

658658
impl MultiCharEq for &[char] {
659659
#[inline]
660660
fn matches(&mut self, c: char) -> bool {
661-
self.iter().any(|&m| m == c)
661+
self.contains(&c)
662662
}
663663
}
664664

0 commit comments

Comments
 (0)
Failed to load comments.