]> git.proxmox.com Git - rustc.git/blob - vendor/tinystr/benches/tinystr.rs
New upstream version 1.66.0+dfsg1
[rustc.git] / vendor / tinystr / benches / tinystr.rs
1 use criterion::black_box;
2 use criterion::criterion_group;
3 use criterion::criterion_main;
4 use criterion::Bencher;
5 use criterion::Criterion;
6 use criterion::Fun;
7
8 use tinystr::{TinyStr16, TinyStr4, TinyStr8};
9
10 static STRINGS_4: &[&str] = &[
11 "US", "GB", "AR", "Hans", "CN", "AT", "PL", "FR", "AT", "Cyrl", "SR", "NO", "FR", "MK", "UK",
12 ];
13
14 static STRINGS_8: &[&str] = &[
15 "Latn", "windows", "AR", "Hans", "macos", "AT", "pl", "FR", "en", "Cyrl", "SR", "NO", "419",
16 "und", "UK",
17 ];
18
19 static STRINGS_16: &[&str] = &[
20 "Latn",
21 "windows",
22 "AR",
23 "Hans",
24 "macos",
25 "AT",
26 "infiniband",
27 "FR",
28 "en",
29 "Cyrl",
30 "FromIntegral",
31 "NO",
32 "419",
33 "MacintoshOSX2019",
34 "UK",
35 ];
36
37 macro_rules! bench_block {
38 ($c:expr, $name:expr, $action:ident) => {
39 let funcs = vec![
40 Fun::new("String", $action!(String)),
41 Fun::new("TinyStr4", $action!(TinyStr4)),
42 Fun::new("TinyStr8", $action!(TinyStr8)),
43 Fun::new("TinyStr16", $action!(TinyStr16)),
44 ];
45
46 $c.bench_functions(&format!("{}/4", $name), funcs, STRINGS_4);
47
48 let funcs = vec![
49 Fun::new("String", $action!(String)),
50 Fun::new("TinyStr8", $action!(TinyStr8)),
51 Fun::new("TinyStr16", $action!(TinyStr16)),
52 ];
53
54 $c.bench_functions(&format!("{}/8", $name), funcs, STRINGS_8);
55
56 let funcs = vec![
57 Fun::new("String", $action!(String)),
58 Fun::new("TinyStr16", $action!(TinyStr16)),
59 ];
60
61 $c.bench_functions(&format!("{}/16", $name), funcs, STRINGS_16);
62 };
63 }
64
65 macro_rules! convert_to_ascii {
66 ($ty:ty, $action:ident) => {
67 |b: &mut Bencher, inputs: &&[&str]| {
68 let raw: Vec<$ty> = inputs.iter().map(|s| s.parse::<$ty>().unwrap()).collect();
69 b.iter(move || {
70 for s in &raw {
71 let _ = black_box(s.$action());
72 }
73 })
74 }
75 };
76 }
77
78 fn convert_to_ascii_lowercase(c: &mut Criterion) {
79 macro_rules! ctal {
80 ($ty:ty) => {
81 convert_to_ascii!($ty, to_ascii_lowercase)
82 };
83 }
84
85 bench_block!(c, "convert_to_ascii_lowercase", ctal);
86 }
87
88 fn convert_to_ascii_uppercase(c: &mut Criterion) {
89 macro_rules! ctau {
90 ($ty:ty) => {
91 convert_to_ascii!($ty, to_ascii_uppercase)
92 };
93 }
94
95 bench_block!(c, "convert_to_ascii_uppercase", ctau);
96 }
97
98 trait ExtToAsciiTitlecase {
99 #[inline(always)]
100 fn to_ascii_titlecase(&self) -> String;
101 }
102
103 impl ExtToAsciiTitlecase for str {
104 fn to_ascii_titlecase(&self) -> String {
105 let mut result = self.to_ascii_lowercase();
106 result[0..1].make_ascii_uppercase();
107 result
108 }
109 }
110
111 fn convert_to_ascii_titlecase(c: &mut Criterion) {
112 macro_rules! ctat {
113 ($ty:ty) => {
114 convert_to_ascii!($ty, to_ascii_titlecase)
115 };
116 }
117
118 bench_block!(c, "convert_to_ascii_titlecase", ctat);
119 }
120
121 trait ExtIsAsciiAlphanumeric {
122 #[inline(always)]
123 fn is_ascii_alphanumeric(&self) -> bool;
124 }
125
126 impl ExtIsAsciiAlphanumeric for str {
127 fn is_ascii_alphanumeric(&self) -> bool {
128 self.chars().all(|c| c.is_ascii_alphanumeric())
129 }
130 }
131
132 fn test_is_ascii_alphanumeric(c: &mut Criterion) {
133 macro_rules! tiaa {
134 ($ty:ty) => {
135 |b: &mut Bencher, inputs: &&[&str]| {
136 let raw: Vec<$ty> = inputs.iter().map(|s| s.parse::<$ty>().unwrap()).collect();
137 b.iter(move || {
138 for s in &raw {
139 let _ = black_box(s.is_ascii_alphanumeric());
140 }
141 })
142 }
143 };
144 }
145
146 bench_block!(c, "test_is_ascii_alphanumeric", tiaa);
147 }
148
149 fn test_eq(c: &mut Criterion) {
150 macro_rules! te {
151 ($ty:ty) => {
152 |b: &mut Bencher, inputs: &&[&str]| {
153 let raw: Vec<$ty> = inputs.iter().map(|s| s.parse::<$ty>().unwrap()).collect();
154 b.iter(move || {
155 for s in &raw {
156 for l in &raw {
157 let _ = black_box(s == l);
158 }
159 }
160 })
161 }
162 };
163 }
164
165 bench_block!(c, "test_eq", te);
166 }
167
168 criterion_group!(
169 benches,
170 convert_to_ascii_lowercase,
171 convert_to_ascii_uppercase,
172 convert_to_ascii_titlecase,
173 test_is_ascii_alphanumeric,
174 test_eq,
175 );
176 criterion_main!(benches);