Skip to content

Commit

Permalink
Update "chunk" benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeier committed Nov 14, 2023
1 parent 4444f5d commit 5cf83ea
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions benches/single_thread_with_chunks.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Single-threaded benchmarks, writing and reading chunks.
//!
//! Single-threaded usage is *not* a typical use case,
//! but the used chunk size and capacity could be.
//! Single-threaded usage is *not* a typical use case!
use std::io::{Read, Write};

Expand All @@ -10,7 +9,7 @@ use criterion::{AxisScale, PlotConfiguration};

use rtrb::{CopyToUninit, RingBuffer};

const CHUNK_SIZE: usize = 4096;
const CHUNK_SIZE: usize = 64;

fn add_function<F, M>(group: &mut criterion::BenchmarkGroup<M>, id: impl Into<String>, mut f: F)
where
Expand All @@ -35,7 +34,7 @@ pub fn criterion_benchmark(criterion: &mut criterion::Criterion) {
group.throughput(criterion::Throughput::Bytes(CHUNK_SIZE as u64));
group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));

let (mut p, mut c) = RingBuffer::<u8>::new(1000 * CHUNK_SIZE);
let (mut p, mut c) = RingBuffer::<u8>::new(CHUNK_SIZE + 1);

add_function(&mut group, "1-pop", |data| {
let mut result = [0; CHUNK_SIZE];
Expand All @@ -60,8 +59,9 @@ pub fn criterion_benchmark(criterion: &mut criterion::Criterion) {
let _ = p.write(data).unwrap();
let chunk = c.read_chunk(data.len()).unwrap();
let (first, second) = chunk.as_slices();
result.copy_from_slice(first);
debug_assert!(second.is_empty());
let mid = first.len();
result[..mid].copy_from_slice(first);
result[mid..].copy_from_slice(second);
chunk.commit_all();
result
});
Expand All @@ -70,8 +70,9 @@ pub fn criterion_benchmark(criterion: &mut criterion::Criterion) {
let mut result = [0; CHUNK_SIZE];
let mut chunk = p.write_chunk(data.len()).unwrap();
let (first, second) = chunk.as_mut_slices();
first.copy_from_slice(data);
debug_assert!(second.is_empty());
let mid = first.len();
first.copy_from_slice(&data[..mid]);
second.copy_from_slice(&data[mid..]);
chunk.commit_all();
let _ = c.read(&mut result).unwrap();
result
Expand All @@ -81,8 +82,9 @@ pub fn criterion_benchmark(criterion: &mut criterion::Criterion) {
let mut result = [0; CHUNK_SIZE];
let mut chunk = p.write_chunk_uninit(data.len()).unwrap();
let (first, second) = chunk.as_mut_slices();
data.copy_to_uninit(first);
debug_assert!(second.is_empty());
let mid = first.len();
data[..mid].copy_to_uninit(first);
data[mid..].copy_to_uninit(second);
unsafe {
chunk.commit_all();
}
Expand Down

0 comments on commit 5cf83ea

Please sign in to comment.