mod utils; use std::hint::black_box; use criterion::{Criterion, criterion_group, criterion_main}; use utils::eval; fn bench_arithmetic(c: &mut Criterion) { let mut group = c.benchmark_group("arithmetic"); group.bench_function("addition", |b| b.iter(|| eval(black_box("1 + 1")))); group.bench_function("subtraction", |b| b.iter(|| eval(black_box("10 - 5")))); group.bench_function("multiplication", |b| b.iter(|| eval(black_box("6 * 7")))); group.bench_function("division", |b| b.iter(|| eval(black_box("100 / 5")))); group.bench_function("complex_expression", |b| { b.iter(|| eval(black_box("(5 + 3) * (10 - 2) / 4"))) }); group.finish(); } fn bench_comparison(c: &mut Criterion) { let mut group = c.benchmark_group("comparison"); group.bench_function("equality", |b| b.iter(|| eval(black_box("42 == 42")))); group.bench_function("less_than", |b| b.iter(|| eval(black_box("5 < 10")))); group.bench_function("logical_and", |b| { b.iter(|| eval(black_box("true && false"))) }); group.bench_function("logical_or", |b| { b.iter(|| eval(black_box("true || false"))) }); group.finish(); } fn bench_function_application(c: &mut Criterion) { let mut group = c.benchmark_group("function_application"); group.bench_function("simple_identity", |b| { b.iter(|| eval(black_box("(x: x) 42"))) }); group.bench_function("curried_function", |b| { b.iter(|| eval(black_box("(x: y: x + y) 10 20"))) }); group.bench_function("nested_application", |b| { b.iter(|| eval(black_box("((x: y: z: x + y + z) 1) 2 3"))) }); group.finish(); } fn bench_let_bindings(c: &mut Criterion) { let mut group = c.benchmark_group("let_bindings"); group.bench_function("simple_let", |b| { b.iter(|| eval(black_box("let x = 5; in x + 10"))) }); group.bench_function("multiple_bindings", |b| { b.iter(|| eval(black_box("let a = 1; b = 2; c = 3; in a + b + c"))) }); group.bench_function("dependent_bindings", |b| { b.iter(|| eval(black_box("let x = 5; y = x * 2; z = y + 3; in z"))) }); group.finish(); } fn bench_attrsets(c: &mut Criterion) { let mut group = c.benchmark_group("attrsets"); group.bench_function("simple_attrset", |b| { b.iter(|| eval(black_box("{ a = 1; b = 2; }.a"))) }); group.bench_function("nested_attrset", |b| { b.iter(|| eval(black_box("{ a.b.c = 42; }.a.b.c"))) }); group.bench_function("rec_attrset", |b| { b.iter(|| eval(black_box("rec { a = 1; b = a + 1; }.b"))) }); group.bench_function("attrset_update", |b| { b.iter(|| eval(black_box("{ a = 1; } // { b = 2; }"))) }); group.finish(); } fn bench_lists(c: &mut Criterion) { let mut group = c.benchmark_group("lists"); group.bench_function("simple_list", |b| { b.iter(|| eval(black_box("[ 1 2 3 4 5 ]"))) }); group.bench_function("list_concatenation", |b| { b.iter(|| eval(black_box("[ 1 2 3 ] ++ [ 4 5 6 ]"))) }); group.bench_function("nested_list", |b| { b.iter(|| eval(black_box("[ [ 1 2 ] [ 3 4 ] [ 5 6 ] ]"))) }); group.finish(); } criterion_group!( benches, bench_arithmetic, bench_comparison, bench_function_application, bench_let_bindings, bench_attrsets, bench_lists ); criterion_main!(benches);