Skip to content

Commit

Permalink
perf: add buffer benchmarks (#1303)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshka committed Aug 7, 2024
1 parent 4753b72 commit f04bf85
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 25 deletions.
2 changes: 2 additions & 0 deletions benches/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod main {
pub mod barchart;
pub mod block;
pub mod buffer;
pub mod line;
pub mod list;
pub mod paragraph;
Expand All @@ -12,6 +13,7 @@ pub use main::*;
criterion::criterion_main!(
barchart::benches,
block::benches,
buffer::benches,
line::benches,
list::benches,
paragraph::benches,
Expand Down
65 changes: 65 additions & 0 deletions benches/main/buffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use criterion::{black_box, BenchmarkId, Criterion};
use ratatui::{
buffer::{Buffer, Cell},
layout::Rect,
text::Line,
};

criterion::criterion_group!(benches, empty, filled, with_lines);

const fn rect(size: u16) -> Rect {
Rect {
x: 0,
y: 0,
width: size,
height: size,
}
}

fn empty(c: &mut Criterion) {
let mut group = c.benchmark_group("buffer/empty");
for size in [16, 64, 255] {
let area = rect(size);
group.bench_with_input(BenchmarkId::from_parameter(size), &area, |b, &area| {
b.iter(|| {
let _buffer = Buffer::empty(black_box(area));
});
});
}
group.finish();
}

/// This likely should have the same performance as `empty`, but it's here for completeness
/// and to catch any potential performance regressions.
fn filled(c: &mut Criterion) {
let mut group = c.benchmark_group("buffer/filled");
for size in [16, 64, 255] {
let area = rect(size);
let cell = Cell::new("AAAA"); // simulate a multi-byte character
group.bench_with_input(
BenchmarkId::from_parameter(size),
&(area, cell),
|b, (area, cell)| {
b.iter(|| {
let _buffer = Buffer::filled(black_box(*area), cell.clone());
});
},
);
}
group.finish();
}

fn with_lines(c: &mut Criterion) {
let mut group = c.benchmark_group("buffer/with_lines");
for size in [16, 64, 255] {
let word_count = 50;
let lines = fakeit::words::sentence(word_count);
let lines = lines.lines().map(Line::from);
group.bench_with_input(BenchmarkId::from_parameter(size), &lines, |b, lines| {
b.iter(|| {
let _buffer = Buffer::with_lines(black_box(lines.clone()));
});
});
}
group.finish();
}
25 changes: 0 additions & 25 deletions benches/rect.rs

This file was deleted.

0 comments on commit f04bf85

Please sign in to comment.