mod utils; use std::hint::black_box; use criterion::{Criterion, criterion_group, criterion_main}; use utils::eval; fn bench_builtin_math(c: &mut Criterion) { let mut group = c.benchmark_group("builtin_math"); group.bench_function("add", |b| b.iter(|| eval(black_box("builtins.add 10 20")))); group.bench_function("sub", |b| b.iter(|| eval(black_box("builtins.sub 20 10")))); group.bench_function("mul", |b| b.iter(|| eval(black_box("builtins.mul 6 7")))); group.bench_function("div", |b| b.iter(|| eval(black_box("builtins.div 100 5")))); group.finish(); } fn bench_builtin_list(c: &mut Criterion) { let mut group = c.benchmark_group("builtin_list"); group.bench_function("length_small", |b| { b.iter(|| eval(black_box("builtins.length [1 2 3 4 5]"))) }); group.bench_function("length_large", |b| { b.iter(|| { eval(black_box( "builtins.length [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]", )) }) }); group.bench_function("head", |b| { b.iter(|| eval(black_box("builtins.head [1 2 3 4 5]"))) }); group.bench_function("tail", |b| { b.iter(|| eval(black_box("builtins.tail [1 2 3 4 5]"))) }); group.bench_function("elem_found", |b| { b.iter(|| eval(black_box("builtins.elem 3 [1 2 3 4 5]"))) }); group.bench_function("elem_not_found", |b| { b.iter(|| eval(black_box("builtins.elem 10 [1 2 3 4 5]"))) }); group.bench_function("concat_lists", |b| { b.iter(|| eval(black_box("builtins.concatLists [[1 2] [3 4] [5 6]]"))) }); group.finish(); } fn bench_builtin_map_filter(c: &mut Criterion) { let mut group = c.benchmark_group("builtin_map_filter"); group.bench_function("map_small", |b| { b.iter(|| eval(black_box("builtins.map (x: x * 2) [1 2 3 4 5]"))) }); group.bench_function("map_large", |b| { b.iter(|| { eval(black_box( "builtins.map (x: x * 2) [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]", )) }) }); group.bench_function("filter_small", |b| { b.iter(|| eval(black_box("builtins.filter (x: x > 2) [1 2 3 4 5]"))) }); group.bench_function("filter_large", |b| { b.iter(|| { eval(black_box( "builtins.filter (x: x > 10) [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]", )) }) }); group.bench_function("foldl", |b| { b.iter(|| { eval(black_box( "builtins.foldl' (acc: x: acc + x) 0 [1 2 3 4 5 6 7 8 9 10]", )) }) }); group.finish(); } fn bench_builtin_attrset(c: &mut Criterion) { let mut group = c.benchmark_group("builtin_attrset"); group.bench_function("attrNames", |b| { b.iter(|| eval(black_box("builtins.attrNames { a = 1; b = 2; c = 3; }"))) }); group.bench_function("attrValues", |b| { b.iter(|| eval(black_box("builtins.attrValues { a = 1; b = 2; c = 3; }"))) }); group.bench_function("hasAttr", |b| { b.iter(|| eval(black_box("builtins.hasAttr \"a\" { a = 1; b = 2; c = 3; }"))) }); group.bench_function("getAttr", |b| { b.iter(|| eval(black_box("builtins.getAttr \"b\" { a = 1; b = 2; c = 3; }"))) }); group.finish(); } fn bench_builtin_type_checks(c: &mut Criterion) { let mut group = c.benchmark_group("builtin_type_checks"); group.bench_function("isInt", |b| b.iter(|| eval(black_box("builtins.isInt 42")))); group.bench_function("isList", |b| { b.iter(|| eval(black_box("builtins.isList [1 2 3]"))) }); group.bench_function("isAttrs", |b| { b.iter(|| eval(black_box("builtins.isAttrs { a = 1; }"))) }); group.bench_function("isFunction", |b| { b.iter(|| eval(black_box("builtins.isFunction (x: x)"))) }); group.bench_function("typeOf", |b| { b.iter(|| eval(black_box("builtins.typeOf 42"))) }); group.finish(); } fn bench_free_globals(c: &mut Criterion) { let mut group = c.benchmark_group("free_globals"); group.bench_function("map", |b| { b.iter(|| eval(black_box("map (x: x * 2) [1 2 3 4 5]"))) }); group.bench_function("isNull", |b| b.iter(|| eval(black_box("isNull null")))); group.bench_function("toString", |b| b.iter(|| eval(black_box("toString 42")))); group.finish(); } criterion_group!( benches, bench_builtin_math, bench_builtin_list, bench_builtin_map_filter, bench_builtin_attrset, bench_builtin_type_checks, bench_free_globals ); criterion_main!(benches);