Stream: git-wasmtime

Topic: wasmtime / PR #3727 Lazily load types into `Func`


view this post on Zulip Wasmtime GitHub notifications bot (Jan 26 2022 at 19:11):

alexcrichton opened PR #3727 from no-type-in-func to main:

This commit changes the construction of a Func to lazily load the type
information if required instead of always loading the type information
at Func-construction time. The main purpose of this change is to
accelerate instantiation of instances which have many imports. Currently
in the fast way of doing this the instantiation loop looks like:

let mut store = Store::new(&engine, ...);
let instance = instance_pre.instantiate(&mut store);

In this situation the instance_pre will typically load host-defined
functions (defined via Linker APIs) into the Store as individual
Func items and then perform the instantiation process. The operation
of loading a HostFunc into a Store however currently involves two
expensive operations:

Neither of these is actually necessary for imported functions. The
FuncType for imported functions is never used since all comparisons
happen with the intern'd indices instead. The only time a FuncType is
used typically is for exported functions when using Func::typed or
similar APIs which need type information.

This commit makes this path faster by storing Option<FuncType> instead
of FuncType within a Func. This means that it starts out as None
and is only filled in on-demand as necessary. This means that when
instantiating a module with many imports no clones/locks are done.

On a simple microbenchmark where a module with 100 imports is
instantiated this PR improves instantiation time by ~35%. Due to the
rwlock used here and the general inefficiency of pthreads rwlocks the
effect is even more profound when many threads are performing the same
instantiation process. On x86_64 with 8 threads performing instantiation
this PR improves instantiation time by 80% and on arm64 it improves by
97% (wow read-contended glibc rwlocks on arm64 are slow).

Note that much of the improvement here is also from memory
allocatoins/deallocations no longer being performed because dropping
functions within a store no longer requires deallocating the FuncType
if it's not present.

A downside of this PR is that Func::ty is now unconditionally taking
an rwlock if the type hasn't already been filled in. (it uses the
engine). If this is an issue in the future though we can investigate at
that time using somthing like a Once to lazily fill in even when
mutable access to the store isn't available.

<!--

Please ensure that the following steps are all taken care of before submitting
the PR.

Please ensure all communication adheres to the code of conduct.
-->

view this post on Zulip Wasmtime GitHub notifications bot (Jan 26 2022 at 19:19):

cfallin submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 26 2022 at 19:19):

cfallin submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 26 2022 at 19:19):

cfallin created PR review comment:

There's probably a mundane borrow-checker-subtlety reason for returning the downgraded immutable-borrow of StoreOpaque here, but could we add a comment describing why?

view this post on Zulip Wasmtime GitHub notifications bot (Jan 26 2022 at 20:23):

alexcrichton updated PR #3727 from no-type-in-func to main.

view this post on Zulip Wasmtime GitHub notifications bot (Jan 26 2022 at 20:23):

alexcrichton merged PR #3727.


Last updated: Nov 22 2024 at 16:03 UTC