Skip to content

Commit 1e2fd99

Browse files
committed
LoongArch64 LSX fast-path for str.contains(&str)
Benchmark results with LLVM 21 on LA664: ``` OLD: test bench_is_contained_in ... bench: 43.63 ns/iter (+/- 0.04) NEW: test bench_is_contained_in ... bench: 12.81 ns/iter (+/- 0.01) ```
1 parent ace6330 commit 1e2fd99

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

library/core/src/str/pattern.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,10 @@ impl<'b> Pattern for &'b str {
996996
return haystack.as_bytes().contains(&self.as_bytes()[0]);
997997
}
998998

999-
#[cfg(all(target_arch = "x86_64", target_feature = "sse2"))]
999+
#[cfg(any(
1000+
all(target_arch = "x86_64", target_feature = "sse2"),
1001+
all(target_arch = "loongarch64", target_feature = "lsx")
1002+
))]
10001003
if self.len() <= 32 {
10011004
if let Some(result) = simd_contains(self, haystack) {
10021005
return result;
@@ -1774,7 +1777,10 @@ impl TwoWayStrategy for RejectAndMatch {
17741777
/// a naive O(n*m) search so this implementation should not be called on larger needles.
17751778
///
17761779
/// [0]: http://0x80.pl/articles/simd-strfind.html#sse-avx2
1777-
#[cfg(all(target_arch = "x86_64", target_feature = "sse2"))]
1780+
#[cfg(any(
1781+
all(target_arch = "x86_64", target_feature = "sse2"),
1782+
all(target_arch = "loongarch64", target_feature = "lsx")
1783+
))]
17781784
#[inline]
17791785
fn simd_contains(needle: &str, haystack: &str) -> Option<bool> {
17801786
let needle = needle.as_bytes();
@@ -1906,7 +1912,10 @@ fn simd_contains(needle: &str, haystack: &str) -> Option<bool> {
19061912
/// # Safety
19071913
///
19081914
/// Both slices must have the same length.
1909-
#[cfg(all(target_arch = "x86_64", target_feature = "sse2"))] // only called on x86
1915+
#[cfg(any(
1916+
all(target_arch = "x86_64", target_feature = "sse2"),
1917+
all(target_arch = "loongarch64", target_feature = "lsx")
1918+
))]
19101919
#[inline]
19111920
unsafe fn small_slice_eq(x: &[u8], y: &[u8]) -> bool {
19121921
debug_assert_eq!(x.len(), y.len());

0 commit comments

Comments
 (0)