diff --git a/utils/writeable/benches/writeable.rs b/utils/writeable/benches/writeable.rs index 6410bfd80f0..f0ccacd69c7 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")] @@ -209,6 +236,33 @@ fn display_benches(c: &mut Criterion) { .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), + }) + }); + }); } #[cfg(feature = "bench")] @@ -244,6 +298,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..da7b52b78ad 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) | 19.999 ns | 22.133 ns | +//! | Create string from complex message | 35.838 ns | 87.703 ns | +//! | Write complex message to buffer | 56.855 ns | 64.971 ns | +//! //! # Examples //! //! ```