From a5d0c2c17482e56cae5112083207e8c0ce19cb96 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 13 Jul 2020 09:34:34 +0000 Subject: [PATCH 1/3] Remove redundant len binding --- library/std/src/sys/windows/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/library/std/src/sys/windows/mod.rs b/library/std/src/sys/windows/mod.rs index 9a52371280e15..3d155a8c8f0f6 100644 --- a/library/std/src/sys/windows/mod.rs +++ b/library/std/src/sys/windows/mod.rs @@ -99,11 +99,10 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind { pub fn unrolled_find_u16s(needle: u16, haystack: &[u16]) -> Option { 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 { + while start.len() >= 8 { if start[0] == needle { return Some((start.as_ptr() as usize - ptr as usize) / 2); } @@ -130,7 +129,6 @@ pub fn unrolled_find_u16s(needle: u16, haystack: &[u16]) -> Option { } start = &start[8..]; - len -= 8; } for (i, c) in start.iter().enumerate() { From 34c343ac27c519350577bd1bfdc4d1a3b90e6db6 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 13 Jul 2020 09:35:10 +0000 Subject: [PATCH 2/3] Make use of macro to avoid repetition --- library/std/src/sys/windows/mod.rs | 33 +++++++++--------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/library/std/src/sys/windows/mod.rs b/library/std/src/sys/windows/mod.rs index 3d155a8c8f0f6..ef4041e92d7dd 100644 --- a/library/std/src/sys/windows/mod.rs +++ b/library/std/src/sys/windows/mod.rs @@ -103,31 +103,18 @@ pub fn unrolled_find_u16s(needle: u16, haystack: &[u16]) -> Option { // For performance reasons unfold the loop eight times. while start.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); + 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..]; } From f55e4d036c10f5b887b4ed8c12b7b31f3bb75d23 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Mon, 13 Jul 2020 09:35:51 +0000 Subject: [PATCH 3/3] Get pointer from address of c directly --- library/std/src/sys/windows/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/sys/windows/mod.rs b/library/std/src/sys/windows/mod.rs index ef4041e92d7dd..982ec912c44b7 100644 --- a/library/std/src/sys/windows/mod.rs +++ b/library/std/src/sys/windows/mod.rs @@ -118,9 +118,9 @@ pub fn unrolled_find_u16s(needle: u16, haystack: &[u16]) -> Option { start = &start[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