Debian Codegraph @

debian/rust-vendor/writeable/benches/writeable.rs

raw
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 use criterion::{black_box, criterion_group, criterion_main, Criterion};
6 use std::fmt;
7 use writeable::LengthHint;
8 use writeable::Writeable;
9
10 /// A sample type implementing Writeable
11 struct WriteableMessage<'s> {
12 message: &'s str,
13 }
14
15 impl Writeable for WriteableMessage<'_> {
16 fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
17 sink.write_str(self.message)
18 }
19
20 fn writeable_length_hint(&self) -> LengthHint {
21 LengthHint::exact(self.message.len())
22 }
23 }
24
25 writeable::impl_display_with_writeable!(WriteableMessage<'_>);
26
27 /// A sample type implementing Display
28 struct DisplayMessage<'s> {
29 message: &'s str,
30 }
31
32 impl fmt::Display for DisplayMessage<'_> {
33 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34 f.write_str(self.message)
35 }
36 }
37
38 /// A sample type that contains multiple fields
39 struct ComplexWriteable<'a> {
40 prefix: &'a str,
41 n0: usize,
42 infix: &'a str,
43 n1: usize,
44 suffix: &'a str,
45 }
46
47 impl Writeable for ComplexWriteable<'_> {
48 fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
49 self.prefix.write_to(sink)?;
50 self.n0.write_to(sink)?;
51 self.infix.write_to(sink)?;
52 self.n1.write_to(sink)?;
53 self.suffix.write_to(sink)?;
54 Ok(())
55 }
56
57 fn writeable_length_hint(&self) -> LengthHint {
58 self.prefix.writeable_length_hint()
59 + self.n0.writeable_length_hint()
60 + self.infix.writeable_length_hint()
61 + self.n1.writeable_length_hint()
62 + self.suffix.writeable_length_hint()
63 }
64 }
65
66 writeable::impl_display_with_writeable!(ComplexWriteable<'_>);
67
68 const SHORT_STR: &str = "short";
69 const MEDIUM_STR: &str = "this is a medium-length string";
70 const LONG_STR: &str = "this string is very very very very very very very very very very very very very very very very very very very very very very very very long";
71 const LONG_OVERLAP_STR: &str =
72 "this string is very very very very very very very long but different";
73
74 fn overview_bench(c: &mut Criterion) {
75 c.bench_function("writeable/overview", |b| {
76 b.iter(|| {
77 // This benchmark runs to_string on short, medium, and long strings in one batch.
78 WriteableMessage {
79 message: black_box(SHORT_STR),
80 }
81 .write_to_string();
82 WriteableMessage {
83 message: black_box(MEDIUM_STR),
84 }
85 .write_to_string();
86 WriteableMessage {
87 message: black_box(LONG_STR),
88 }
89 .write_to_string();
90 });
91 });
92
93 {
94 writeable_benches(c);
95 writeable_dyn_benches(c);
96 display_benches(c);
97 complex_benches(c);
98 }
99 }
100
101 fn writeable_benches(c: &mut Criterion) {
102 c.bench_function("writeable/to_string/short", |b| {
103 b.iter(|| {
104 WriteableMessage {
105 message: black_box(SHORT_STR),
106 }
107 .write_to_string()
108 .into_owned()
109 });
110 });
111 c.bench_function("writeable/to_string/medium", |b| {
112 b.iter(|| {
113 WriteableMessage {
114 message: black_box(MEDIUM_STR),
115 }
116 .write_to_string()
117 .into_owned()
118 });
119 });
120 c.bench_function("writeable/to_string/long", |b| {
121 b.iter(|| {
122 WriteableMessage {
123 message: black_box(LONG_STR),
124 }
125 .write_to_string()
126 .into_owned()
127 });
128 });
129 c.bench_function("writeable/cmp_str", |b| {
130 b.iter(|| {
131 let short = black_box(SHORT_STR);
132 let medium = black_box(MEDIUM_STR);
133 let long = black_box(LONG_STR);
134 let long_overlap = black_box(LONG_OVERLAP_STR);
135 [short, medium, long, long_overlap].map(|s1| {
136 [short, medium, long, long_overlap].map(|s2| {
137 let message = WriteableMessage { message: s1 };
138 writeable::cmp_str(&message, s2)
139 })
140 })
141 });
142 });
143 c.bench_function("writeable/write_to/short", |b| {
144 b.iter(|| {
145 let mut buf = String::with_capacity(500);
146 WriteableMessage {
147 message: black_box(SHORT_STR),
148 }
149 .write_to(&mut buf)
150 });
151 });
152 c.bench_function("writeable/write_to/medium", |b| {
153 b.iter(|| {
154 let mut buf = String::with_capacity(500);
155 WriteableMessage {
156 message: black_box(MEDIUM_STR),
157 }
158 .write_to(&mut buf)
159 });
160 });
161 c.bench_function("writeable/write_to/long", |b| {
162 b.iter(|| {
163 let mut buf = String::with_capacity(500);
164 WriteableMessage {
165 message: black_box(LONG_STR),
166 }
167 .write_to(&mut buf)
168 });
169 });
170 }
171
172 fn writeable_dyn_benches(c: &mut Criterion) {
173 // Same as `write_to_string`, but casts to a `dyn fmt::Write`
174 fn writeable_dyn_to_string(w: &impl Writeable) -> String {
175 let mut output = String::with_capacity(w.writeable_length_hint().capacity());
176 w.write_to(&mut output as &mut dyn fmt::Write)
177 .expect("impl Write for String is infallible");
178 output
179 }
180
181 c.bench_function("writeable_dyn/to_string/short", |b| {
182 b.iter(|| {
183 writeable_dyn_to_string(&WriteableMessage {
184 message: black_box(SHORT_STR),
185 })
186 });
187 });
188 c.bench_function("writeable_dyn/to_string/medium", |b| {
189 b.iter(|| {
190 writeable_dyn_to_string(&WriteableMessage {
191 message: black_box(MEDIUM_STR),
192 })
193 });
194 });
195 c.bench_function("writeable_dyn/to_string/long", |b| {
196 b.iter(|| {
197 writeable_dyn_to_string(&WriteableMessage {
198 message: black_box(LONG_STR),
199 })
200 });
201 });
202 }
203
204 fn display_benches(c: &mut Criterion) {
205 c.bench_function("display/to_string/short", |b| {
206 b.iter(|| {
207 ToString::to_string(&DisplayMessage {
208 message: black_box(SHORT_STR),
209 })
210 });
211 });
212 c.bench_function("display/to_string/medium", |b| {
213 b.iter(|| {
214 ToString::to_string(&DisplayMessage {
215 message: black_box(MEDIUM_STR),
216 })
217 });
218 });
219 c.bench_function("display/to_string/long", |b| {
220 b.iter(|| {
221 ToString::to_string(&DisplayMessage {
222 message: black_box(LONG_STR),
223 })
224 });
225 });
226 c.bench_function("display/fmt/short", |b| {
227 b.iter(|| {
228 use std::io::Write;
229 let mut buf = Vec::<u8>::with_capacity(500);
230 write!(
231 &mut buf,
232 "{}",
233 DisplayMessage {
234 message: black_box(SHORT_STR),
235 }
236 )
237 });
238 });
239 c.bench_function("display/fmt/medium", |b| {
240 b.iter(|| {
241 use std::io::Write;
242 let mut buf = Vec::<u8>::with_capacity(500);
243 write!(
244 &mut buf,
245 "{}",
246 DisplayMessage {
247 message: black_box(MEDIUM_STR),
248 }
249 )
250 });
251 });
252 c.bench_function("display/fmt/long", |b| {
253 b.iter(|| {
254 use std::io::Write;
255 let mut buf = Vec::<u8>::with_capacity(500);
256 write!(
257 &mut buf,
258 "{}",
259 DisplayMessage {
260 message: black_box(LONG_STR),
261 }
262 )
263 });
264 });
265 }
266
267 fn complex_benches(c: &mut Criterion) {
268 const COMPLEX_WRITEABLE_MEDIUM: ComplexWriteable = ComplexWriteable {
269 prefix: "There are ",
270 n0: 55,
271 infix: " apples and ",
272 n1: 8124,
273 suffix: " oranges",
274 };
275 c.bench_function("complex/write_to_string/medium", |b| {
276 b.iter(|| {
277 black_box(COMPLEX_WRITEABLE_MEDIUM)
278 .write_to_string()
279 .into_owned()
280 });
281 });
282 c.bench_function("complex/display_to_string/medium", |b| {
283 b.iter(|| ToString::to_string(&black_box(COMPLEX_WRITEABLE_MEDIUM)));
284 });
285 const REFERENCE_STRS: [&str; 6] = [
286 "There are 55 apples and 8124 oranges",
287 "There are 55 apples and 0 oranges",
288 "There are no apples",
289 SHORT_STR,
290 MEDIUM_STR,
291 LONG_STR,
292 ];
293 c.bench_function("complex/cmp_str", |b| {
294 b.iter(|| {
295 black_box(REFERENCE_STRS)
296 .map(|s| writeable::cmp_str(black_box(&COMPLEX_WRITEABLE_MEDIUM), s))
297 });
298 });
299 c.bench_function("complex/write_to/medium", |b| {
300 b.iter(|| {
301 let mut buf = String::with_capacity(500);
302 black_box(COMPLEX_WRITEABLE_MEDIUM).write_to(&mut buf)
303 });
304 });
305 c.bench_function("complex/fmt/medium", |b| {
306 b.iter(|| {
307 use std::io::Write;
308 let mut buf = Vec::<u8>::with_capacity(500);
309 write!(&mut buf, "{}", black_box(COMPLEX_WRITEABLE_MEDIUM))
310 })
311 });
312 }
313
314 criterion_group!(benches, overview_bench,);
315 criterion_main!(benches);