alexcrichton opened PR #3345 from add-c-api-func-wrap
to main
:
This commit adds a new API to the
wasmtime::Func
type for wrapping a
C-ABI function with a well-defined signature derived from a wasm type
signature. The purpose of this API is to add the-most-optimized-we-can
path for using the C API and having wasm call host functions. Previously
when wasm called a host function it would perform these steps:
Using a trampoline, place all arguments into a
u128*
array on the
stack.Call
Func::invoke
which uses the type of the function (dynamically)
to read values from thisu128*
.Values are placed within a
Vec<Val>
after being read.The C API receives
&[Val]
and translates this to
&[wasmtime_val_t]
, iterating over each value and copying its
contents into a new vector.Then the host function is actually called.
- The above argument-shuffling steps are all performed in reverse for
the results, shipping everything throughwasmtime_val_t
andVal
.PRs such as #3319 and related attempts have made this sequence faster,
but the numbers on #3319 show that even after we get all the allocation
and such bits out of the way we're still spending quite a lot of time
shuffling arguments back-and-forth relative to theFunc::wrap
API that
Rust can use.This commit fixes the issue by eliminating all steps except 1/5 above.
Although we still place all arguments on the stack and read them out
again to call the C-defined function with it's much faster than pushing
this all through theVal
andwasmtime_val_t
machinery. This overall
gets the cost of a wasm->host call basically on-par withFunc::wrap
,
although it's still not as optimized. Benchmarking the overhead of
wasm->host calls, wherei64
returns one i64 value andmany
takes 5
i32
parameters and returns onei64
value, the numbers I get are:
Import Rust C before C after i64
1ns 40ns 7ns many
1ns 91ns 10ns This commit is a clear win over the previous implementation, but it's
even still somewhat far away from Rust. That being said I think I'm out
of ideas of how to make this better. Without open-coding much larger
portions ofwasmtime
I'm not sure how much better we can get here. The
time in C after this commit is almost entirely spent in trampolines
storing the arguments to the stack and loading them later, and at this
point I'm not sure how much more optimized we can get than that since
Rust needs to enter the picture here somehow to handle the Wasmtime
fiddly-bits of calling back into C. I'm hopeful, though, that this is
such a large improvement from before that the question of optimizing
this further in C is best left for another day.The new
Func::wrap_cabi
method is unlikely to ever get used from Rust,
but awasmtime_func_wrap
API was added to the C API to mirror
Func::wrap
where if the host function pointer has a specific ABI this
function can be called instead ofwasmtime_func_new
.<!--
Please ensure that the following steps are all taken care of before submitting
the PR.
[ ] This has been discussed in issue #..., or if not, please tell us why
here.[ ] A short description of what this does, why it is needed; if the
description becomes long, the matter should probably be discussed in an issue
first.[ ] This PR contains test cases, if meaningful.
- [ ] A reviewer from the core maintainer team has been assigned for this PR.
If you don't know who could review this, please indicate so. The list of
suggested reviewers on the right can help you.Please ensure all communication adheres to the code of conduct.
-->
alexcrichton updated PR #3345 from add-c-api-func-wrap
to main
.
alexcrichton updated PR #3345 from add-c-api-func-wrap
to main
.
alexcrichton updated PR #3345 from add-c-api-func-wrap
to main
.
alexcrichton closed without merge PR #3345.
Last updated: Nov 22 2024 at 16:03 UTC