forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjit_bench.rs
More file actions
60 lines (56 loc) · 2.15 KB
/
Copy pathjit_bench.rs
File metadata and controls
60 lines (56 loc) · 2.15 KB
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
//! JIT tiering benchmarks: hot loop speedup vs interpreter, tier-up latency.
use criterion::{black_box, criterion_group, Criterion};
use nulang::effect_checker::{CapContext, CapabilityAnalyzer, EffectChecker};
use nulang::lexer::Lexer;
use nulang::parser::Parser;
use nulang::typechecker::TypeChecker;
use nulang::vm::VM;
fn compile_and_load(source: &str) -> VM {
let mut lexer = Lexer::new(source);
let tokens = lexer.lex().expect("lex failed");
let mut parser = Parser::new(tokens);
let ast = parser.parse_module().expect("parse failed");
let mut type_checker = TypeChecker::new();
type_checker.check_module(&ast).expect("typecheck failed");
let mut effect_checker = EffectChecker::new();
effect_checker
.check_module(&ast.decls)
.expect("effect check failed");
let mut cap_analyzer = CapabilityAnalyzer::new();
let cap_ctx = CapContext::new();
for decl in nulang::effect_checker::flatten_decls(&ast.decls) {
match decl {
nulang::ast::Decl::Function { body, .. } => {
cap_analyzer
.infer_cap(&cap_ctx, body)
.expect("cap check failed");
}
_ => {}
}
}
let hir = nulang::hir_lower::lower_module(&ast);
let mir = nulang::mir_lower::lower_module(&hir).expect("mir lower failed");
let code_module = nulang::mir_codegen::compile_mir(&mir).expect("codegen failed");
let mut vm = VM::new();
vm.load_module(code_module);
vm
}
fn bench_jit_hot_loop(c: &mut Criterion) {
let source = "let mut sum = 0; let mut i = 0; while i < 100000 { sum = sum + i * 3 - i / 7; i = i + 1; }; sum";
let vm_interp = compile_and_load(source);
c.bench_function("jit/hot_loop_first_run", |b| {
b.iter(|| {
let mut vm = vm_interp.clone();
black_box(vm.run().unwrap());
})
});
let mut vm_warm = compile_and_load(source);
let _ = vm_warm.run();
c.bench_function("jit/hot_loop_warm", |b| {
b.iter(|| {
let mut vm = vm_warm.clone();
black_box(vm.run().unwrap());
})
});
}
criterion_group!(benches, bench_jit_hot_loop);