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 theVMTrampoline
for the host
function in a side table so it can be looked up later if the host
function is called through theFunc
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 atFunc
-insertion-time (aka instantiation
time) and instead only lazily populate the map of trampolines when
needed. The theory behind this is that almost allFunc
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
makinglookup_trampoline
a bit slower seems ok.The
lookup_trampoline
function will now, on a miss from the wasm
modules andhost_trampolines
map, lazily iterate over the functions
within the store and insert trampolines into thehost_trampolines
map.
This process will eventually reach something which matches the function
provided because it should at least hit the same host function. The
relevantlookup_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.
[ ] This has been discussed in issue #..., or if not, please tell us why
here.[ ] A short description of what this does, why it is needed; if the
description becomes long, the matter should probably be discussed in an issue
first.[ ] This PR contains test cases, if meaningful.
- [ ] A reviewer from the core maintainer team has been assigned for this PR.
If you don't know who could review this, please indicate so. The list of
suggested reviewers on the right can help you.Please ensure all communication adheres to the code of conduct.
-->
alexcrichton requested fitzgen for a review on PR #3742.
fitzgen submitted PR review.
fitzgen submitted PR review.
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.
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.
fitzgen created PR review comment:
// Next consult the list of store-local host trampolines. This is
fitzgen created PR review comment:
let old_entry = self.host_trampolines.insert(f.sig_index(), f.trampoline()); debug_assert!(old_entry.is_none());
fitzgen created PR review comment:
// into `host_trampolines` yet. Skip over all the ones we've looked at
fitzgen created PR review comment:
/// wasm given an anyfunc function pointer.
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.
fitzgen created PR review comment:
/// This is a somewhat complicated implementation at this time, unfortunately.
alexcrichton submitted PR review.
alexcrichton created PR review comment:
Indeed!
alexcrichton submitted PR review.
alexcrichton created PR review comment:
I'm glad you're better with words than I am
alexcrichton updated PR #3742 from no-insert-hashmap-instantiate
to main
.
alexcrichton updated PR #3742 from no-insert-hashmap-instantiate
to main
.
alexcrichton submitted PR review.
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.
alexcrichton merged PR #3742.
Last updated: Dec 23 2024 at 12:05 UTC