Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix benchmark #4096

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 46 additions & 42 deletions benches/static_and_dynamic_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static BASIC_WAT: &str = r#"(module
i32 i32 i32 i32 i32
i32 i32 i32 i32 i32) (result i32)
(i32.add
(i32.add
(i32.add
(i32.add (i32.add (i32.add (local.get 0) (local.get 1))
(i32.add (local.get 2) (local.get 3)))
(i32.add (i32.add (local.get 4) (local.get 5))
Expand All @@ -31,20 +31,20 @@ static BASIC_WAT: &str = r#"(module
(call $multiply (local.get 1) (i32.const 2))))
)"#;

pub fn run_basic_static_function(store: &Store, compiler_name: &str, c: &mut Criterion) {
pub fn run_basic_static_function(store: &mut Store, compiler_name: &str, c: &mut Criterion) {
let module = Module::new(store, BASIC_WAT).unwrap();
let import_object = imports! {
"env" => {
"multiply" => Function::new_native(store, |a: i32, b: i32| a * b),
"multiply" => Function::new_typed(store, |a: i32, b: i32| a * b),
},
};
let instance = Instance::new(&module, &import_object).unwrap();
let instance = Instance::new(store, &module, &import_object).unwrap();
let dyn_f: &Function = instance.exports.get("add").unwrap();
let f: TypedFunction<(i32, i32), i32> = dyn_f.native().unwrap();
let f: TypedFunction<(i32, i32), i32> = dyn_f.typed(store).unwrap();

c.bench_function(&format!("basic static func {}", compiler_name), |b| {
b.iter(|| {
let result = black_box(f.call(4, 6).unwrap());
let result = black_box(f.call(store, 4, 6).unwrap());
assert_eq!(result, 10);
})
});
Expand Down Expand Up @@ -74,15 +74,16 @@ pub fn run_basic_static_function(store: &Store, compiler_name: &str, c: &mut Cri
i32,
),
i32,
> = dyn_f_many.native().unwrap();
> = dyn_f_many.typed(store).unwrap();
c.bench_function(
&format!("basic static func with many args {}", compiler_name),
|b| {
b.iter(|| {
let result = black_box(
f_many
.call(
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
store, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20,
)
.unwrap(),
);
Expand All @@ -92,20 +93,20 @@ pub fn run_basic_static_function(store: &Store, compiler_name: &str, c: &mut Cri
);
}

pub fn run_basic_dynamic_function(store: &Store, compiler_name: &str, c: &mut Criterion) {
pub fn run_basic_dynamic_function(store: &mut Store, compiler_name: &str, c: &mut Criterion) {
let module = Module::new(store, BASIC_WAT).unwrap();
let import_object = imports! {
"env" => {
"multiply" => Function::new_native(store, |a: i32, b: i32| a * b),
"multiply" => Function::new_typed(store, |a: i32, b: i32| a * b),
},
};
let instance = Instance::new(&module, &import_object).unwrap();
let instance = Instance::new(store, &module, &import_object).unwrap();

let dyn_f: &Function = instance.exports.get("add").unwrap();
c.bench_function(&format!("basic dynfunc {}", compiler_name), |b| {
b.iter(|| {
let dyn_result = black_box(dyn_f.call(&[Val::I32(4), Val::I32(6)]).unwrap());
assert_eq!(dyn_result[0], Val::I32(10));
let dyn_result = black_box(dyn_f.call(store, &[Value::I32(4), Value::I32(6)]).unwrap());
assert_eq!(dyn_result[0], Value::I32(10));
})
});

Expand All @@ -116,31 +117,34 @@ pub fn run_basic_dynamic_function(store: &Store, compiler_name: &str, c: &mut Cr
b.iter(|| {
let dyn_result = black_box(
dyn_f_many
.call(&[
Val::I32(1),
Val::I32(2),
Val::I32(3),
Val::I32(4),
Val::I32(5),
Val::I32(6),
Val::I32(7),
Val::I32(8),
Val::I32(9),
Val::I32(10),
Val::I32(11),
Val::I32(12),
Val::I32(13),
Val::I32(14),
Val::I32(15),
Val::I32(16),
Val::I32(17),
Val::I32(18),
Val::I32(19),
Val::I32(20),
])
.call(
store,
&[
Value::I32(1),
Value::I32(2),
Value::I32(3),
Value::I32(4),
Value::I32(5),
Value::I32(6),
Value::I32(7),
Value::I32(8),
Value::I32(9),
Value::I32(10),
Value::I32(11),
Value::I32(12),
Value::I32(13),
Value::I32(14),
Value::I32(15),
Value::I32(16),
Value::I32(17),
Value::I32(18),
Value::I32(19),
Value::I32(20),
],
)
.unwrap(),
);
assert_eq!(dyn_result[0], Val::I32(210));
assert_eq!(dyn_result[0], Value::I32(210));
})
},
);
Expand All @@ -150,39 +154,39 @@ fn run_static_benchmarks(_c: &mut Criterion) {
#[cfg(feature = "llvm")]
{
let mut store = Store::new(wasmer_compiler_llvm::LLVM::new());
run_basic_static_function(&store, "llvm", c);
run_basic_static_function(&mut store, "llvm", _c);
}

#[cfg(feature = "cranelift")]
{
let mut store = Store::new(wasmer_compiler_cranelift::Cranelift::new());
run_basic_static_function(&store, "cranelift", c);
run_basic_static_function(&mut store, "cranelift", _c);
}

#[cfg(feature = "singlepass")]
{
let mut store = Store::new(wasmer_compiler_singlepass::Singlepass::new());
run_basic_static_function(&store, "singlepass", c);
run_basic_static_function(&mut store, "singlepass", _c);
}
}

fn run_dynamic_benchmarks(_c: &mut Criterion) {
#[cfg(feature = "llvm")]
{
let mut store = Store::new(wasmer_compiler_llvm::LLVM::new());
run_basic_dynamic_function(&store, "llvm", c);
run_basic_dynamic_function(&mut store, "llvm", _c);
}

#[cfg(feature = "cranelift")]
{
let mut store = Store::new(wasmer_compiler_cranelift::Cranelift::new());
run_basic_dynamic_function(&store, "cranelift", c);
run_basic_dynamic_function(&mut store, "cranelift", _c);
}

#[cfg(feature = "singlepass")]
{
let mut store = Store::new(wasmer_compiler_singlepass::Singlepass::new());
run_basic_dynamic_function(&store, "singlepass", c);
run_basic_dynamic_function(&mut store, "singlepass", _c);
}
}

Expand Down