Stream: wasmtime

Topic: Binding imports to closures with exports


view this post on Zulip Matias Funder (Nov 30 2020 at 08:00):

In an embedding use case, the host loads a module. The module has imports and exports. The imports must be bound when creating an instance. But the imports must call exports, and operate on instance memory.

It is possible to use Caller, but it is too slow.

My use case is collaborating isolated micromodules. Imagine a Fibonacci algorithm where each computation crosses between host and module (in fact, the host will bridge two micromodules). This means that Caller will be called a million times.
When running Fib in the Wasm module only, it takes 56us. When crossing, it takes 3.8ms. It is almost 70x slower, which I think is largely due to the Caller (I haven't learned how to do proper profiling in Rust yet).

My question is: Is there a better way to bind imports, so that the exports and memory are available?

Currently I'm trying to look into caching the Caller result in the import binding, but that's a lot of boilerplate code, and it would be far better to bind during setup.

view this post on Zulip Alex Crichton (Nov 30 2020 at 15:05):

Wasm itself is designed this way where a module's exports are not available before a module is instantiated, e.g. when you're creating the exports

view this post on Zulip Alex Crichton (Nov 30 2020 at 15:06):

One way to implement this would be to have the module import its memory, but otherwise all you can do is "fill it in after the fact" where each import has a slot that's filled in after instantiation

view this post on Zulip Alex Crichton (Nov 30 2020 at 15:06):

which means that if the start function calls imports it'll likely "panic" or t rap


Last updated: Dec 23 2024 at 13:07 UTC