Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more writeable benches and put them in README #5880

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 76 additions & 10 deletions utils/writeable/benches/writeable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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::<u8>::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::<u8>::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::<u8>::with_capacity(500);
write!(&mut buf, "{}", DisplayMessage {
message: black_box(LONG_STR),
})
});
});
}
Expand All @@ -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",
Expand All @@ -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::<u8>::with_capacity(500);
write!(&mut buf, "{}", black_box(COMPLEX_WRITEABLE_MEDIUM))
})
});
}

criterion_group!(benches, overview_bench,);
Expand Down
16 changes: 13 additions & 3 deletions utils/writeable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//!
Expand All @@ -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
//!
//! ```
Expand Down
Loading