summaryrefslogtreecommitdiffstats
path: root/third_party/rust/bumpalo/benches
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/bumpalo/benches
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/bumpalo/benches')
-rw-r--r--third_party/rust/bumpalo/benches/benches.rs66
1 files changed, 66 insertions, 0 deletions
diff --git a/third_party/rust/bumpalo/benches/benches.rs b/third_party/rust/bumpalo/benches/benches.rs
new file mode 100644
index 0000000000..5f1415f546
--- /dev/null
+++ b/third_party/rust/bumpalo/benches/benches.rs
@@ -0,0 +1,66 @@
+use criterion::*;
+
+#[derive(Default)]
+struct Small(u8);
+
+#[derive(Default)]
+struct Big([usize; 32]);
+
+fn alloc<T: Default>(n: usize) {
+ let arena = bumpalo::Bump::with_capacity(n * std::mem::size_of::<T>());
+ for _ in 0..n {
+ let arena = black_box(&arena);
+ let val: &mut T = arena.alloc(black_box(Default::default()));
+ black_box(val);
+ }
+}
+
+fn alloc_with<T: Default>(n: usize) {
+ let arena = bumpalo::Bump::with_capacity(n * std::mem::size_of::<T>());
+ for _ in 0..n {
+ let arena = black_box(&arena);
+ let val: &mut T = arena.alloc_with(|| black_box(Default::default()));
+ black_box(val);
+ }
+}
+
+#[cfg(feature = "collections")]
+fn format_realloc(bump: &bumpalo::Bump, n: usize) {
+ let n = criterion::black_box(n);
+ let s = bumpalo::format!(in bump, "Hello {:.*}", n, "World! ");
+ criterion::black_box(s);
+}
+
+const ALLOCATIONS: usize = 10_000;
+
+fn bench_alloc(c: &mut Criterion) {
+ let mut group = c.benchmark_group("alloc");
+ group.throughput(Throughput::Elements(ALLOCATIONS as u64));
+ group.bench_function("small", |b| b.iter(|| alloc::<Small>(ALLOCATIONS)));
+ group.bench_function("big", |b| b.iter(|| alloc::<Big>(ALLOCATIONS)));
+}
+
+fn bench_alloc_with(c: &mut Criterion) {
+ let mut group = c.benchmark_group("alloc-with");
+ group.throughput(Throughput::Elements(ALLOCATIONS as u64));
+ group.bench_function("small", |b| b.iter(|| alloc_with::<Small>(ALLOCATIONS)));
+ group.bench_function("big", |b| b.iter(|| alloc_with::<Big>(ALLOCATIONS)));
+}
+
+fn bench_format_realloc(c: &mut Criterion) {
+ let mut group = c.benchmark_group("format-realloc");
+
+ for n in (1..5).map(|n| n * n * n * 10) {
+ group.throughput(Throughput::Elements(n as u64));
+ group.bench_with_input(BenchmarkId::new("format-realloc", n), &n, |b, n| {
+ let mut bump = bumpalo::Bump::new();
+ b.iter(|| {
+ bump.reset();
+ format_realloc(&bump, *n);
+ });
+ });
+ }
+}
+
+criterion_group!(benches, bench_alloc, bench_alloc_with, bench_format_realloc);
+criterion_main!(benches);