Skip to content

Commit

Permalink
Add Fibonacci benchmarks (wasmi-labs#366)
Browse files Browse the repository at this point in the history
* add fibonacci benchmarks

* fix fib benchmarks

* apply rustfmt
  • Loading branch information
Robbepop authored Feb 19, 2022
1 parent 86cb7e5 commit 29110ed
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
31 changes: 30 additions & 1 deletion benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ criterion_group!(
bench_execute_recursive_trap_v0,
bench_execute_recursive_trap_v1,
bench_execute_host_calls_v0,
bench_execute_host_calls_v1
bench_execute_host_calls_v1,
bench_execute_fibonacci_recursive_v0,
bench_execute_fibonacci_recursive_v1,
);

criterion_main!(bench_compile_and_validate, bench_instantiate, bench_execute);
Expand Down Expand Up @@ -650,3 +652,30 @@ fn bench_execute_host_calls_v1(c: &mut Criterion) {
})
});
}

fn bench_execute_fibonacci_recursive_v0(c: &mut Criterion) {
let instance = load_instance_from_wat_v0(include_bytes!("wat/fibonacci.wat"));
c.bench_function("execute/fib_recursive/v0", |b| {
b.iter(|| {
let result =
instance.invoke_export("fib_recursive", &[Value::I32(25)], &mut v0::NopExternals);
assert_matches!(result, Ok(Some(Value::I32(75025))));
})
});
}

fn bench_execute_fibonacci_recursive_v1(c: &mut Criterion) {
let (mut store, instance) = load_instance_from_wat_v1(include_bytes!("wat/fibonacci.wat"));
let bench_call = instance
.get_export(&store, "fib_recursive")
.and_then(v1::Extern::into_func)
.unwrap();
let mut result = [Value::I32(0)];
c.bench_function("execute/fib_recursive/v1", |b| {
b.iter(|| {
let result = bench_call.call(&mut store, &[Value::I32(25)], &mut result);
assert_matches!(result, Ok(_));
});
assert_eq!(result, [Value::I32(75025)]);
});
}
58 changes: 58 additions & 0 deletions benches/wat/fibonacci.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
(module
(func $fib_recursive (param $N i32) (result i32)
(if
(i32.eq (local.get $N) (i32.const 0))
(then (return (i32.const 0)))
)
(if
(i32.eq (local.get $N) (i32.const 1))
(then (return (i32.const 1)))
)
(return
(i32.add
(call $fib_recursive
(i32.sub (local.get $N) (i32.const 1))
)
(call $fib_recursive
(i32.sub (local.get $N) (i32.const 2))
)
)
)
)

(func $fib_iterative (param $N i32) (result i32)
(local $n1 i32)
(local $n2 i32)
(local $tmp i32)
(local $i i32)
(local.set $n1 (i32.const 1))
(local.set $n2 (i32.const 1))
(local.set $i (i32.const 2))
;; return 0 for N <= 0
(if
(i32.le_s (local.get $N) (i32.const 0))
(then (return (i32.const 0)))
)
;;since we normally return n2, handle n=1 case specially
(if
(i32.le_s (local.get $N) (i32.const 2))
(then (return (i32.const 1)))
)
(loop $again
(if
(i32.lt_s (local.get $i) (local.get $N))
(then
(local.set $tmp (i32.add (local.get $n1) (local.get $n2)))
(local.set $n1 (local.get $n2))
(local.set $n2 (local.get $tmp))
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br $again)
)
)
)
(local.get $n2)
)

(export "fib_iterative" (func $fib_iterative))
(export "fib_recursive" (func $fib_recursive))
)

0 comments on commit 29110ed

Please sign in to comment.