Skip to content

Commit

Permalink
Auto merge of #74289 - lzutao:unroll, r=LukasKalbertodt
Browse files Browse the repository at this point in the history
Remove some redundant parts from `unrolled_find_u16s`

See each commit message for details.

r? @wesleywiser from old PR #67705 .
  • Loading branch information
bors committed Aug 8, 2020
2 parents c92fc8d + f55e4d0 commit c989ac1
Showing 1 changed file with 13 additions and 28 deletions.
41 changes: 13 additions & 28 deletions library/std/src/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c989ac1

Please sign in to comment.