• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4 
5 // This file was adapted from https://github.com/zbraniecki/tinystr
6 
7 mod common;
8 use common::*;
9 
10 use criterion::black_box;
11 use criterion::criterion_group;
12 use criterion::criterion_main;
13 use criterion::Bencher;
14 use criterion::Criterion;
15 
16 use tinystr::TinyAsciiStr;
17 
construct_from_str(c: &mut Criterion)18 fn construct_from_str(c: &mut Criterion) {
19     macro_rules! cfs {
20         ($r:ty, $inputs:expr) => {
21             |b: &mut Bencher| {
22                 b.iter(|| {
23                     for s in $inputs {
24                         let _: $r = black_box(s.parse().unwrap());
25                     }
26                 })
27             }
28         };
29     }
30 
31     bench_block!(c, "construct_from_str", cfs);
32 }
33 
construct_from_utf8(c: &mut Criterion)34 fn construct_from_utf8(c: &mut Criterion) {
35     macro_rules! cfu {
36         ($r:ty, $inputs:expr) => {
37             |b| {
38                 let raw: Vec<&[u8]> = $inputs.iter().map(|s| s.as_bytes()).collect();
39                 b.iter(move || {
40                     for u in &raw {
41                         let _ = black_box(<$r>::try_from_utf8(*u).unwrap());
42                     }
43                 })
44             }
45         };
46     }
47 
48     let mut group4 = c.benchmark_group("construct_from_utf8/4");
49     group4.bench_function("TinyAsciiStr<4>", cfu!(TinyAsciiStr<4>, STRINGS_4));
50     group4.bench_function("TinyAsciiStr<8>", cfu!(TinyAsciiStr<8>, STRINGS_4));
51     group4.bench_function("TinyAsciiStr<16>", cfu!(TinyAsciiStr<16>, STRINGS_4));
52     group4.finish();
53 
54     let mut group8 = c.benchmark_group("construct_from_utf8/8");
55     group8.bench_function("TinyAsciiStr<8>", cfu!(TinyAsciiStr<8>, STRINGS_8));
56     group8.bench_function("TinyAsciiStr<16>", cfu!(TinyAsciiStr<16>, STRINGS_8));
57     group8.finish();
58 
59     let mut group16 = c.benchmark_group("construct_from_utf8/16");
60     group16.bench_function("TinyAsciiStr<16>", cfu!(TinyAsciiStr<16>, STRINGS_16));
61     group16.finish();
62 }
63 
64 criterion_group!(benches, construct_from_str, construct_from_utf8,);
65 criterion_main!(benches);
66