Skip to content

Instantly share code, notes, and snippets.

@alexcrichton
Created September 9, 2021 15:52
Show Gist options
  • Save alexcrichton/59590c6509790cace6bef5b9d15ba69d to your computer and use it in GitHub Desktop.
Save alexcrichton/59590c6509790cace6bef5b9d15ba69d to your computer and use it in GitHub Desktop.
use anyhow::Result;
use wasmtime::*;
const N: u32 = 10_000_000;
fn main() -> Result<()> {
let engine = Engine::default();
let mut linker = Linker::new(&engine);
linker.func_wrap("", "typed", || 0i64)?;
linker.func_wrap("", "many", |_: i32, _: i32, _: i32, _: i32, _: i32| 0i32)?;
let ty = FuncType::new([], [ValType::I64]);
linker.func_new("", "untyped", ty, |_caller, _params, results| {
results[0] = Val::I64(0);
Ok(())
})?;
let module = Module::new(
&engine,
r#"
(module
(import "" "typed" (func $typed (result i64)))
(import "" "untyped" (func $untyped (result i64)))
(import "" "many" (func $many (param i32 i32 i32 i32 i32) (result i32)))
(func (export "nop"))
(func (export "i64_typed") (result i64) call $typed)
(func (export "i64_untyped") (result i64) call $untyped)
(func (export "many")
i32.const 0
i32.const 0
i32.const 0
i32.const 0
i32.const 0
call $many
drop)
)
"#,
)?;
let mut store = Store::<()>::new(&engine, ());
let instance = linker.instantiate(&mut store, &module)?;
let untyped_nop = instance.get_func(&mut store, "nop").unwrap();
let untyped_i64_typed = instance.get_func(&mut store, "i64_typed").unwrap();
let untyped_i64_untyped = instance.get_func(&mut store, "i64_untyped").unwrap();
let typed_nop = instance.get_typed_func::<(), (), _>(&mut store, "nop")?;
let typed_i64_typed = instance.get_typed_func::<(), i64, _>(&mut store, "i64_typed")?;
let typed_i64_untyped = instance.get_typed_func::<(), i64, _>(&mut store, "i64_untyped")?;
let many = instance.get_typed_func::<(), (), _>(&mut store, "many")?;
let start = std::time::Instant::now();
for _ in 0..N {
typed_nop.call(&mut store, ())?;
}
println!("typed_nop - {:?} / iter", start.elapsed() / N);
let start = std::time::Instant::now();
for _ in 0..N {
typed_i64_typed.call(&mut store, ())?;
}
println!("typed_i64_typed - {:?} / iter", start.elapsed() / N);
let start = std::time::Instant::now();
for _ in 0..N {
typed_i64_untyped.call(&mut store, ())?;
}
println!("typed_i64_untyped - {:?} / iter", start.elapsed() / N);
let start = std::time::Instant::now();
for _ in 0..N {
untyped_nop.call(&mut store, &[], &mut [])?;
}
println!("untyped_nop - {:?} / iter", start.elapsed() / N);
let mut results = [Val::I32(0)];
let start = std::time::Instant::now();
for _ in 0..N {
untyped_i64_typed.call(&mut store, &[], &mut results)?;
}
println!("untyped_i64_typed - {:?} / iter", start.elapsed() / N);
let start = std::time::Instant::now();
for _ in 0..N {
untyped_i64_untyped.call(&mut store, &[], &mut results)?;
}
println!("untyped_i64_untyped - {:?} / iter", start.elapsed() / N);
let start = std::time::Instant::now();
for _ in 0..N {
many.call(&mut store, ())?;
}
println!("many - {:?} / iter", start.elapsed() / N);
// let mut nop_storage = FuncStorage::new(&engine, FuncType::new([], []));
// let mut i64_storage = FuncStorage::new(&engine, FuncType::new([], [ValType::I64]));
// let start = std::time::Instant::now();
// for _ in 0..N {
// untyped_nop.call_with_storage(&mut store, &mut nop_storage, None)?;
// }
// println!("s - untyped_nop - {:?} / iter", start.elapsed() / N);
// let start = std::time::Instant::now();
// for _ in 0..N {
// untyped_i64_typed.call_with_storage(&mut store, &mut i64_storage, None)?;
// }
// println!("s - untyped_i64_typed - {:?} / iter", start.elapsed() / N);
// let start = std::time::Instant::now();
// for _ in 0..N {
// untyped_i64_untyped.call_with_storage(&mut store, &mut i64_storage, None)?;
// }
// println!("s - untyped_i64_untyped - {:?} / iter", start.elapsed() / N);
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment