use std::collections::BTreeMap;
use std::hint::black_box;
use std::path::PathBuf;
use std::rc::Rc;
use criterion::{criterion_group, criterion_main, Criterion};
use harn_hostlib::{ast::AstCapability, BuiltinRegistry, HostlibCapability};
use harn_vm::VmValue;
fn ast_registry() -> BuiltinRegistry {
let mut registry = BuiltinRegistry::new();
AstCapability.register_builtins(&mut registry);
registry
}
fn dict(pairs: &[(&str, VmValue)]) -> VmValue {
let mut map: BTreeMap<String, VmValue> = BTreeMap::new();
for (k, v) in pairs {
map.insert((*k).into(), v.clone());
}
VmValue::Dict(Rc::new(map))
}
fn fixture_path(rel: &str) -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/ast")
.join(rel)
}
fn bench_parse_file_warm(c: &mut Criterion) {
let registry = ast_registry();
let path = fixture_path("rust/source.rs");
let payload = dict(&[(
"path",
VmValue::String(Rc::from(path.to_string_lossy().as_ref())),
)]);
let entry = registry
.find("hostlib_ast_parse_file")
.expect("hostlib_ast_parse_file builtin registered");
let _ = (entry.handler)(std::slice::from_ref(&payload)).expect("warmup parse_file succeeds");
c.bench_function("hostlib_ast_parse_file/rust_source.rs (warm)", |b| {
b.iter(|| {
let result = (entry.handler)(std::slice::from_ref(black_box(&payload)))
.expect("parse_file succeeds");
black_box(result);
})
});
}
criterion_group!(benches, bench_parse_file_warm);
criterion_main!(benches);