mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-10 15:14:27 +00:00
fa8001f21d
This change renames the `StatefulWidget::render` method to `render_stateful` to avoid conflicts with the `Widget::render` method. Often both the `Widget` and `StatefulWidget` traits are in scope, and when calling the `render` method on a `StatefulWidget` the compiler cannot determine which trait to use. This change resolves that issue by renaming the `StatefulWidget::render` method to `render_stateful`. The `StatefulWidget::render` method is still available, but it is deprecated. A default implementation of `render_stateful` is provided that calls the deprecated `render` method. Callers should update their code to use `render_stateful` instead of `render`. implementors of the `StatefulWidget` trait should update their implementations to implement `render_stateful` instead of `render`, and provide an implementation of `render` that calls `render_stateful`. This change is non-breaking. The deprecated `render` method will be removed in a future release of Ratatui (likely 0.29.0). Addresses part of a problem raised in <https://github.com/ratatui-org/ratatui/issues/996>
73 lines
2.4 KiB
Rust
73 lines
2.4 KiB
Rust
use criterion::{criterion_group, criterion_main, BatchSize, Bencher, BenchmarkId, Criterion};
|
|
use ratatui::{
|
|
buffer::Buffer,
|
|
layout::Rect,
|
|
widgets::{List, ListItem, ListState, StatefulWidget, Widget},
|
|
};
|
|
|
|
/// Benchmark for rendering a list.
|
|
/// It only benchmarks the render with a different amount of items.
|
|
fn list(c: &mut Criterion) {
|
|
let mut group = c.benchmark_group("list");
|
|
|
|
for line_count in [64, 2048, 16384] {
|
|
let lines: Vec<ListItem> = (0..line_count)
|
|
.map(|_| ListItem::new(fakeit::words::sentence(10)))
|
|
.collect();
|
|
|
|
// Render default list
|
|
group.bench_with_input(
|
|
BenchmarkId::new("render", line_count),
|
|
&List::new(lines.clone()),
|
|
render,
|
|
);
|
|
|
|
// Render with an offset to the middle of the list and a selected item
|
|
group.bench_with_input(
|
|
BenchmarkId::new("render_scroll_half", line_count),
|
|
&List::new(lines.clone()).highlight_symbol(">>"),
|
|
|b, list| {
|
|
render_stateful(
|
|
b,
|
|
list,
|
|
ListState::default()
|
|
.with_offset(line_count / 2)
|
|
.with_selected(Some(line_count / 2)),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
group.finish();
|
|
}
|
|
|
|
/// render the list into a common size buffer
|
|
fn render(bencher: &mut Bencher, list: &List) {
|
|
let mut buffer = Buffer::empty(Rect::new(0, 0, 200, 50));
|
|
// We use `iter_batched` to clone the value in the setup function.
|
|
// See https://github.com/ratatui-org/ratatui/pull/377.
|
|
bencher.iter_batched(
|
|
|| list.to_owned(),
|
|
|bench_list| {
|
|
Widget::render(bench_list, buffer.area, &mut buffer);
|
|
},
|
|
BatchSize::LargeInput,
|
|
);
|
|
}
|
|
|
|
/// render the list into a common size buffer with a state
|
|
fn render_stateful(bencher: &mut Bencher, list: &List, mut state: ListState) {
|
|
let mut buffer = Buffer::empty(Rect::new(0, 0, 200, 50));
|
|
// We use `iter_batched` to clone the value in the setup function.
|
|
// See https://github.com/ratatui-org/ratatui/pull/377.
|
|
bencher.iter_batched(
|
|
|| list.to_owned(),
|
|
|bench_list| {
|
|
bench_list.render_stateful(buffer.area, &mut buffer, &mut state);
|
|
},
|
|
BatchSize::LargeInput,
|
|
);
|
|
}
|
|
|
|
criterion_group!(benches, list);
|
|
criterion_main!(benches);
|