1 use super::{LONG, MEDIUM, SHORT};
6 ($( fn $name: ident($arg: ident: &[u8]) $body: block )+) => {
7 benches!(mod short SHORT[..] $($name $arg $body)+);
8 benches!(mod medium MEDIUM[..] $($name $arg $body)+);
9 benches!(mod long LONG[..] $($name $arg $body)+);
10 // Ensure we benchmark cases where the functions are called with strings
11 // that are not perfectly aligned or have a length which is not a
12 // multiple of size_of::<usize>() (or both)
13 benches!(mod unaligned_head MEDIUM[1..] $($name $arg $body)+);
14 benches!(mod unaligned_tail MEDIUM[..(MEDIUM.len() - 1)] $($name $arg $body)+);
15 benches!(mod unaligned_both MEDIUM[1..(MEDIUM.len() - 1)] $($name $arg $body)+);
18 (mod $mod_name: ident $input: ident [$range: expr] $($name: ident $arg: ident $body: block)+) => {
23 fn $name(bencher: &mut Bencher) {
24 bencher.bytes = $input[$range].len() as u64;
25 let mut vec = $input.as_bytes().to_vec();
27 let $arg: &[u8] = &black_box(&mut vec)[$range];
37 fn case00_libcore(bytes: &[u8]) {
41 fn case01_iter_all(bytes: &[u8]) {
42 bytes.iter().all(|b| b.is_ascii())
45 fn case02_align_to(bytes: &[u8]) {
46 is_ascii_align_to(bytes)
49 fn case03_align_to_unrolled(bytes: &[u8]) {
50 is_ascii_align_to_unrolled(bytes)
54 // These are separate since it's easier to debug errors if they don't go through
55 // macro expansion first.
56 fn is_ascii_align_to(bytes: &[u8]) -> bool {
57 if bytes.len() < core::mem::size_of::<usize>() {
58 return bytes.iter().all(|b| b.is_ascii());
60 // SAFETY: transmuting a sequence of `u8` to `usize` is always fine
61 let (head, body, tail) = unsafe { bytes.align_to::<usize>() };
62 head.iter().all(|b| b.is_ascii())
63 && body.iter().all(|w| !contains_nonascii(*w))
64 && tail.iter().all(|b| b.is_ascii())
67 fn is_ascii_align_to_unrolled(bytes: &[u8]) -> bool {
68 if bytes.len() < core::mem::size_of::<usize>() {
69 return bytes.iter().all(|b| b.is_ascii());
71 // SAFETY: transmuting a sequence of `u8` to `[usize; 2]` is always fine
72 let (head, body, tail) = unsafe { bytes.align_to::<[usize; 2]>() };
73 head.iter().all(|b| b.is_ascii())
74 && body.iter().all(|w| !contains_nonascii(w[0] | w[1]))
75 && tail.iter().all(|b| b.is_ascii())
79 fn contains_nonascii(v: usize) -> bool {
80 const NONASCII_MASK: usize = usize::from_ne_bytes([0x80; core::mem::size_of::<usize>()]);
81 (NONASCII_MASK & v) != 0