Debian Codegraph @

third_party/rust/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
72 fn overview_bench(c: &mut Criterion) {
73 c.bench_function("writeable/overview", |b| {
74 b.iter(|| {
75 // This benchmark runs to_string on short, medium, and long strings in one batch.
76 WriteableMessage {
77 message: black_box(SHORT_STR),
78 }
79 .write_to_string();
80 WriteableMessage {
81 message: black_box(MEDIUM_STR),
82 }
83 .write_to_string();
84 WriteableMessage {
85 message: black_box(LONG_STR),
86 }
87 .write_to_string();
88 });
89 });
90
91 #[cfg(feature = "bench")]
92 {
93 writeable_benches(c);
94 writeable_dyn_benches(c);
95 display_benches(c);
96 complex_benches(c);
97 }
98 }
99
100 #[cfg(feature = "bench")]
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 }
130
131 #[cfg(feature = "bench")]
132 fn writeable_dyn_benches(c: &mut Criterion) {
133 // Same as `write_to_string`, but casts to a `dyn fmt::Write`
134 fn writeable_dyn_to_string(w: &impl Writeable) -> String {
135 let mut output = String::with_capacity(w.writeable_length_hint().capacity());
136 w.write_to(&mut output as &mut dyn fmt::Write)
137 .expect("impl Write for String is infallible");
138 output
139 }
140
141 c.bench_function("writeable_dyn/to_string/short", |b| {
142 b.iter(|| {
143 writeable_dyn_to_string(&WriteableMessage {
144 message: black_box(SHORT_STR),
145 })
146 });
147 });
148 c.bench_function("writeable_dyn/to_string/medium", |b| {
149 b.iter(|| {
150 writeable_dyn_to_string(&WriteableMessage {
151 message: black_box(MEDIUM_STR),
152 })
153 });
154 });
155 c.bench_function("writeable_dyn/to_string/long", |b| {
156 b.iter(|| {
157 writeable_dyn_to_string(&WriteableMessage {
158 message: black_box(LONG_STR),
159 })
160 });
161 });
162 }
163
164 #[cfg(feature = "bench")]
165 fn display_benches(c: &mut Criterion) {
166 c.bench_function("display/to_string/short", |b| {
167 b.iter(|| {
168 DisplayMessage {
169 message: black_box(SHORT_STR),
170 }
171 .to_string()
172 });
173 });
174 c.bench_function("display/to_string/medium", |b| {
175 b.iter(|| {
176 DisplayMessage {
177 message: black_box(MEDIUM_STR),
178 }
179 .to_string()
180 });
181 });
182 c.bench_function("display/to_string/long", |b| {
183 b.iter(|| {
184 DisplayMessage {
185 message: black_box(LONG_STR),
186 }
187 .to_string()
188 });
189 });
190 }
191
192 #[cfg(feature = "bench")]
193 fn complex_benches(c: &mut Criterion) {
194 const COMPLEX_WRITEABLE_MEDIUM: ComplexWriteable = ComplexWriteable {
195 prefix: "There are ",
196 n0: 55,
197 infix: " apples and ",
198 n1: 8124,
199 suffix: " oranges",
200 };
201 c.bench_function("complex/write_to_string/medium", |b| {
202 b.iter(|| {
203 black_box(COMPLEX_WRITEABLE_MEDIUM)
204 .write_to_string()
205 .into_owned()
206 });
207 });
208 c.bench_function("complex/display_to_string/medium", |b| {
209 b.iter(|| black_box(COMPLEX_WRITEABLE_MEDIUM).to_string());
210 });
211 }
212
213 criterion_group!(benches, overview_bench,);
214 criterion_main!(benches);