Optimize calling a WebAssembly function (#2757)
This commit implements a few optimizations, mainly inlining, that should improve the performance of calling a WebAssembly function. This code path can be quite hot depending on the embedding case and we hadn't really put much effort into optimizing the nitty gritty. The predominant optimization here is adding `#[inline]` to trivial functions so performance is improved without having to compile with LTO. Another optimization is to call `lazy_per_thread_init` when traps are initialized per-thread (when a `Store` is created) rather than each time a function is called. The next optimization is to change the unwind reason in the `CallThreadState` to `MaybeUninit` to avoid extra checks in the default case about whether we need to drop its variants (since in the happy path we never need to drop it). The final optimization is to optimize out a few checks when `async` support is disabled for a small speed boost. In a small benchmark where wasmtime calls a simple wasm function my macOS computer dropped from 110ns to 86ns overhead, a 20% decrease. The macOS overhead is still largely dominated by the global lock acquisition and hash table management for traps right now, but I suspect the Linux overhead is much better (should be on the order of ~30 or so ns). We still have a long way to go to compete with SpiderMonkey which, in testing, seem to have ~6ns overhead in calling the same wasm function on my computer.
This commit is contained in:
@@ -56,6 +56,7 @@ impl Engine {
|
||||
}
|
||||
|
||||
/// Returns the configuration settings that this engine is using.
|
||||
#[inline]
|
||||
pub fn config(&self) -> &Config {
|
||||
&self.inner.config
|
||||
}
|
||||
|
||||
@@ -804,7 +804,7 @@ impl Func {
|
||||
/// initiates a panic.
|
||||
pub fn call(&self, params: &[Val]) -> Result<Box<[Val]>> {
|
||||
assert!(
|
||||
!self.store().async_support(),
|
||||
!cfg!(feature = "async") || !self.store().async_support(),
|
||||
"must use `call_async` when async support is enabled on the config",
|
||||
);
|
||||
self._call(params)
|
||||
@@ -926,6 +926,7 @@ impl Func {
|
||||
}
|
||||
|
||||
/// Get a reference to this function's store.
|
||||
#[inline]
|
||||
pub fn store(&self) -> &Store {
|
||||
&self.instance.store
|
||||
}
|
||||
@@ -1414,6 +1415,7 @@ impl Caller<'_> {
|
||||
}
|
||||
|
||||
/// Get a reference to the caller's store.
|
||||
#[inline]
|
||||
pub fn store(&self) -> &Store {
|
||||
self.store
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ where
|
||||
/// connected to an asynchronous store.
|
||||
pub fn call(&self, params: Params) -> Result<Results, Trap> {
|
||||
assert!(
|
||||
!self.func.store().async_support(),
|
||||
!cfg!(feature = "async") || !self.func.store().async_support(),
|
||||
"must use `call_async` with async stores"
|
||||
);
|
||||
unsafe { self._call(params) }
|
||||
|
||||
@@ -136,7 +136,7 @@ impl Store {
|
||||
// once-per-thread. Platforms like Unix, however, only require this
|
||||
// once-per-program. In any case this is safe to call many times and
|
||||
// each one that's not relevant just won't do anything.
|
||||
wasmtime_runtime::init_traps();
|
||||
wasmtime_runtime::init_traps().expect("failed to initialize trap handling");
|
||||
|
||||
Store {
|
||||
inner: Rc::new(StoreInner {
|
||||
@@ -209,6 +209,7 @@ impl Store {
|
||||
}
|
||||
|
||||
/// Returns the [`Engine`] that this store is associated with.
|
||||
#[inline]
|
||||
pub fn engine(&self) -> &Engine {
|
||||
&self.inner.engine
|
||||
}
|
||||
@@ -503,10 +504,12 @@ impl Store {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn externref_activations_table(&self) -> &VMExternRefActivationsTable {
|
||||
&self.inner.externref_activations_table
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn stack_map_registry(&self) -> &StackMapRegistry {
|
||||
&self.inner.stack_map_registry
|
||||
}
|
||||
@@ -655,6 +658,7 @@ impl Store {
|
||||
});
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn async_support(&self) -> bool {
|
||||
self.inner.engine.config().async_support
|
||||
}
|
||||
@@ -915,6 +919,7 @@ impl Store {
|
||||
}
|
||||
|
||||
unsafe impl TrapInfo for Store {
|
||||
#[inline]
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
@@ -930,6 +935,7 @@ unsafe impl TrapInfo for Store {
|
||||
false
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn max_wasm_stack(&self) -> usize {
|
||||
self.engine().config().max_wasm_stack
|
||||
}
|
||||
@@ -956,6 +962,7 @@ unsafe impl TrapInfo for Store {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn interrupts(&self) -> &VMInterrupts {
|
||||
&self.inner.interrupts
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user