aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--benches/sylt_benchmark.rs56
-rw-r--r--progs/bench/fib.sy10
-rw-r--r--progs/bench/fib_iter.sy14
-rw-r--r--progs/bench/sum.sy6
-rw-r--r--src/vm.rs4
5 files changed, 54 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/progs/bench/fib.sy b/progs/bench/fib.sy
new file mode 100644
index 0000000..de68f5c
--- /dev/null
+++ b/progs/bench/fib.sy
@@ -0,0 +1,10 @@
+// The worst implementation of Fibonacci calculations
+// possible. FYI, it can be done in constant time.
+fib :: fn a:int -> int {
+ if a < 2 {
+ ret a
+ }
+ ret fib(a - 1) + fib(a - 2)
+}
+// 23 is around where things start getting slow.
+fib(23) <=> 28657
diff --git a/progs/bench/fib_iter.sy b/progs/bench/fib_iter.sy
new file mode 100644
index 0000000..c51469a
--- /dev/null
+++ b/progs/bench/fib_iter.sy
@@ -0,0 +1,14 @@
+// A Fibonacci implementation that is a little
+// less awful. But we run it 1000 times instead.
+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
+}
diff --git a/progs/bench/sum.sy b/progs/bench/sum.sy
new file mode 100644
index 0000000..bb6870f
--- /dev/null
+++ b/progs/bench/sum.sy
@@ -0,0 +1,6 @@
+// Adds the numbers 0 to 10000
+sum := 0
+for i := 0, i <= 100000, i += 1 {
+ sum += i
+}
+sum <=> 5000050000
diff --git a/src/vm.rs b/src/vm.rs
index f5c1cf0..bfb5ce4 100644
--- a/src/vm.rs
+++ b/src/vm.rs
@@ -504,8 +504,8 @@ 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) {
+ #[doc(hidden)]
+ pub fn init(&mut self, prog: &Prog) {
let block = Rc::clone(&prog.blocks[0]);
self.constants = prog.constants.clone();
self.strings = prog.strings.clone();