Auto merge of #74289 - lzutao:unroll, r=LukasKalbertodt

Remove some redundant parts from `unrolled_find_u16s`

See each commit message for details.

r? @wesleywiser from old PR #67705 .
This commit is contained in:
bors 2020-08-08 11:34:18 +00:00
commit c989ac132a

View file

@ -99,43 +99,28 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
pub fn unrolled_find_u16s(needle: u16, haystack: &[u16]) -> Option<usize> {
let ptr = haystack.as_ptr();
let mut len = haystack.len();
let mut start = &haystack[..];
// For performance reasons unfold the loop eight times.
while len >= 8 {
if start[0] == needle {
return Some((start.as_ptr() as usize - ptr as usize) / 2);
}
if start[1] == needle {
return Some((start[1..].as_ptr() as usize - ptr as usize) / 2);
}
if start[2] == needle {
return Some((start[2..].as_ptr() as usize - ptr as usize) / 2);
}
if start[3] == needle {
return Some((start[3..].as_ptr() as usize - ptr as usize) / 2);
}
if start[4] == needle {
return Some((start[4..].as_ptr() as usize - ptr as usize) / 2);
}
if start[5] == needle {
return Some((start[5..].as_ptr() as usize - ptr as usize) / 2);
}
if start[6] == needle {
return Some((start[6..].as_ptr() as usize - ptr as usize) / 2);
}
if start[7] == needle {
return Some((start[7..].as_ptr() as usize - ptr as usize) / 2);
while start.len() >= 8 {
macro_rules! if_return {
($($n:literal,)+) => {
$(
if start[$n] == needle {
return Some((&start[$n] as *const u16 as usize - ptr as usize) / 2);
}
)+
}
}
if_return!(0, 1, 2, 3, 4, 5, 6, 7,);
start = &start[8..];
len -= 8;
}
for (i, c) in start.iter().enumerate() {
for c in start {
if *c == needle {
return Some((start.as_ptr() as usize - ptr as usize) / 2 + i);
return Some((c as *const u16 as usize - ptr as usize) / 2);
}
}
None