diff --git a/utils/writeable/benches/writeable.rs b/utils/writeable/benches/writeable.rs index 6410bfd80f0..e8296b9317d 100644 --- a/utils/writeable/benches/writeable.rs +++ b/utils/writeable/benches/writeable.rs @@ -148,6 +148,33 @@ fn writeable_benches(c: &mut Criterion) { }) }); }); + c.bench_function("writeable/write_to/short", |b| { + b.iter(|| { + let mut buf = String::with_capacity(500); + WriteableMessage { + message: black_box(SHORT_STR), + } + .write_to(&mut buf) + }); + }); + c.bench_function("writeable/write_to/medium", |b| { + b.iter(|| { + let mut buf = String::with_capacity(500); + WriteableMessage { + message: black_box(MEDIUM_STR), + } + .write_to(&mut buf) + }); + }); + c.bench_function("writeable/write_to/long", |b| { + b.iter(|| { + let mut buf = String::with_capacity(500); + WriteableMessage { + message: black_box(LONG_STR), + } + .write_to(&mut buf) + }); + }); } #[cfg(feature = "bench")] @@ -187,26 +214,50 @@ fn writeable_dyn_benches(c: &mut Criterion) { fn display_benches(c: &mut Criterion) { c.bench_function("display/to_string/short", |b| { b.iter(|| { - DisplayMessage { + std::string::ToString::to_string(&DisplayMessage { message: black_box(SHORT_STR), - } - .to_string() + }) }); }); c.bench_function("display/to_string/medium", |b| { b.iter(|| { - DisplayMessage { + std::string::ToString::to_string(&DisplayMessage { message: black_box(MEDIUM_STR), - } - .to_string() + }) }); }); c.bench_function("display/to_string/long", |b| { b.iter(|| { - DisplayMessage { + std::string::ToString::to_string(&DisplayMessage { message: black_box(LONG_STR), - } - .to_string() + }) + }); + }); + c.bench_function("display/fmt/short", |b| { + b.iter(|| { + use std::io::Write; + let mut buf = Vec::::with_capacity(500); + write!(&mut buf, "{}", DisplayMessage { + message: black_box(SHORT_STR), + }) + }); + }); + c.bench_function("display/fmt/medium", |b| { + b.iter(|| { + use std::io::Write; + let mut buf = Vec::::with_capacity(500); + write!(&mut buf, "{}", DisplayMessage { + message: black_box(MEDIUM_STR), + }) + }); + }); + c.bench_function("display/fmt/long", |b| { + b.iter(|| { + use std::io::Write; + let mut buf = Vec::::with_capacity(500); + write!(&mut buf, "{}", DisplayMessage { + message: black_box(LONG_STR), + }) }); }); } @@ -228,7 +279,9 @@ fn complex_benches(c: &mut Criterion) { }); }); c.bench_function("complex/display_to_string/medium", |b| { - b.iter(|| black_box(COMPLEX_WRITEABLE_MEDIUM).to_string()); + b.iter(|| { + std::string::ToString::to_string(&black_box(COMPLEX_WRITEABLE_MEDIUM)) + }); }); const REFERENCE_STRS: [&str; 6] = [ "There are 55 apples and 8124 oranges", @@ -244,6 +297,19 @@ fn complex_benches(c: &mut Criterion) { .map(|s| writeable::cmp_str(black_box(&COMPLEX_WRITEABLE_MEDIUM), s)) }); }); + c.bench_function("complex/write_to/medium", |b| { + b.iter(|| { + let mut buf = String::with_capacity(500); + black_box(COMPLEX_WRITEABLE_MEDIUM).write_to(&mut buf) + }); + }); + c.bench_function("complex/fmt/medium", |b| { + b.iter(|| { + use std::io::Write; + let mut buf = Vec::::with_capacity(500); + write!(&mut buf, "{}", black_box(COMPLEX_WRITEABLE_MEDIUM)) + }) + }); } criterion_group!(benches, overview_bench,); diff --git a/utils/writeable/src/lib.rs b/utils/writeable/src/lib.rs index fcdfcd18369..5141a8e12f1 100644 --- a/utils/writeable/src/lib.rs +++ b/utils/writeable/src/lib.rs @@ -17,9 +17,7 @@ ) )] -//! `writeable` is a utility crate of the [`ICU4X`] project. -//! -//! It includes [`Writeable`], a core trait representing an object that can be written to a +//! This crate defines [`Writeable`], a trait representing an object that can be written to a //! sink implementing `std::fmt::Write`. It is an alternative to `std::fmt::Display` with the //! addition of a function indicating the number of bytes to be written. //! @@ -28,6 +26,18 @@ //! 1. More efficient, since the sink can pre-allocate bytes. //! 2. Smaller code, since the format machinery can be short-circuited. //! +//! This crate also exports [`TryWriteable`], a writeable that supports a custom error. +//! +//! # Benchmarks +//! +//! The benchmarks to generate the following data can be found in the `benches` directory. +//! +//! | Case | `Writeable` | `Display` | +//! |---|---|---| +//! | Create string from single-string message (139 chars) | 15.642 ns | 19.251 ns | +//! | Create string from complex message | 35.830 ns | 89.478 ns | +//! | Write complex message to buffer | 57.336 ns | 64.408 ns | +//! //! # Examples //! //! ```