Stream: git-wasmtime

Topic: wasmtime / PR #3742 Lazily populate a store's trampoline map


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

alexcrichton opened PR #3742 from no-insert-hashmap-instantiate to main:

This commit is another installment of "how fast can we make
instantiation". Currently when instantiating a module with many function
imports each function, typically from the host, is inserted into the
store. This insertion process stores the VMTrampoline for the host
function in a side table so it can be looked up later if the host
function is called through the Func interface. This insertion process,
however, involves a hash map insertion which can be relatively expensive
at the scale of the rest of the instantiation process.

The optimization implemented in this commit is to avoid inserting
trampolines into the store at Func-insertion-time (aka instantiation
time) and instead only lazily populate the map of trampolines when
needed. The theory behind this is that almost all Func instances that
are called indirectly from the host are actually wasm functions, not
host-defined functions. This means that they already don't need to go
through the map of host trampolines and can instead be looked up from
the module they're defined in. With the assumed rarity of host functions
making lookup_trampoline a bit slower seems ok.

The lookup_trampoline function will now, on a miss from the wasm
modules and host_trampolines map, lazily iterate over the functions
within the store and insert trampolines into the host_trampolines map.
This process will eventually reach something which matches the function
provided because it should at least hit the same host function. The
relevant lookup_trampoline now sports a new documentation block
explaining all this as well for future readers.

Concretely this commit speeds up instantiation of an empty module with
100 imports and ~80 unique signatures from 10.6us to 6.4us, a 40%
improvement.

<!--

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 (Feb 01 2022 at 21:54):

alexcrichton requested fitzgen for a review on PR #3742.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 01 2022 at 22:23):

fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 01 2022 at 22:23):

fitzgen submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 01 2022 at 22:23):

fitzgen created PR review comment:

I am having a really hard time parsing this first sentence for whatever reason. Is this what you are saying?

Most of the time we are looking up a Wasm function's trampoline when
calling this function, and we don't want to make insertion of a host
function into the store more expensive than it has to be.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 01 2022 at 22:23):

fitzgen created PR review comment:

    ///   signatures of functions that escape the module (e.g. exports and `ref.func`able functions)

Right? Just double checking my understanding.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 01 2022 at 22:23):

fitzgen created PR review comment:

        // Next consult the list of store-local host trampolines. This is

view this post on Zulip Wasmtime GitHub notifications bot (Feb 01 2022 at 22:23):

fitzgen created PR review comment:

            let old_entry = self.host_trampolines.insert(f.sig_index(), f.trampoline());
            debug_assert!(old_entry.is_none());

view this post on Zulip Wasmtime GitHub notifications bot (Feb 01 2022 at 22:23):

fitzgen created PR review comment:

        // into `host_trampolines` yet. Skip over all the ones we've looked at

view this post on Zulip Wasmtime GitHub notifications bot (Feb 01 2022 at 22:23):

fitzgen created PR review comment:

    /// wasm given an anyfunc function pointer.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 01 2022 at 22:23):

fitzgen created PR review comment:

    /// relatively expensive hash map insertion. Instead the work is deferred
    /// until we actually look up that trampoline in this method.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 01 2022 at 22:23):

fitzgen created PR review comment:

    /// This is a somewhat complicated implementation at this time, unfortunately.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 01 2022 at 22:27):

alexcrichton submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 01 2022 at 22:27):

alexcrichton created PR review comment:

Indeed!

view this post on Zulip Wasmtime GitHub notifications bot (Feb 01 2022 at 22:44):

alexcrichton submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 01 2022 at 22:44):

alexcrichton created PR review comment:

I'm glad you're better with words than I am

view this post on Zulip Wasmtime GitHub notifications bot (Feb 01 2022 at 22:46):

alexcrichton updated PR #3742 from no-insert-hashmap-instantiate to main.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 02 2022 at 15:12):

alexcrichton updated PR #3742 from no-insert-hashmap-instantiate to main.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 02 2022 at 15:12):

alexcrichton submitted PR review.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 02 2022 at 15:12):

alexcrichton created PR review comment:

Ah actually doing this causes a test failure because we might find duplicates of signatures we're not looking for as the array of functions is scanned.

view this post on Zulip Wasmtime GitHub notifications bot (Feb 02 2022 at 15:43):

alexcrichton merged PR #3742.


Last updated: Oct 23 2024 at 20:03 UTC