Files
next.js/turbopack/crates/turbo-rcstr/benches/mod.rs
Luke SandbergandGitHub f1899f3487 [turbopack] Create a macro rcstr! for constructing RcStr from string literals. (#79759)
## Add `rcstr!` macro for efficient string literal handling

### What?

This PR introduces a new `rcstr!` macro that creates `RcStr` instances from string literals, optimizing for inline storage when possible and using `LazyLock` for longer strings.

### Why?

The `rcstr!` macro provides several benefits:
- Allows compile-time evaluation of inline strings
- Caches longer strings using `LazyLock` to avoid repeated allocations
2025-06-01 14:24:30 -07:00

56 lines
2.0 KiB
Rust

use criterion::{BatchSize, BenchmarkId, Criterion, criterion_group, criterion_main};
use turbo_rcstr::{RcStr, rcstr};
// map has a fast-path if the Arc is uniquely owned
fn bench_map(c: &mut Criterion) {
let long_string = "this is a long string that will take time to copy".repeat(1000);
for cloned in [false, true] {
c.bench_with_input(
BenchmarkId::new("RcStr::map", if cloned { "cloned" } else { "unique" }),
&cloned,
|b, cloned| {
b.iter_batched(
|| {
let rc_str = RcStr::from(long_string.as_str());
let maybe_cloned = if *cloned { Some(rc_str.clone()) } else { None };
(rc_str, maybe_cloned)
},
|(mut rc_str, maybe_cloned)| {
rc_str = rc_str.map(|mut s| {
s.truncate(s.len() - 1);
s
});
// don't drop these ourselves, let criterion do that later
(rc_str, maybe_cloned)
},
BatchSize::LargeInput,
);
},
);
}
}
/// Compare the performance of `from` and `rcstr!`
fn bench_construct(c: &mut Criterion) {
let mut g = c.benchmark_group("Rcstr::construct");
g.bench_with_input("rcstr!/small", "small", |f, _| {
f.iter(|| rcstr!("hello"));
});
g.bench_with_input("rcstr!/large", "large", |f, _| {
f.iter(|| rcstr!("this is a long string that will take time to copy"));
});
g.bench_with_input("from/small", "small", |f, _| {
f.iter(|| RcStr::from("hello"));
});
g.bench_with_input("from/large", "large", |f, _| {
f.iter(|| RcStr::from("this is a long string that will take time to copy"));
});
}
criterion_group!(
name = benches;
config = Criterion::default();
targets = bench_map,bench_construct,
);
criterion_main!(benches);