Commit ea410ed
committed
Add a new API to make
The fastest way to call a WebAssembly function with Wasmtime is to use
the `TypedFunc` API and methods. This is only available to Rust code,
however, due to the usage of generics. The C API as a result is left to
only be able to use `Func::call`, which is quite slow today. While
`Func::call` has a lot of reasons that it's slow, some major
contributors are:
* Space must be allocated for the arguments/return values to call the
trampoline with. This `u128` storage is allocated on all
`Func::call`-based calls today.
* The function's type is loaded to typecheck the arguments, and this
requires taking an rwlock in the `Engine` as well as cloning out the
`FuncType` itself.
* For the C API the slice of inputs needs to be translated to a slice of
`Val`, and the results are translated from a vector of `Val` back to a
vector of `wasmtime_val_t`.
These two operations are particularly costly and the goal of this commit
is to solve these two issues. The solution implemented here is a new
structure, called `FuncStorage`, which can be created within an `Engine`
on a per-function-type basis. This storage is then used with a new API,
`Func::call_with_storage`, which removes the first two slowdowns mentioned
above. Each `FuncStorage` stores a copy of the `FuncType` it's intended
to be used with. Additionally it stores an appropriately-sized
`Vec<u128>` for storage of trampoline-encoded arguments.
The final bullet above is solved with tweaks to the
`Func::call_with_storage` API relative to `Func::call` where the
parameters/results are both iterators instead of slices.
This new API is intended to be a "power user" API for the Rust crate,
but is expected to be more commonly used with the C API since it's such
a large performance improvement to calling wasm functions.
Overall I'm not overly happy with this API. It solves a lot of the slow
`wasmtime_func_call` problem, but the APIs added here are pretty
unfortunate I think. Ideally we could solve this issue with no
additional API surface area. For example the first bullet could be
solved with a solution along the lines of #3294 where vectors are stored
in a `Store` and reused per-call. The third bullet could probably be
fixed with the same style and also changing `Func::call` to taking a
`&mut [Val]` as an argument instead of returning a boxed slice. The
second bullet though is probably one of the harder ones to fix. Each
`Func` could store it's fully-fleshed-out `FuncType`, but that's a
relatively large impact and would also likely require changing
`FuncType` to internally use `Arc<[WasmType]>` or similar. In any case
I'm hoping that this can help spur on some creativity for someone to
find a better solution to this issue.Func::call faster1 parent 36b7e81 commit ea410ed
4 files changed
+272
-97
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
172 | 172 | | |
173 | 173 | | |
174 | 174 | | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
175 | 232 | | |
176 | 233 | | |
177 | 234 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
4 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | | - | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
336 | 337 | | |
337 | 338 | | |
338 | 339 | | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
0 commit comments