use std::hint::black_box;
use criterion::{criterion_group, criterion_main, Criterion};
use datui_lib::numfmt::{
display_width, format_any_value, CellFormatter, NumberFormat, NumberFormatSettings,
};
use polars::prelude::{AnyValue, DataType};
fn coordinates() -> Vec<i64> {
(0..1024)
.map(|i| 10_000 + (i as i64 * 2_999_983) % 3_088_269_832)
.collect()
}
fn bench_integer_formatting(c: &mut Criterion) {
let values = coordinates();
let grouped = NumberFormat::preset("thousands").unwrap();
let mut group = c.benchmark_group("integer_formatting");
group.bench_function("to_string_baseline", |b| {
b.iter(|| {
let mut sink = 0usize;
for &v in &values {
sink += black_box(v.to_string()).len();
}
sink
})
});
group.bench_function("grouped", |b| {
b.iter(|| {
let mut sink = 0usize;
for &v in &values {
let mut out = String::new();
sink += grouped.write_i64(black_box(v), &mut out);
}
sink
})
});
group.bench_function("width_only", |b| {
b.iter(|| {
let mut sink = 0usize;
for &v in &values {
sink += grouped.width_i64(black_box(v));
}
sink
})
});
group.finish();
}
fn bench_frame_of_cells(c: &mut Criterion) {
let values: Vec<AnyValue> = coordinates()
.into_iter()
.take(1000)
.map(AnyValue::Int64)
.collect();
let passthrough = CellFormatter::Passthrough;
let formatted = CellFormatter::Number(NumberFormat::preset("thousands").unwrap());
let mut group = c.benchmark_group("frame_of_cells");
for (name, fmt) in [("passthrough", &passthrough), ("formatted", &formatted)] {
group.bench_function(name, |b| {
let mut scratch = String::new();
b.iter(|| {
let mut sink = 0usize;
for v in &values {
sink += format_any_value(black_box(fmt), v, &mut scratch).len();
}
sink
})
});
}
group.bench_function("width_pass", |b| {
b.iter(|| {
let mut scratch = String::new();
let mut sink = 0usize;
for v in &values {
sink += display_width(black_box(&formatted), v, &mut scratch);
}
sink
})
});
group.finish();
}
fn bench_column_resolution(c: &mut Criterion) {
let settings = NumberFormatSettings {
format: NumberFormat::preset("thousands").unwrap(),
enabled: true,
exclude: vec![
datui_lib::numfmt::Glob::new("*_id"),
datui_lib::numfmt::Glob::new("year"),
],
align_numeric_right: true,
};
let names: Vec<String> = (0..32).map(|i| format!("column_{i}")).collect();
c.bench_function("column_resolution_32_cols", |b| {
b.iter(|| {
let mut sink = 0usize;
for n in &names {
if !settings
.formatter_for(black_box(n), &DataType::Int64)
.is_passthrough()
{
sink += 1;
}
}
sink
})
});
}
criterion_group!(
benches,
bench_integer_formatting,
bench_frame_of_cells,
bench_column_resolution
);
criterion_main!(benches);