Skip to content

Commit 63e2e2e

Browse files
author
Thom Chiovoloni
committed
Avoid vec! allocation in is_ascii_slice_* benches
1 parent 980d8e1 commit 63e2e2e

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/libcore/benches/ascii.rs

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,31 @@ macro_rules! benches {
5858
}
5959
)+
6060
}
61-
}
61+
};
62+
63+
// For some tests the vec allocation tends to dominate, so it can be avoided.
64+
(@readonly $( fn $name: ident($arg: ident: &[u8]) $body: block )+) => {
65+
benches!(@ro mod short_readonly SHORT $($name $arg $body)+);
66+
benches!(@ro mod medium_readonly MEDIUM $($name $arg $body)+);
67+
benches!(@ro mod long_readonly LONG $($name $arg $body)+);
68+
};
69+
(@ro mod $mod_name: ident $input: ident $($name: ident $arg: ident $body: block)+) => {
70+
mod $mod_name {
71+
use super::*;
72+
73+
$(
74+
#[bench]
75+
fn $name(bencher: &mut Bencher) {
76+
bencher.bytes = $input.len() as u64;
77+
let vec = $input.as_bytes().to_vec();
78+
bencher.iter(|| {
79+
let $arg = black_box(&vec[..]);
80+
black_box($body)
81+
})
82+
}
83+
)+
84+
}
85+
};
6286
}
6387

6488
use test::black_box;
@@ -230,14 +254,6 @@ benches! {
230254
}
231255
}
232256

233-
fn is_ascii_slice_libcore(bytes: &mut [u8]) {
234-
bytes.is_ascii()
235-
}
236-
237-
fn is_ascii_slice_iter_all(bytes: &mut [u8]) {
238-
bytes.iter().all(|b| b.is_ascii())
239-
}
240-
241257
@iter
242258

243259
is_ascii,
@@ -253,6 +269,17 @@ benches! {
253269
is_ascii_control,
254270
}
255271

272+
benches! {
273+
@readonly
274+
fn is_ascii_slice_libcore(bytes: &[u8]) {
275+
bytes.is_ascii()
276+
}
277+
278+
fn is_ascii_slice_iter_all(bytes: &[u8]) {
279+
bytes.iter().all(|b| b.is_ascii())
280+
}
281+
}
282+
256283
macro_rules! repeat {
257284
($s: expr) => {
258285
concat!($s, $s, $s, $s, $s, $s, $s, $s, $s, $s)

0 commit comments

Comments
 (0)