Skip to content

Commit c989ac1

Browse files
committed
Auto merge of rust-lang#74289 - lzutao:unroll, r=LukasKalbertodt
Remove some redundant parts from `unrolled_find_u16s` See each commit message for details. r? @wesleywiser from old PR rust-lang#67705 .
2 parents c92fc8d + f55e4d0 commit c989ac1

File tree

1 file changed

+13
-28
lines changed
  • library/std/src/sys/windows

1 file changed

+13
-28
lines changed

library/std/src/sys/windows/mod.rs

+13-28
Original file line numberDiff line numberDiff line change
@@ -99,43 +99,28 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
9999

100100
pub fn unrolled_find_u16s(needle: u16, haystack: &[u16]) -> Option<usize> {
101101
let ptr = haystack.as_ptr();
102-
let mut len = haystack.len();
103102
let mut start = &haystack[..];
104103

105104
// For performance reasons unfold the loop eight times.
106-
while len >= 8 {
107-
if start[0] == needle {
108-
return Some((start.as_ptr() as usize - ptr as usize) / 2);
109-
}
110-
if start[1] == needle {
111-
return Some((start[1..].as_ptr() as usize - ptr as usize) / 2);
112-
}
113-
if start[2] == needle {
114-
return Some((start[2..].as_ptr() as usize - ptr as usize) / 2);
115-
}
116-
if start[3] == needle {
117-
return Some((start[3..].as_ptr() as usize - ptr as usize) / 2);
118-
}
119-
if start[4] == needle {
120-
return Some((start[4..].as_ptr() as usize - ptr as usize) / 2);
121-
}
122-
if start[5] == needle {
123-
return Some((start[5..].as_ptr() as usize - ptr as usize) / 2);
124-
}
125-
if start[6] == needle {
126-
return Some((start[6..].as_ptr() as usize - ptr as usize) / 2);
127-
}
128-
if start[7] == needle {
129-
return Some((start[7..].as_ptr() as usize - ptr as usize) / 2);
105+
while start.len() >= 8 {
106+
macro_rules! if_return {
107+
($($n:literal,)+) => {
108+
$(
109+
if start[$n] == needle {
110+
return Some((&start[$n] as *const u16 as usize - ptr as usize) / 2);
111+
}
112+
)+
113+
}
130114
}
131115

116+
if_return!(0, 1, 2, 3, 4, 5, 6, 7,);
117+
132118
start = &start[8..];
133-
len -= 8;
134119
}
135120

136-
for (i, c) in start.iter().enumerate() {
121+
for c in start {
137122
if *c == needle {
138-
return Some((start.as_ptr() as usize - ptr as usize) / 2 + i);
123+
return Some((c as *const u16 as usize - ptr as usize) / 2);
139124
}
140125
}
141126
None

0 commit comments

Comments
 (0)