* 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
187 lines
5.3 KiB
Rust
187 lines
5.3 KiB
Rust
use crate::{wasm_engine_t, wasmtime_error_t, wasmtime_val_t, ForeignData};
|
|
use std::cell::UnsafeCell;
|
|
use std::ffi::c_void;
|
|
use std::sync::Arc;
|
|
use wasmtime::{
|
|
AsContext, AsContextMut, InterruptHandle, Store, StoreContext, StoreContextMut, Val,
|
|
};
|
|
|
|
/// This representation of a `Store` is used to implement the `wasm.h` API.
|
|
///
|
|
/// This is stored alongside `Func` and such for `wasm.h` so each object is
|
|
/// independently owned. The usage of `Arc` here is mostly to just get it to be
|
|
/// safe to drop across multiple threads, but otherwise acquiring the `context`
|
|
/// values from this struct is considered unsafe due to it being unknown how the
|
|
/// aliasing is working on the C side of things.
|
|
///
|
|
/// The aliasing requirements are documented in the C API `wasm.h` itself (at
|
|
/// least Wasmtime's implementation).
|
|
#[derive(Clone)]
|
|
pub struct StoreRef {
|
|
store: Arc<UnsafeCell<Store<()>>>,
|
|
}
|
|
|
|
impl StoreRef {
|
|
pub unsafe fn context(&self) -> StoreContext<'_, ()> {
|
|
(*self.store.get()).as_context()
|
|
}
|
|
|
|
pub unsafe fn context_mut(&mut self) -> StoreContextMut<'_, ()> {
|
|
(*self.store.get()).as_context_mut()
|
|
}
|
|
}
|
|
|
|
#[repr(C)]
|
|
#[derive(Clone)]
|
|
pub struct wasm_store_t {
|
|
pub(crate) store: StoreRef,
|
|
}
|
|
|
|
wasmtime_c_api_macros::declare_own!(wasm_store_t);
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasm_store_new(engine: &wasm_engine_t) -> Box<wasm_store_t> {
|
|
let engine = &engine.engine;
|
|
let store = Store::new(engine, ());
|
|
Box::new(wasm_store_t {
|
|
store: StoreRef {
|
|
store: Arc::new(UnsafeCell::new(store)),
|
|
},
|
|
})
|
|
}
|
|
|
|
/// Representation of a `Store` for `wasmtime.h` This notably tries to move more
|
|
/// burden of aliasing on the caller rather than internally, allowing for a more
|
|
/// raw representation of contexts and such that requires less `unsafe` in the
|
|
/// implementation.
|
|
///
|
|
/// Note that this notably carries `StoreData` as a payload which allows storing
|
|
/// foreign data and configuring WASI as well.
|
|
#[repr(C)]
|
|
pub struct wasmtime_store_t {
|
|
pub(crate) store: Store<StoreData>,
|
|
}
|
|
|
|
pub type CStoreContext<'a> = StoreContext<'a, StoreData>;
|
|
pub type CStoreContextMut<'a> = StoreContextMut<'a, StoreData>;
|
|
|
|
pub struct StoreData {
|
|
foreign: crate::ForeignData,
|
|
#[cfg(feature = "wasi")]
|
|
pub(crate) wasi: Option<wasmtime_wasi::WasiCtx>,
|
|
|
|
/// Temporary storage for usage during a wasm->host call to store values
|
|
/// in a slice we pass to the C API.
|
|
pub hostcall_val_storage: Vec<wasmtime_val_t>,
|
|
|
|
/// Temporary storage for usage during host->wasm calls, same as above but
|
|
/// for a different direction.
|
|
pub wasm_val_storage: Vec<Val>,
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_store_delete(_: Box<wasmtime_store_t>) {}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_store_new(
|
|
engine: &wasm_engine_t,
|
|
data: *mut c_void,
|
|
finalizer: Option<extern "C" fn(*mut c_void)>,
|
|
) -> Box<wasmtime_store_t> {
|
|
Box::new(wasmtime_store_t {
|
|
store: Store::new(
|
|
&engine.engine,
|
|
StoreData {
|
|
foreign: ForeignData { data, finalizer },
|
|
#[cfg(feature = "wasi")]
|
|
wasi: None,
|
|
hostcall_val_storage: Vec::new(),
|
|
wasm_val_storage: Vec::new(),
|
|
},
|
|
),
|
|
})
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_store_context(store: &mut wasmtime_store_t) -> CStoreContextMut<'_> {
|
|
store.store.as_context_mut()
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_context_get_data(store: CStoreContext<'_>) -> *mut c_void {
|
|
store.data().foreign.data
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_context_set_data(mut store: CStoreContextMut<'_>, data: *mut c_void) {
|
|
store.data_mut().foreign.data = data;
|
|
}
|
|
|
|
#[cfg(feature = "wasi")]
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_context_set_wasi(
|
|
mut context: CStoreContextMut<'_>,
|
|
wasi: Box<crate::wasi_config_t>,
|
|
) -> Option<Box<wasmtime_error_t>> {
|
|
crate::handle_result(wasi.into_wasi_ctx(), |wasi| {
|
|
context.data_mut().wasi = Some(wasi);
|
|
})
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_context_gc(mut context: CStoreContextMut<'_>) {
|
|
context.gc();
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_context_add_fuel(
|
|
mut store: CStoreContextMut<'_>,
|
|
fuel: u64,
|
|
) -> Option<Box<wasmtime_error_t>> {
|
|
crate::handle_result(store.add_fuel(fuel), |()| {})
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_context_fuel_consumed(store: CStoreContext<'_>, fuel: &mut u64) -> bool {
|
|
match store.fuel_consumed() {
|
|
Some(amt) => {
|
|
*fuel = amt;
|
|
true
|
|
}
|
|
None => false,
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_context_consume_fuel(
|
|
mut store: CStoreContextMut<'_>,
|
|
fuel: u64,
|
|
remaining_fuel: &mut u64,
|
|
) -> Option<Box<wasmtime_error_t>> {
|
|
crate::handle_result(store.consume_fuel(fuel), |remaining| {
|
|
*remaining_fuel = remaining;
|
|
})
|
|
}
|
|
|
|
#[repr(C)]
|
|
pub struct wasmtime_interrupt_handle_t {
|
|
handle: InterruptHandle,
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_interrupt_handle_new(
|
|
store: CStoreContext<'_>,
|
|
) -> Option<Box<wasmtime_interrupt_handle_t>> {
|
|
Some(Box::new(wasmtime_interrupt_handle_t {
|
|
handle: store.interrupt_handle().ok()?,
|
|
}))
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_interrupt_handle_interrupt(handle: &wasmtime_interrupt_handle_t) {
|
|
handle.handle.interrupt();
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub extern "C" fn wasmtime_interrupt_handle_delete(_: Box<wasmtime_interrupt_handle_t>) {}
|