* Optimize `Func::call` and its C API This commit is an alternative to #3298 which achieves effectively the same goal of optimizing the `Func::call` API as well as its C API sibling of `wasmtime_func_call`. The strategy taken here is different than #3298 though where a new API isn't created, rather a small tweak to an existing API is done. Specifically this commit handles the major sources of slowness with `Func::call` with: * Looking up the type of a function, to typecheck the arguments with and use to guide how the results should be loaded, no longer hits the rwlock in the `Engine` but instead each `Func` contains its own `FuncType`. This can be an unnecessary allocation for funcs not used with `Func::call`, so this is a downside of this implementation relative to #3298. A mitigating factor, though, is that instance exports are loaded lazily into the `Store` and in theory not too many funcs are active in the store as `Func` objects. * Temporary storage is amortized with a long-lived `Vec` in the `Store` rather than allocating a new vector on each call. This is basically the same strategy as #3294 only applied to different types in different places. Specifically `wasmtime::Store` now retains a `Vec<u128>` for `Func::call`, and the C API retains a `Vec<Val>` for calling `Func::call`. * Finally, an API breaking change is made to `Func::call` and its type signature (as well as `Func::call_async`). Instead of returning `Box<[Val]>` as it did before this function now takes a `results: &mut [Val]` parameter. This allows the caller to manage the allocation and we can amortize-remove it in `wasmtime_func_call` by using space after the parameters in the `Vec<Val>` we're passing in. This change is naturally a breaking change and we'll want to consider it carefully, but mitigating factors are that most embeddings are likely using `TypedFunc::call` instead and this signature taking a mutable slice better aligns with `Func::new` which receives a mutable slice for the results. Overall this change, in the benchmark of "call a nop function from the C API" is not quite as good as #3298. It's still a bit slower, on the order of 15ns, because there's lots of capacity checks around vectors and the type checks are slightly less optimized than before. Overall though this is still significantly better than today because allocations and the rwlock to acquire the type information are both avoided. I personally feel that this change is the best to do because it has less of an API impact than #3298. * Rebase issues
156 lines
4.4 KiB
Rust
156 lines
4.4 KiB
Rust
use super::ref_types_module;
|
|
use std::sync::atomic::{AtomicBool, Ordering::SeqCst};
|
|
use std::sync::Arc;
|
|
use wasmtime::*;
|
|
|
|
#[test]
|
|
fn pass_funcref_in_and_out_of_wasm() -> anyhow::Result<()> {
|
|
let (mut store, module) = ref_types_module(
|
|
r#"
|
|
(module
|
|
(func (export "func") (param funcref) (result funcref)
|
|
local.get 0
|
|
)
|
|
)
|
|
"#,
|
|
)?;
|
|
|
|
let instance = Instance::new(&mut store, &module, &[])?;
|
|
let func = instance.get_func(&mut store, "func").unwrap();
|
|
|
|
// Pass in a non-null funcref.
|
|
{
|
|
let mut results = [Val::I32(0)];
|
|
func.call(
|
|
&mut store,
|
|
&[Val::FuncRef(Some(func.clone()))],
|
|
&mut results,
|
|
)?;
|
|
|
|
// Can't compare `Func` for equality, so this is the best we can do here.
|
|
let result_func = results[0].unwrap_funcref().unwrap();
|
|
assert_eq!(func.ty(&store), result_func.ty(&store));
|
|
}
|
|
|
|
// Pass in a null funcref.
|
|
{
|
|
let mut results = [Val::I32(0)];
|
|
func.call(&mut store, &[Val::FuncRef(None)], &mut results)?;
|
|
let result_func = results[0].unwrap_funcref();
|
|
assert!(result_func.is_none());
|
|
}
|
|
|
|
// Pass in a `funcref` from another instance.
|
|
{
|
|
let other_instance = Instance::new(&mut store, &module, &[])?;
|
|
let other_instance_func = other_instance.get_func(&mut store, "func").unwrap();
|
|
|
|
let mut results = [Val::I32(0)];
|
|
func.call(
|
|
&mut store,
|
|
&[Val::FuncRef(Some(other_instance_func.clone()))],
|
|
&mut results,
|
|
)?;
|
|
assert_eq!(results.len(), 1);
|
|
|
|
// Can't compare `Func` for equality, so this is the best we can do here.
|
|
let result_func = results[0].unwrap_funcref().unwrap();
|
|
assert_eq!(other_instance_func.ty(&store), result_func.ty(&store));
|
|
}
|
|
|
|
// Passing in a `funcref` from another store fails.
|
|
{
|
|
let (mut other_store, other_module) = ref_types_module(r#"(module (func (export "f")))"#)?;
|
|
let other_store_instance = Instance::new(&mut other_store, &other_module, &[])?;
|
|
let f = other_store_instance
|
|
.get_func(&mut other_store, "f")
|
|
.unwrap();
|
|
|
|
assert!(func
|
|
.call(&mut store, &[Val::FuncRef(Some(f))], &mut [Val::I32(0)])
|
|
.is_err());
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn receive_null_funcref_from_wasm() -> anyhow::Result<()> {
|
|
let (mut store, module) = ref_types_module(
|
|
r#"
|
|
(module
|
|
(func (export "get-null") (result funcref)
|
|
ref.null func
|
|
)
|
|
)
|
|
"#,
|
|
)?;
|
|
|
|
let instance = Instance::new(&mut store, &module, &[])?;
|
|
let get_null = instance.get_func(&mut store, "get-null").unwrap();
|
|
|
|
let mut results = [Val::I32(0)];
|
|
get_null.call(&mut store, &[], &mut results)?;
|
|
let result_func = results[0].unwrap_funcref();
|
|
assert!(result_func.is_none());
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn wrong_store() -> anyhow::Result<()> {
|
|
let dropped = Arc::new(AtomicBool::new(false));
|
|
{
|
|
let mut store1 = Store::<()>::default();
|
|
let mut store2 = Store::<()>::default();
|
|
|
|
let set = SetOnDrop(dropped.clone());
|
|
let f1 = Func::wrap(&mut store1, move || drop(&set));
|
|
let f2 = Func::wrap(&mut store2, move || Some(f1.clone()));
|
|
assert!(f2.call(&mut store2, &[], &mut []).is_err());
|
|
}
|
|
assert!(dropped.load(SeqCst));
|
|
|
|
return Ok(());
|
|
|
|
struct SetOnDrop(Arc<AtomicBool>);
|
|
|
|
impl Drop for SetOnDrop {
|
|
fn drop(&mut self) {
|
|
self.0.store(true, SeqCst);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn func_new_returns_wrong_store() -> anyhow::Result<()> {
|
|
let dropped = Arc::new(AtomicBool::new(false));
|
|
{
|
|
let mut store1 = Store::<()>::default();
|
|
let mut store2 = Store::<()>::default();
|
|
|
|
let set = SetOnDrop(dropped.clone());
|
|
let f1 = Func::wrap(&mut store1, move || drop(&set));
|
|
let f2 = Func::new(
|
|
&mut store2,
|
|
FuncType::new(None, Some(ValType::FuncRef)),
|
|
move |_, _, results| {
|
|
results[0] = f1.clone().into();
|
|
Ok(())
|
|
},
|
|
);
|
|
assert!(f2.call(&mut store2, &[], &mut [Val::I32(0)]).is_err());
|
|
}
|
|
assert!(dropped.load(SeqCst));
|
|
|
|
return Ok(());
|
|
|
|
struct SetOnDrop(Arc<AtomicBool>);
|
|
|
|
impl Drop for SetOnDrop {
|
|
fn drop(&mut self) {
|
|
self.0.store(true, SeqCst);
|
|
}
|
|
}
|
|
}
|