#![allow(missing_docs)]
use criterion::{Criterion, criterion_group, criterion_main};
use retroglyph_core::color::{Color, Style};
use retroglyph_core::grid::Grid;
use retroglyph_core::tile::Tile;
use std::hint::black_box;
fn filled(cols: u16, rows: u16, glyph: char, fg: Color) -> Grid {
let style = Style::new().fg(fg);
let mut grid = Grid::new(cols, rows);
for y in 0..rows {
for x in 0..cols {
grid.put_tile(0, (x, y), Tile::new(glyph, style));
}
}
grid
}
fn sparse_pair(cols: u16, rows: u16, pct: u32) -> (Grid, Grid) {
let old = filled(cols, rows, ' ', Color::Default);
let mut new = filled(cols, rows, ' ', Color::Default);
let changed_style = Style::new().fg(Color::Rgb { r: 255, g: 0, b: 0 });
let total = u32::from(cols) * u32::from(rows);
let changes = total * pct / 100;
let mut rng = fastrand::Rng::with_seed(42);
for _ in 0..changes {
let x = rng.u16(0..cols);
let y = rng.u16(0..rows);
new.put_tile(0, (x, y), Tile::new('X', changed_style));
}
(old, new)
}
fn bench_size(c: &mut Criterion, cols: u16, rows: u16) {
let mut group = c.benchmark_group(format!("grid_diff/{cols}x{rows}"));
group.bench_function("no_changes", |b| {
let old = filled(cols, rows, ' ', Color::Default);
let new = filled(cols, rows, ' ', Color::Default);
b.iter(|| black_box(old.diff(&new).count()));
});
for pct in [1, 5, 25] {
let (old, new) = sparse_pair(cols, rows, pct);
group.bench_function(format!("sparse_{pct}pct"), |b| {
b.iter(|| black_box(old.diff(&new).count()));
});
}
group.bench_function("full_repaint", |b| {
let old = filled(cols, rows, ' ', Color::Default);
let new = filled(
cols,
rows,
'X',
Color::Rgb {
r: 255,
g: 255,
b: 255,
},
);
b.iter(|| black_box(old.diff(&new).count()));
});
group.finish();
}
fn grid_diff(c: &mut Criterion) {
bench_size(c, 80, 24);
bench_size(c, 200, 60);
}
criterion_group!(benches, grid_diff);
criterion_main!(benches);