Optimize Func::call and its C API (#3319)
* 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
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use wasmtime::{Config, Engine, Linker, Module, Store};
|
||||
use wasmtime::{Config, Engine, Linker, Module, Store, Val};
|
||||
|
||||
wiggle::from_witx!({
|
||||
witx: ["$CARGO_MANIFEST_DIR/tests/atoms.witx"],
|
||||
@@ -42,14 +42,14 @@ async fn test_sync_host_func() {
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let results = shim_inst
|
||||
let mut results = [Val::I32(0)];
|
||||
shim_inst
|
||||
.get_func(&mut store, "int_float_args_shim")
|
||||
.unwrap()
|
||||
.call_async(&mut store, &[0i32.into(), 123.45f32.into()])
|
||||
.call_async(&mut store, &[0i32.into(), 123.45f32.into()], &mut results)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(results.len(), 1, "one return value");
|
||||
assert_eq!(
|
||||
results[0].unwrap_i32(),
|
||||
types::Errno::Ok as i32,
|
||||
@@ -72,14 +72,18 @@ async fn test_async_host_func() {
|
||||
let input: i32 = 123;
|
||||
let result_location: i32 = 0;
|
||||
|
||||
let results = shim_inst
|
||||
let mut results = [Val::I32(0)];
|
||||
shim_inst
|
||||
.get_func(&mut store, "double_int_return_float_shim")
|
||||
.unwrap()
|
||||
.call_async(&mut store, &[input.into(), result_location.into()])
|
||||
.call_async(
|
||||
&mut store,
|
||||
&[input.into(), result_location.into()],
|
||||
&mut results,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(results.len(), 1, "one return value");
|
||||
assert_eq!(
|
||||
results[0].unwrap_i32(),
|
||||
types::Errno::Ok as i32,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use wasmtime::{Engine, Linker, Module, Store};
|
||||
use wasmtime::{Engine, Linker, Module, Store, Val};
|
||||
|
||||
// from_witx invocation says the func is async. This context doesn't support async!
|
||||
wiggle::from_witx!({
|
||||
@@ -49,13 +49,13 @@ fn test_sync_host_func() {
|
||||
let shim_mod = shim_module(&engine);
|
||||
let shim_inst = linker.instantiate(&mut store, &shim_mod).unwrap();
|
||||
|
||||
let results = shim_inst
|
||||
let mut results = [Val::I32(0)];
|
||||
shim_inst
|
||||
.get_func(&mut store, "int_float_args_shim")
|
||||
.unwrap()
|
||||
.call(&mut store, &[0i32.into(), 123.45f32.into()])
|
||||
.call(&mut store, &[0i32.into(), 123.45f32.into()], &mut results)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(results.len(), 1, "one return value");
|
||||
assert_eq!(
|
||||
results[0].unwrap_i32(),
|
||||
types::Errno::Ok as i32,
|
||||
@@ -76,13 +76,17 @@ fn test_async_host_func() {
|
||||
let input: i32 = 123;
|
||||
let result_location: i32 = 0;
|
||||
|
||||
let results = shim_inst
|
||||
let mut results = [Val::I32(0)];
|
||||
shim_inst
|
||||
.get_func(&mut store, "double_int_return_float_shim")
|
||||
.unwrap()
|
||||
.call(&mut store, &[input.into(), result_location.into()])
|
||||
.call(
|
||||
&mut store,
|
||||
&[input.into(), result_location.into()],
|
||||
&mut results,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(results.len(), 1, "one return value");
|
||||
assert_eq!(
|
||||
results[0].unwrap_i32(),
|
||||
types::Errno::Ok as i32,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use wasmtime::{Engine, Linker, Module, Store};
|
||||
use wasmtime::{Engine, Linker, Module, Store, Val};
|
||||
|
||||
wiggle::from_witx!({
|
||||
witx: ["$CARGO_MANIFEST_DIR/tests/atoms.witx"],
|
||||
@@ -55,13 +55,13 @@ fn test_sync_host_func() {
|
||||
let shim_mod = shim_module(&engine);
|
||||
let shim_inst = linker.instantiate(&mut store, &shim_mod).unwrap();
|
||||
|
||||
let results = shim_inst
|
||||
let mut results = [Val::I32(0)];
|
||||
shim_inst
|
||||
.get_func(&mut store, "int_float_args_shim")
|
||||
.unwrap()
|
||||
.call(&mut store, &[0i32.into(), 123.45f32.into()])
|
||||
.call(&mut store, &[0i32.into(), 123.45f32.into()], &mut results)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(results.len(), 1, "one return value");
|
||||
assert_eq!(
|
||||
results[0].unwrap_i32(),
|
||||
types::Errno::Ok as i32,
|
||||
@@ -82,13 +82,17 @@ fn test_async_host_func() {
|
||||
let input: i32 = 123;
|
||||
let result_location: i32 = 0;
|
||||
|
||||
let results = shim_inst
|
||||
let mut results = [Val::I32(0)];
|
||||
shim_inst
|
||||
.get_func(&mut store, "double_int_return_float_shim")
|
||||
.unwrap()
|
||||
.call(&mut store, &[input.into(), result_location.into()])
|
||||
.call(
|
||||
&mut store,
|
||||
&[input.into(), result_location.into()],
|
||||
&mut results,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(results.len(), 1, "one return value");
|
||||
assert_eq!(
|
||||
results[0].unwrap_i32(),
|
||||
types::Errno::Ok as i32,
|
||||
@@ -121,7 +125,11 @@ fn test_async_host_func_pending() {
|
||||
let trap = shim_inst
|
||||
.get_func(&mut store, "double_int_return_float_shim")
|
||||
.unwrap()
|
||||
.call(&mut store, &[input.into(), result_location.into()])
|
||||
.call(
|
||||
&mut store,
|
||||
&[input.into(), result_location.into()],
|
||||
&mut [Val::I32(0)],
|
||||
)
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
format!("{}", trap).contains("Cannot wait on pending future"),
|
||||
|
||||
Reference in New Issue
Block a user