141 lines
3.7 KiB
Rust
141 lines
3.7 KiB
Rust
mod utils;
|
|
|
|
use criterion::{Criterion, criterion_group, criterion_main};
|
|
use nix_js::context::Context;
|
|
use std::hint::black_box;
|
|
use utils::compile;
|
|
|
|
fn bench_parse_and_downgrade(c: &mut Criterion) {
|
|
let mut group = c.benchmark_group("parse_and_downgrade");
|
|
|
|
group.bench_function("simple_expression", |b| {
|
|
b.iter(|| {
|
|
compile(black_box("1 + 1"));
|
|
})
|
|
});
|
|
|
|
group.bench_function("complex_function", |b| {
|
|
b.iter(|| {
|
|
compile(black_box(
|
|
"let fib = n: if n <= 1 then 1 else fib (n - 1) + fib (n - 2); in fib",
|
|
));
|
|
})
|
|
});
|
|
|
|
group.bench_function("large_attrset", |b| {
|
|
b.iter(|| {
|
|
compile(black_box(
|
|
"{ a = 1; b = 2; c = 3; d = 4; e = 5; f = 6; g = 7; h = 8; i = 9; j = 10; k = 11; l = 12; m = 13; n = 14; o = 15; }",
|
|
));
|
|
})
|
|
});
|
|
|
|
group.bench_function("nested_let_bindings", |b| {
|
|
b.iter(|| {
|
|
compile(black_box(
|
|
"let a = 1; b = 2; c = 3; in let d = a + b; e = b + c; in let f = d + e; in f",
|
|
));
|
|
})
|
|
});
|
|
|
|
group.finish();
|
|
}
|
|
|
|
fn bench_codegen(c: &mut Criterion) {
|
|
let mut group = c.benchmark_group("codegen");
|
|
|
|
group.bench_function("arithmetic_expression", |b| {
|
|
b.iter(|| compile(black_box("(1 + 2) * (3 - 4) / 5")))
|
|
});
|
|
|
|
group.bench_function("function_with_closure", |b| {
|
|
b.iter(|| compile(black_box("let x = 10; f = y: x + y; in f 5")))
|
|
});
|
|
|
|
group.bench_function("recursive_attrset", |b| {
|
|
b.iter(|| {
|
|
compile(black_box(
|
|
"rec { a = 1; b = a + 1; c = b + 1; d = c + 1; e = d + 1; }",
|
|
))
|
|
})
|
|
});
|
|
|
|
group.finish();
|
|
}
|
|
|
|
fn bench_full_pipeline(c: &mut Criterion) {
|
|
let mut group = c.benchmark_group("full_pipeline");
|
|
|
|
group.bench_function("simple_eval", |b| b.iter(|| compile(black_box("1 + 1"))));
|
|
|
|
group.bench_function("fibonacci_10", |b| {
|
|
b.iter(|| {
|
|
compile(black_box(
|
|
"let fib = n: if n <= 1 then 1 else fib (n - 1) + fib (n - 2); in fib 10",
|
|
))
|
|
})
|
|
});
|
|
|
|
group.bench_function("map_operation", |b| {
|
|
b.iter(|| compile(black_box("map (x: x * 2) [1 2 3 4 5 6 7 8 9 10]")))
|
|
});
|
|
|
|
group.bench_function("complex_attrset_access", |b| {
|
|
b.iter(|| {
|
|
compile(black_box(
|
|
"let attrs = { a.b.c = { d.e = 42; }; }; in attrs.a.b.c.d.e",
|
|
))
|
|
})
|
|
});
|
|
|
|
group.bench_function("with_expression", |b| {
|
|
b.iter(|| {
|
|
compile(black_box(
|
|
"let attrs = { x = 1; y = 2; z = 3; }; in with attrs; x + y + z",
|
|
))
|
|
})
|
|
});
|
|
|
|
group.finish();
|
|
}
|
|
|
|
fn bench_context_creation(c: &mut Criterion) {
|
|
c.bench_function("context_new", |b| {
|
|
b.iter(|| {
|
|
let _ = Context::new();
|
|
})
|
|
});
|
|
}
|
|
|
|
fn bench_symbol_interning(c: &mut Criterion) {
|
|
let mut group = c.benchmark_group("symbol_interning");
|
|
|
|
group.bench_function("many_unique_symbols", |b| {
|
|
b.iter(|| {
|
|
compile(black_box(
|
|
"let a1 = 1; a2 = 2; a3 = 3; a4 = 4; a5 = 5; a6 = 6; a7 = 7; a8 = 8; a9 = 9; a10 = 10; in a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10",
|
|
))
|
|
})
|
|
});
|
|
|
|
group.bench_function("repeated_symbols", |b| {
|
|
b.iter(|| {
|
|
compile(black_box(
|
|
"let x = 1; y = x; z = x; a = x; b = x; c = x; in x + y + z + a + b + c",
|
|
))
|
|
})
|
|
});
|
|
|
|
group.finish();
|
|
}
|
|
|
|
criterion_group!(
|
|
benches,
|
|
bench_parse_and_downgrade,
|
|
bench_codegen,
|
|
bench_full_pipeline,
|
|
bench_context_creation,
|
|
bench_symbol_interning
|
|
);
|
|
criterion_main!(benches);
|