Skip to content

Commit

Permalink
Avoid vec! allocation in is_ascii_slice_* benches
Browse files Browse the repository at this point in the history
  • Loading branch information
Thom Chiovoloni committed Jul 5, 2020
1 parent 980d8e1 commit 63e2e2e
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions src/libcore/benches/ascii.rs
Expand Up @@ -58,7 +58,31 @@ macro_rules! benches {
}
)+
}
}
};

// For some tests the vec allocation tends to dominate, so it can be avoided.
(@readonly $( fn $name: ident($arg: ident: &[u8]) $body: block )+) => {
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)+);
};
(@ro mod $mod_name: ident $input: ident $($name: ident $arg: ident $body: block)+) => {
mod $mod_name {
use super::*;

$(
#[bench]
fn $name(bencher: &mut Bencher) {
bencher.bytes = $input.len() as u64;
let vec = $input.as_bytes().to_vec();
bencher.iter(|| {
let $arg = black_box(&vec[..]);
black_box($body)
})
}
)+
}
};
}

use test::black_box;
Expand Down Expand Up @@ -230,14 +254,6 @@ benches! {
}
}

fn is_ascii_slice_libcore(bytes: &mut [u8]) {
bytes.is_ascii()
}

fn is_ascii_slice_iter_all(bytes: &mut [u8]) {
bytes.iter().all(|b| b.is_ascii())
}

@iter

is_ascii,
Expand All @@ -253,6 +269,17 @@ benches! {
is_ascii_control,
}

benches! {
@readonly
fn is_ascii_slice_libcore(bytes: &[u8]) {
bytes.is_ascii()
}

fn is_ascii_slice_iter_all(bytes: &[u8]) {
bytes.iter().all(|b| b.is_ascii())
}
}

macro_rules! repeat {
($s: expr) => {
concat!($s, $s, $s, $s, $s, $s, $s, $s, $s, $s)
Expand Down

0 comments on commit 63e2e2e

Please sign in to comment.