1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use std::fmt;
use writeable::LengthHint;
use writeable::Writeable;
/// A sample type implementing Writeable
struct WriteableMessage<'s> {
message: &'s str,
}
impl Writeable for WriteableMessage<'_> {
fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
sink.write_str(self.message)
}
fn writeable_length_hint(&self) -> LengthHint {
LengthHint::exact(self.message.len())
}
}
writeable::impl_display_with_writeable!(WriteableMessage<'_>);
/// A sample type implementing Display
struct DisplayMessage<'s> {
message: &'s str,
}
impl fmt::Display for DisplayMessage<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(self.message)
}
}
const SHORT_STR: &str = "short";
const MEDIUM_STR: &str = "this is a medium-length string";
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";
fn overview_bench(c: &mut Criterion) {
c.bench_function("writeable/overview", |b| {
b.iter(|| {
// This benchmark runs to_string on short, medium, and long strings in one batch.
WriteableMessage {
message: black_box(SHORT_STR),
}
.write_to_string();
WriteableMessage {
message: black_box(MEDIUM_STR),
}
.write_to_string();
WriteableMessage {
message: black_box(LONG_STR),
}
.write_to_string();
});
});
#[cfg(feature = "bench")]
{
writeable_benches(c);
writeable_dyn_benches(c);
display_benches(c);
}
}
#[cfg(feature = "bench")]
fn writeable_benches(c: &mut Criterion) {
c.bench_function("writeable/to_string/short", |b| {
b.iter(|| {
WriteableMessage {
message: black_box(SHORT_STR),
}
.write_to_string()
.into_owned()
});
});
c.bench_function("writeable/to_string/medium", |b| {
b.iter(|| {
WriteableMessage {
message: black_box(MEDIUM_STR),
}
.write_to_string()
.into_owned()
});
});
c.bench_function("writeable/to_string/long", |b| {
b.iter(|| {
WriteableMessage {
message: black_box(LONG_STR),
}
.write_to_string()
.into_owned()
});
});
}
#[cfg(feature = "bench")]
fn writeable_dyn_benches(c: &mut Criterion) {
// Same as `write_to_string`, but casts to a `dyn fmt::Write`
fn writeable_dyn_to_string(w: &impl Writeable) -> String {
let mut output = String::with_capacity(w.writeable_length_hint().capacity());
w.write_to(&mut output as &mut dyn fmt::Write)
.expect("impl Write for String is infallible");
output
}
c.bench_function("writeable_dyn/to_string/short", |b| {
b.iter(|| {
writeable_dyn_to_string(&WriteableMessage {
message: black_box(SHORT_STR),
})
});
});
c.bench_function("writeable_dyn/to_string/medium", |b| {
b.iter(|| {
writeable_dyn_to_string(&WriteableMessage {
message: black_box(MEDIUM_STR),
})
});
});
c.bench_function("writeable_dyn/to_string/long", |b| {
b.iter(|| {
writeable_dyn_to_string(&WriteableMessage {
message: black_box(LONG_STR),
})
});
});
}
#[cfg(feature = "bench")]
fn display_benches(c: &mut Criterion) {
c.bench_function("display/to_string/short", |b| {
b.iter(|| {
DisplayMessage {
message: black_box(SHORT_STR),
}
.to_string()
});
});
c.bench_function("display/to_string/medium", |b| {
b.iter(|| {
DisplayMessage {
message: black_box(MEDIUM_STR),
}
.to_string()
});
});
c.bench_function("display/to_string/long", |b| {
b.iter(|| {
DisplayMessage {
message: black_box(LONG_STR),
}
.to_string()
});
});
}
criterion_group!(benches, overview_bench,);
criterion_main!(benches);
|