Skip to content

Commit

Permalink
Add tail recursive fibonacci benchmark (#697)
Browse files Browse the repository at this point in the history
* add tail recursive fibonacci benchmark

* apply rustfmt

* rename wat label
  • Loading branch information
Robbepop committed Feb 28, 2023
1 parent 8b35b2b commit 01423af
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
21 changes: 6 additions & 15 deletions crates/wasmi/benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,12 +902,12 @@ fn bench_execute_fibonacci(c: &mut Criterion) {
}

const FIBONACCI_REC_N: i64 = 25;
const FIBONACCI_REC_RESULT: i64 = fib(FIBONACCI_REC_N);
const FIBONACCI_TAIL_N: i64 = 50_000;
const FIBONACCI_INC_N: i64 = 100_000;
const FIBONACCI_INC_RESULT: i64 = fib(FIBONACCI_INC_N);
let (mut store, instance) = load_instance_from_wat(include_bytes!("wat/fibonacci.wat"));
let mut bench_fib = |bench_id: &str, func_name: &str, input: i64, expected: i64| {
let mut bench_fib = |bench_id: &str, func_name: &str, input: i64| {
c.bench_function(bench_id, |b| {
let expected = fib(input);
let fib = instance
.get_export(&store, func_name)
.and_then(Extern::into_func)
Expand All @@ -919,18 +919,9 @@ fn bench_execute_fibonacci(c: &mut Criterion) {
});
});
};
bench_fib(
"execute/fib_recursive",
"fib_recursive",
FIBONACCI_REC_N,
FIBONACCI_REC_RESULT,
);
bench_fib(
"execute/fib_iterative",
"fib_iterative",
FIBONACCI_INC_N,
FIBONACCI_INC_RESULT,
);
bench_fib("execute/fibonacci_rec", "fibonacci_rec", FIBONACCI_REC_N);
bench_fib("execute/fibonacci_tail", "fibonacci_tail", FIBONACCI_TAIL_N);
bench_fib("execute/fibonacci_iter", "fibonacci_iter", FIBONACCI_INC_N);
}

fn bench_execute_memory_sum(c: &mut Criterion) {
Expand Down
30 changes: 26 additions & 4 deletions crates/wasmi/benches/wat/fibonacci.wat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(module
(func $fib_recursive (export "fib_recursive") (param $N i64) (result i64)
(func $fib_recursive (export "fibonacci_rec") (param $N i64) (result i64)
(if
(i64.le_s (local.get $N) (i64.const 1))
(then (return (local.get $N)))
Expand All @@ -16,7 +16,29 @@
)
)

(func $fib_iterative (export "fib_iterative") (param $N i64) (result i64)
(func $fib_tail_recursive (param $N i64) (param $a i64) (param $b i64) (result i64)
(if (i64.eqz (local.get $N))
(then
(return (local.get $a))
)
)
(if (i64.eq (local.get $N) (i64.const 1))
(then
(return (local.get $b))
)
)
(call $fib_tail_recursive
(i64.sub (local.get $N) (i64.const 1))
(local.get $b)
(i64.add (local.get $a) (local.get $b))
)
)

(func (export "fibonacci_tail") (param $N i64) (result i64)
(call $fib_tail_recursive (local.get $N) (i64.const 0) (i64.const 1))
)

(func $fib_iterative (export "fibonacci_iter") (param $N i64) (result i64)
(local $n1 i64)
(local $n2 i64)
(local $tmp i64)
Expand All @@ -30,15 +52,15 @@
(local.set $n2 (i64.const 1))
(local.set $i (i64.const 2))
;;since we normally return n2, handle n=1 case specially
(loop $again
(loop $continue
(if
(i64.lt_s (local.get $i) (local.get $N))
(then
(local.set $tmp (i64.add (local.get $n1) (local.get $n2)))
(local.set $n1 (local.get $n2))
(local.set $n2 (local.get $tmp))
(local.set $i (i64.add (local.get $i) (i64.const 1)))
(br $again)
(br $continue)
)
)
)
Expand Down

0 comments on commit 01423af

Please sign in to comment.