mirror of
https://github.com/ratatui/ratatui.git
synced 2025-10-02 15:25:54 +00:00
perf(buffer): simplify Buffer::filled with macro (#1036)
The `vec![]` macro is highly optimized by the Rust team and shorter. Don't do it manually. This change is mainly cleaner code. The only production code that uses this is `Terminal::with_options` and `Terminal::insert_before` so it's not performance relevant on every render.
This commit is contained in:
parent
c75aa1990f
commit
2e71c1874e
@ -1,8 +1,7 @@
|
|||||||
use criterion::{criterion_group, criterion_main, BatchSize, Bencher, BenchmarkId, Criterion};
|
use criterion::{criterion_group, criterion_main, BatchSize, Bencher, Criterion};
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
buffer::Buffer,
|
buffer::Buffer,
|
||||||
layout::Rect,
|
layout::{Alignment, Rect},
|
||||||
prelude::Alignment,
|
|
||||||
widgets::{
|
widgets::{
|
||||||
block::{Position, Title},
|
block::{Position, Title},
|
||||||
Block, Borders, Padding, Widget,
|
Block, Borders, Padding, Widget,
|
||||||
@ -13,23 +12,23 @@ use ratatui::{
|
|||||||
fn block(c: &mut Criterion) {
|
fn block(c: &mut Criterion) {
|
||||||
let mut group = c.benchmark_group("block");
|
let mut group = c.benchmark_group("block");
|
||||||
|
|
||||||
for buffer_size in [
|
for (width, height) in [
|
||||||
Rect::new(0, 0, 100, 50), // vertically split screen
|
(100, 50), // vertically split screen
|
||||||
Rect::new(0, 0, 200, 50), // 1080p fullscreen with medium font
|
(200, 50), // 1080p fullscreen with medium font
|
||||||
Rect::new(0, 0, 256, 256), // Max sized area
|
(256, 256), // Max sized area
|
||||||
] {
|
] {
|
||||||
let buffer_area = buffer_size.area();
|
let buffer_size = Rect::new(0, 0, width, height);
|
||||||
|
|
||||||
// Render an empty block
|
// Render an empty block
|
||||||
group.bench_with_input(
|
group.bench_with_input(
|
||||||
BenchmarkId::new("render_empty", buffer_area),
|
format!("render_empty/{width}x{height}"),
|
||||||
&Block::new(),
|
&Block::new(),
|
||||||
|b, block| render(b, block, buffer_size),
|
|b, block| render(b, block, buffer_size),
|
||||||
);
|
);
|
||||||
|
|
||||||
// Render with all features
|
// Render with all features
|
||||||
group.bench_with_input(
|
group.bench_with_input(
|
||||||
BenchmarkId::new("render_all_feature", buffer_area),
|
format!("render_all_feature/{width}x{height}"),
|
||||||
&Block::new()
|
&Block::new()
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
.title("test title")
|
.title("test title")
|
||||||
|
@ -55,22 +55,21 @@ pub struct Buffer {
|
|||||||
|
|
||||||
impl Buffer {
|
impl Buffer {
|
||||||
/// Returns a Buffer with all cells set to the default one
|
/// Returns a Buffer with all cells set to the default one
|
||||||
|
#[must_use]
|
||||||
pub fn empty(area: Rect) -> Self {
|
pub fn empty(area: Rect) -> Self {
|
||||||
let cell = Cell::default();
|
Self::filled(area, &Cell::default())
|
||||||
Self::filled(area, &cell)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a Buffer with all cells initialized with the attributes of the given Cell
|
/// Returns a Buffer with all cells initialized with the attributes of the given Cell
|
||||||
|
#[must_use]
|
||||||
pub fn filled(area: Rect, cell: &Cell) -> Self {
|
pub fn filled(area: Rect, cell: &Cell) -> Self {
|
||||||
let size = area.area() as usize;
|
let size = area.area() as usize;
|
||||||
let mut content = Vec::with_capacity(size);
|
let content = vec![cell.clone(); size];
|
||||||
for _ in 0..size {
|
|
||||||
content.push(cell.clone());
|
|
||||||
}
|
|
||||||
Self { area, content }
|
Self { area, content }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a Buffer containing the given lines
|
/// Returns a Buffer containing the given lines
|
||||||
|
#[must_use]
|
||||||
pub fn with_lines<'a, Iter>(lines: Iter) -> Self
|
pub fn with_lines<'a, Iter>(lines: Iter) -> Self
|
||||||
where
|
where
|
||||||
Iter: IntoIterator,
|
Iter: IntoIterator,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user