From 13e380d798cd13fac2c4683f673da50514770cb3 Mon Sep 17 00:00:00 2001 From: Thom Chiovoloni Date: Sun, 5 Jul 2020 14:51:05 -0700 Subject: [PATCH] Benchmark the unaligned case for is_ascii, and add missing SAFETY --- src/libcore/benches/ascii.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/libcore/benches/ascii.rs b/src/libcore/benches/ascii.rs index 61763dd8268..21199ecaac1 100644 --- a/src/libcore/benches/ascii.rs +++ b/src/libcore/benches/ascii.rs @@ -65,6 +65,23 @@ macro_rules! benches { benches!(@ro mod short_readonly SHORT $($name $arg $body)+); benches!(@ro mod medium_readonly MEDIUM $($name $arg $body)+); benches!(@ro mod long_readonly LONG $($name $arg $body)+); + // Add another `MEDIUM` bench, but trim the ends so that we can (try to) + // benchmark a case where the function has to handle misalignment. + mod medium_unaligned { + use super::*; + $( + #[bench] + fn $name(bencher: &mut Bencher) { + bencher.bytes = MEDIUM.len() as u64 - 2; + let mut vec = MEDIUM.as_bytes().to_vec(); + bencher.iter(|| { + black_box(&mut vec); + let $arg = black_box(&vec[1..(vec.len() - 1)]); + black_box($body) + }) + } + )+ + } }; (@ro mod $mod_name: ident $input: ident $($name: ident $arg: ident $body: block)+) => { mod $mod_name { @@ -291,10 +308,11 @@ fn is_ascii_align_to_impl(bytes: &[u8]) -> bool { if bytes.len() < core::mem::size_of::() { return bytes.iter().all(|b| b.is_ascii()); } + // SAFETY: transmuting a sequence of `u8` to `usize` is always fine let (head, body, tail) = unsafe { bytes.align_to::() }; - head.iter().all(|b| b.is_ascii()) && - body.iter().all(|w| !contains_nonascii(*w)) && - tail.iter().all(|b| b.is_ascii()) + head.iter().all(|b| b.is_ascii()) + && body.iter().all(|w| !contains_nonascii(*w)) + && tail.iter().all(|b| b.is_ascii()) } #[inline]