Skip to content

Commit

Permalink
Add another Writeable benchmark (#4649)
Browse files Browse the repository at this point in the history
See #4590

```
complex/write_to_string/medium
                        time:   [34.344 ns 34.380 ns 34.417 ns]

complex/display_to_string/medium
                        time:   [90.373 ns 90.482 ns 90.661 ns]
```
  • Loading branch information
sffc authored Mar 5, 2024
1 parent 7428f22 commit e33525c
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions utils/writeable/benches/writeable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,36 @@ impl fmt::Display for DisplayMessage<'_> {
}
}

/// A sample type that contains multiple fields
struct ComplexWriteable<'a> {
prefix: &'a str,
n0: usize,
infix: &'a str,
n1: usize,
suffix: &'a str,
}

impl Writeable for ComplexWriteable<'_> {
fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
self.prefix.write_to(sink)?;
self.n0.write_to(sink)?;
self.infix.write_to(sink)?;
self.n1.write_to(sink)?;
self.suffix.write_to(sink)?;
Ok(())
}

fn writeable_length_hint(&self) -> LengthHint {
self.prefix.writeable_length_hint()
+ self.n0.writeable_length_hint()
+ self.infix.writeable_length_hint()
+ self.n1.writeable_length_hint()
+ self.suffix.writeable_length_hint()
}
}

writeable::impl_display_with_writeable!(ComplexWriteable<'_>);

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";
Expand Down Expand Up @@ -63,6 +93,7 @@ fn overview_bench(c: &mut Criterion) {
writeable_benches(c);
writeable_dyn_benches(c);
display_benches(c);
complex_benches(c);
}
}

Expand Down Expand Up @@ -158,5 +189,26 @@ fn display_benches(c: &mut Criterion) {
});
}

#[cfg(feature = "bench")]
fn complex_benches(c: &mut Criterion) {
const COMPLEX_WRITEABLE_MEDIUM: ComplexWriteable = ComplexWriteable {
prefix: "There are ",
n0: 55,
infix: " apples and ",
n1: 8124,
suffix: " oranges",
};
c.bench_function("complex/write_to_string/medium", |b| {
b.iter(|| {
black_box(COMPLEX_WRITEABLE_MEDIUM)
.write_to_string()
.into_owned()
});
});
c.bench_function("complex/display_to_string/medium", |b| {
b.iter(|| black_box(COMPLEX_WRITEABLE_MEDIUM).to_string());
});
}

criterion_group!(benches, overview_bench,);
criterion_main!(benches);

0 comments on commit e33525c

Please sign in to comment.