Skip to content

Commit 147754b

Browse files
committed
vectorize eq_ignore_ascii_case
1 parent 52daa7d commit 147754b

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

library/core/src/slice/ascii.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use core::ascii::EscapeDefault;
44

55
use crate::fmt::{self, Write};
6+
use crate::hint::assert_unchecked;
67
#[cfg(not(all(target_arch = "x86_64", target_feature = "sse2")))]
78
use crate::intrinsics::const_eval_select;
89
use crate::{ascii, iter, ops};
@@ -57,25 +58,34 @@ impl [u8] {
5758
#[must_use]
5859
#[inline]
5960
pub const fn eq_ignore_ascii_case(&self, other: &[u8]) -> bool {
61+
const CHUNK_SIZE: usize = 16;
6062
if self.len() != other.len() {
6163
return false;
6264
}
63-
64-
// FIXME(const-hack): This implementation can be reverted when
65-
// `core::iter::zip` is allowed in const. The original implementation:
66-
// self.len() == other.len() && iter::zip(self, other).all(|(a, b)| a.eq_ignore_ascii_case(b))
67-
let mut a = self;
68-
let mut b = other;
69-
70-
while let ([first_a, rest_a @ ..], [first_b, rest_b @ ..]) = (a, b) {
71-
if first_a.eq_ignore_ascii_case(&first_b) {
72-
a = rest_a;
73-
b = rest_b;
74-
} else {
65+
// FIXME(const-hack): use chunks_exact or for loops.
66+
// Check in chunks for vectorization.
67+
let mut i = 0;
68+
while i + CHUNK_SIZE <= self.len() {
69+
let mut equal = true;
70+
let mut j = 0;
71+
while j < CHUNK_SIZE {
72+
// SAFETY: Outer loop stops before we could go out of bounds.
73+
unsafe { assert_unchecked(i + j < self.len()) }
74+
equal &= self[i + j].eq_ignore_ascii_case(&other[i + j]);
75+
j += 1;
76+
}
77+
if !equal {
7578
return false;
7679
}
80+
i += CHUNK_SIZE;
81+
}
82+
// Process the remainder that did not fit the above chunks.
83+
while i < self.len() {
84+
if !self[i].eq_ignore_ascii_case(&other[i]) {
85+
return false;
86+
}
87+
i += 1;
7788
}
78-
7989
true
8090
}
8191

0 commit comments

Comments
 (0)