Skip to content

Commit

Permalink
Add more writeable benches and put them in README
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Dec 6, 2024
1 parent e7c271a commit 4230fc1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
67 changes: 67 additions & 0 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 @@ -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::<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),
})
});
});
}

#[cfg(feature = "bench")]
Expand Down Expand Up @@ -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::<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) | 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
//!
//! ```
Expand Down

0 comments on commit 4230fc1

Please sign in to comment.