Skip to content

Commit

Permalink
perf(bench): Used iter_batched to clone widgets in setup function (#…
Browse files Browse the repository at this point in the history
…383)

Replaced `Bencher::iter` by `Bencher::iter_batched` to clone the widget in the setup function instead of in the benchmark timing.
  • Loading branch information
Valentin271 committed Aug 11, 2023
1 parent 8c4a2e0 commit 149d489
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
14 changes: 10 additions & 4 deletions benches/block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use criterion::{criterion_group, criterion_main, Bencher, BenchmarkId, Criterion};
use criterion::{criterion_group, criterion_main, BatchSize, Bencher, BenchmarkId, Criterion};
use ratatui::{
buffer::Buffer,
layout::Rect,
Expand Down Expand Up @@ -49,9 +49,15 @@ pub fn block(c: &mut Criterion) {
/// render the block into a buffer of the given `size`
fn render(bencher: &mut Bencher, block: &Block, size: &Rect) {
let mut buffer = Buffer::empty(*size);
bencher.iter(|| {
block.clone().render(buffer.area, &mut buffer);
})
// We use `iter_batched` to clone the value in the setup function.
// See https://github.com/ratatui-org/ratatui/pull/377.
bencher.iter_batched(
|| block.to_owned(),
|bench_block| {
bench_block.render(buffer.area, &mut buffer);
},
BatchSize::SmallInput,
)
}

criterion_group!(benches, block);
Expand Down
16 changes: 12 additions & 4 deletions benches/paragraph.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use criterion::{black_box, criterion_group, criterion_main, Bencher, BenchmarkId, Criterion};
use criterion::{
black_box, criterion_group, criterion_main, BatchSize, Bencher, BenchmarkId, Criterion,
};
use ratatui::{
buffer::Buffer,
layout::Rect,
Expand Down Expand Up @@ -69,9 +71,15 @@ pub fn paragraph(c: &mut Criterion) {
/// render the paragraph into a buffer with the given width
fn render(bencher: &mut Bencher, paragraph: &Paragraph, width: u16) {
let mut buffer = Buffer::empty(Rect::new(0, 0, width, 50));
bencher.iter(|| {
paragraph.clone().render(buffer.area, &mut buffer);
})
// We use `iter_batched` to clone the value in the setup function.
// See https://github.com/ratatui-org/ratatui/pull/377.
bencher.iter_batched(
|| paragraph.to_owned(),
|bench_paragraph| {
bench_paragraph.render(buffer.area, &mut buffer);
},
BatchSize::LargeInput,
)
}

/// Create a string with the given number of lines filled with nonsense words
Expand Down

0 comments on commit 149d489

Please sign in to comment.