From f04bf855cbc28e0ae29eaf678f26425a05f2295e Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Tue, 6 Aug 2024 23:05:36 -0700 Subject: [PATCH] perf: add buffer benchmarks (#1303) --- benches/main.rs | 2 ++ benches/main/buffer.rs | 65 ++++++++++++++++++++++++++++++++++++++++++ benches/rect.rs | 25 ---------------- 3 files changed, 67 insertions(+), 25 deletions(-) create mode 100644 benches/main/buffer.rs delete mode 100644 benches/rect.rs diff --git a/benches/main.rs b/benches/main.rs index 4651b73c..b6ad7f18 100644 --- a/benches/main.rs +++ b/benches/main.rs @@ -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; @@ -12,6 +13,7 @@ pub use main::*; criterion::criterion_main!( barchart::benches, block::benches, + buffer::benches, line::benches, list::benches, paragraph::benches, diff --git a/benches/main/buffer.rs b/benches/main/buffer.rs new file mode 100644 index 00000000..2f7801e5 --- /dev/null +++ b/benches/main/buffer.rs @@ -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(); +} diff --git a/benches/rect.rs b/benches/rect.rs deleted file mode 100644 index 18c93142..00000000 --- a/benches/rect.rs +++ /dev/null @@ -1,25 +0,0 @@ -use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion}; -use ratatui::layout::Rect; - -fn rect_rows_benchmark(c: &mut Criterion) { - let rect_sizes = vec![ - Rect::new(0, 0, 1, 16), - Rect::new(0, 0, 1, 1024), - Rect::new(0, 0, 1, 65535), - ]; - let mut group = c.benchmark_group("rect_rows"); - for rect in rect_sizes { - group.bench_with_input(BenchmarkId::new("rows", rect.height), &rect, |b, rect| { - b.iter(|| { - for row in rect.rows() { - // Perform any necessary operations on each row - black_box(row); - } - }); - }); - } - group.finish(); -} - -criterion_group!(benches, rect_rows_benchmark); -criterion_main!(benches);