From d369b7cde247ce632960384448c26654ef9ed4b7 Mon Sep 17 00:00:00 2001 From: meskill <8974488+meskill@users.noreply.github.com> Date: Tue, 26 Nov 2024 16:37:49 +0000 Subject: [PATCH 1/2] chore: cleanup benchmark code --- benches/operations.rs | 60 +++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/benches/operations.rs b/benches/operations.rs index c328412..81a7451 100644 --- a/benches/operations.rs +++ b/benches/operations.rs @@ -1,4 +1,4 @@ -use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use criterion::{criterion_group, criterion_main, Criterion}; use tailcall_chunk::Chunk; const N: usize = 10000; @@ -6,79 +6,73 @@ const N: usize = 10000; fn bench_operations(c: &mut Criterion) { // Benchmark append operations c.benchmark_group("append") - .bench_function("chunk_append", |b| { + .bench_function("Chunk", |b| { b.iter(|| { let mut chunk = Chunk::default(); - for i in 0..10000 { + for i in 0..N { chunk = chunk.append(i.to_string()); } - black_box(chunk); + chunk }) }) - .bench_function("vec_append", |b| { + .bench_function("Vec", |b| { b.iter(|| { let mut vec = Vec::new(); - for i in 0..10000 { + for i in 0..N { vec.push(i.to_string()); } - black_box(vec); + vec }) }); // Benchmark prepend operations c.benchmark_group("prepend") - .bench_function("chunk_prepend", |b| { + .bench_function("Chunk", |b| { b.iter(|| { let mut chunk = Chunk::default(); - for i in 0..10000 { + for i in 0..N { chunk = chunk.prepend(i.to_string()); } - black_box(chunk); + chunk }) }) - .bench_function("vec_prepend", |b| { + .bench_function("Vec", |b| { b.iter(|| { let mut vec = Vec::new(); - for i in 0..10000 { + for i in 0..N { vec.insert(0, i.to_string()); } - black_box(vec); + vec }) }); // Benchmark concat operations c.benchmark_group("concat") - .bench_function("chunk_concat", |b| { - let chunk1: Chunk<_> = (0..5000).map(|i| i.to_string()).collect(); - let chunk2: Chunk<_> = (5000..10000).map(|i| i.to_string()).collect(); - b.iter(|| { - black_box(chunk1.clone().concat(chunk2.clone())); - }) + .bench_function("Chunk", |b| { + let chunk1: Chunk<_> = (0..N / 2).map(|i| i.to_string()).collect(); + let chunk2: Chunk<_> = (N / 2..N).map(|i| i.to_string()).collect(); + b.iter(|| chunk1.clone().concat(chunk2.clone())) }) - .bench_function("vec_concat", |b| { - let vec1: Vec<_> = (0..5000).map(|i| i.to_string()).collect(); - let vec2: Vec<_> = (5000..10000).map(|i| i.to_string()).collect(); + .bench_function("Vec", |b| { + let vec1: Vec<_> = (0..N / 2).map(|i| i.to_string()).collect(); + let vec2: Vec<_> = (N / 2..N).map(|i| i.to_string()).collect(); b.iter(|| { let mut result = vec1.clone(); result.extend(vec2.iter().cloned()); - black_box(result) + result }) }); // Benchmark clone operations c.benchmark_group("clone") - .bench_function("chunk_clone", |b| { - let chunk: Chunk<_> = (0..10000).collect(); - b.iter(|| { - black_box(chunk.clone()); - }) + .bench_function("Chunk", |b| { + let chunk: Chunk<_> = (0..N).collect(); + b.iter(|| chunk.clone()) }) - .bench_function("vec_clone", |b| { - let vec: Vec<_> = (0..10000).collect(); - b.iter(|| { - black_box(vec.clone()); - }) + .bench_function("Vec", |b| { + let vec: Vec<_> = (0..N).collect(); + b.iter(|| vec.clone()) }); // Benchmark from_iter operation From 431cfdd12bc1309304179d8c6459acf4f5cb2caa Mon Sep 17 00:00:00 2001 From: meskill <8974488+meskill@users.noreply.github.com> Date: Tue, 26 Nov 2024 16:49:04 +0000 Subject: [PATCH 2/2] perf: convert Chunk to vec in benchmarks --- benches/operations.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/benches/operations.rs b/benches/operations.rs index 81a7451..9e4a7d9 100644 --- a/benches/operations.rs +++ b/benches/operations.rs @@ -13,7 +13,7 @@ fn bench_operations(c: &mut Criterion) { chunk = chunk.append(i.to_string()); } - chunk + chunk.as_vec() }) }) .bench_function("Vec", |b| { @@ -22,6 +22,7 @@ fn bench_operations(c: &mut Criterion) { for i in 0..N { vec.push(i.to_string()); } + vec }) }); @@ -34,7 +35,7 @@ fn bench_operations(c: &mut Criterion) { for i in 0..N { chunk = chunk.prepend(i.to_string()); } - chunk + chunk.as_vec() }) }) .bench_function("Vec", |b| { @@ -50,16 +51,24 @@ fn bench_operations(c: &mut Criterion) { // Benchmark concat operations c.benchmark_group("concat") .bench_function("Chunk", |b| { - let chunk1: Chunk<_> = (0..N / 2).map(|i| i.to_string()).collect(); - let chunk2: Chunk<_> = (N / 2..N).map(|i| i.to_string()).collect(); - b.iter(|| chunk1.clone().concat(chunk2.clone())) + let part: Chunk<_> = (0..100).map(|i| i.to_string()).collect(); + b.iter(|| { + let mut chunk = Chunk::default(); + for _ in 0..N { + chunk = chunk.concat(part.clone()); + } + + chunk.as_vec() + }) }) .bench_function("Vec", |b| { - let vec1: Vec<_> = (0..N / 2).map(|i| i.to_string()).collect(); - let vec2: Vec<_> = (N / 2..N).map(|i| i.to_string()).collect(); + let part: Vec<_> = (0..100).map(|i| i.to_string()).collect(); b.iter(|| { - let mut result = vec1.clone(); - result.extend(vec2.iter().cloned()); + let mut result = Vec::new(); + for _ in 0..N { + result.extend(part.iter().cloned()); + } + result }) }); @@ -68,7 +77,7 @@ fn bench_operations(c: &mut Criterion) { c.benchmark_group("clone") .bench_function("Chunk", |b| { let chunk: Chunk<_> = (0..N).collect(); - b.iter(|| chunk.clone()) + b.iter(|| chunk.clone().as_vec()) }) .bench_function("Vec", |b| { let vec: Vec<_> = (0..N).collect(); @@ -78,7 +87,7 @@ fn bench_operations(c: &mut Criterion) { // Benchmark from_iter operation c.benchmark_group("from_iter") .bench_function("Chunk", |b| { - b.iter(|| Chunk::from_iter((0..N).into_iter())); + b.iter(|| Chunk::from_iter((0..N).into_iter()).as_vec()); }) .bench_function("Vec", |b| b.iter(|| Vec::from_iter((0..N).into_iter()))); }