#![allow(missing_docs, unused_results)]
use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
use noyalib::{ParserConfig, Value, from_str, from_str_with_config};
use std::hint::black_box;
fn permissive_config() -> ParserConfig {
let mut cfg = ParserConfig::default();
cfg.max_alias_expansions = 100_000;
cfg.max_merge_keys = 100_000;
cfg.alias_anchor_ratio = None;
cfg
}
fn integer_keyed_mapping(count: usize) -> String {
let mut s = String::with_capacity(count * 12);
for i in 0..count {
s.push_str(&format!("{i}: v{i}\n"));
}
s
}
fn string_keyed_mapping(count: usize) -> String {
let mut s = String::with_capacity(count * 16);
for i in 0..count {
s.push_str(&format!("key{i}: value{i}\n"));
}
s
}
fn merge_heavy_mapping(count: usize) -> String {
let mut s = String::from("base: &b\n x: 1\n y: 2\n");
s.push_str("docs:\n");
for i in 0..count {
s.push_str(&format!(" m{i}:\n <<: *b\n z: {i}\n"));
}
s
}
fn bench_mapping_key_clone(c: &mut Criterion) {
let mut group = c.benchmark_group("mapping_key_clone");
for &(label, count) in &[
("small", 32usize),
("medium", 1024usize),
("large", 8192usize),
] {
let int_yaml = integer_keyed_mapping(count);
let str_yaml = string_keyed_mapping(count);
let merge_yaml = merge_heavy_mapping(count);
group.throughput(Throughput::Bytes(int_yaml.len() as u64));
group.bench_with_input(
BenchmarkId::new("integer_keys", label),
&int_yaml,
|b, y| {
b.iter(|| {
let v: Value = from_str(black_box(y)).unwrap();
black_box(v);
});
},
);
group.throughput(Throughput::Bytes(str_yaml.len() as u64));
group.bench_with_input(BenchmarkId::new("string_keys", label), &str_yaml, |b, y| {
b.iter(|| {
let v: Value = from_str(black_box(y)).unwrap();
black_box(v);
});
});
let merge_cfg = permissive_config();
group.throughput(Throughput::Bytes(merge_yaml.len() as u64));
group.bench_with_input(
BenchmarkId::new("merge_heavy", label),
&merge_yaml,
|b, y| {
b.iter(|| {
let v: Value = from_str_with_config(black_box(y), &merge_cfg).unwrap();
black_box(v);
});
},
);
}
group.finish();
}
criterion_group!(benches, bench_mapping_key_clone);
criterion_main!(benches);