summaryrefslogtreecommitdiffstats
path: root/crates/rebel-lang/benches/recipe.rs
blob: ceb8520800e8c01eacbbfb530c32c437b7a7a274 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use rebel_lang::{
	scope::{Context, ModuleEntry},
	typing::Type,
};
use rebel_parse::ast;

fn main() {
	divan::main();
}

const RECIPE: &str = include_str!("../../../examples/recipes/gmp/build.recipe");

fn recipe() -> ast::Recipe<'static> {
	let tokens = rebel_parse::tokenize::token_stream(RECIPE).unwrap();
	rebel_parse::recipe::recipe(&tokens).unwrap()
}

fn context() -> Context {
	let mut ctx = Context::default();

	ctx.values
		.0
		.insert("workdir".to_owned(), ModuleEntry::Def((Type::Str, None)));
	ctx.values
		.0
		.insert("name".to_owned(), ModuleEntry::Def((Type::Str, None)));

	ctx
}

#[divan::bench]
fn validate(bencher: divan::Bencher) {
	let recipe = recipe();

	bencher.bench(|| {
		for stmt in divan::black_box(&recipe) {
			stmt.validate().unwrap();
		}
	});
}

#[divan::bench]
fn typecheck(bencher: divan::Bencher) {
	let recipe = recipe();
	let ctx = context();

	for stmt in &recipe {
		stmt.validate().unwrap();
	}

	bencher.bench(|| {
		let mut ctx = divan::black_box(ctx.clone());

		for stmt in divan::black_box(&recipe) {
			let ast::RecipeStmt::BodyStmt(stmt) = stmt else {
				// TODO: Check other statements
				continue;
			};

			ctx.record_type(stmt).unwrap();
		}
	});
}