From 6b9b43565c66e1da12d775ffd2881a9ea461d604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edvard=20Th=C3=B6rnros?= Date: Wed, 17 Feb 2021 23:46:20 +0100 Subject: bring back the benching --- benches/sylt_benchmark.rs | 56 +++++++++++++++++++---------------------------- src/vm.rs | 5 +++-- 2 files changed, 25 insertions(+), 36 deletions(-) diff --git a/benches/sylt_benchmark.rs b/benches/sylt_benchmark.rs index a68a762..e77a916 100644 --- a/benches/sylt_benchmark.rs +++ b/benches/sylt_benchmark.rs @@ -1,42 +1,30 @@ use criterion::{criterion_group, criterion_main, Criterion}; +use std::fs; use std::path::Path; -pub fn fib_50(c: &mut Criterion) { - let prog = -" -j := 0 -for , j < 1000, j = j + 1 { - a := 0 - b := 1 - - for i := 0, i < 50, i = i + 1 { - c := a - a = b - b = c + b - } - a <=> 12586269025 -} -"; - let compiled = sylt::compiler::compile("main", Path::new("prog"), sylt::tokenizer::string_to_tokens(prog)).unwrap(); - c.bench_function("fib 50", |b| b.iter(|| sylt::vm::run_block(&compiled).unwrap())); +macro_rules! bench_file { + ( $name:ident ) => { + pub fn $name(c: &mut Criterion) { + let prog = fs::read_to_string(Path::new(&format!("progs/bench/{}.sy", stringify!($name)))) + .unwrap(); + c.bench_function(stringify!($name), |b| { + b.iter(|| { + sylt::run_string(&prog, false, Vec::new()).unwrap(); + }) + }); + } + }; } -pub fn fib_90(c: &mut Criterion) { - let prog = -" -a := 0 -b := 1 +macro_rules! bench { + ( [ $( $name:ident ),* ] ) => { + $(bench_file!($name);)* -for i := 0, i < 90, i = i + 1 { - c := a - a = b - b = c + b -} -a <=> 2880067194370816120 -"; - let compiled = sylt::compiler::compile("main", Path::new("prog"), sylt::tokenizer::string_to_tokens(prog)).unwrap(); - c.bench_function("fib 90", |b| b.iter(|| sylt::vm::run_block(&compiled).unwrap())); + criterion_group!(benches, $( $name ),* ); + criterion_main!(benches); + } } -criterion_group!(benches, fib_50, fib_90); -criterion_main!(benches); +// List of all benchmarks to run +bench!([fib, fib_iter, sum]); + diff --git a/src/vm.rs b/src/vm.rs index c4d72a1..4ed19ff 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -480,8 +480,9 @@ impl VM { self.frame().block.borrow().ops[self.frame().ip]); } - // Initalizes the VM for running. Run cannot be called before this. - pub(crate) fn init(&mut self, prog: &Prog) { + /// Initalizes the VM for running. Run cannot be called before this. + /// You do not have to call this. + pub fn init(&mut self, prog: &Prog) { let block = Rc::clone(&prog.blocks[0]); self.blobs = prog.blobs.clone(); self.constants = prog.constants.clone(); -- cgit v1.2.1