Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can multiple Wasm modules interact with each other and share memory directly via functions?

Is there a way to instantiate two Wasm modules, a and b such that a can call functions from b and also access the distinct memory from b? For example, let's say that a gets memory by calling an implementation of malloc in b that is exposed to a. This way, all useful memory comes from b. If so, how can it be done? More specifically, can this be done without additional overhead, and without interacting with JavaScript in-between these operations (except for the initialization step when instantiating the modules and setting imports/exports)? What are the performance characteristics of this sort of indirection and memory access, or is there no issue at all? In this case, I want to micro-optimize.

(I've read several pieces of documentation, but I can't find a clear answer. I think that in future versions of Wasm, there are plans for standardized dynamic linking that would help, but we're not there yet.)

like image 414
synchronizer Avatar asked Jan 14 '20 03:01

synchronizer


People also ask

What is Wasm memory?

The WebAssembly. Memory() constructor creates a new Memory object whose buffer property is a resizable ArrayBuffer or SharedArrayBuffer that holds the raw bytes of memory accessed by a WebAssembly. Instance .

What are Wasm modules?

WebAssembly, or Wasm for brevity, is a Web-optimized executable software format, designed to give programmers the greatest possible flexibility. Wasm binary modules can be compiled once, and then safely run anywhere, alone or embedded in other applications.

What are the static methods of WebAssembly object?

Static methods Compiles and instantiates a WebAssembly module directly from a streamed underlying source, returning both a Module and its first Instance . Compiles a WebAssembly. Module from WebAssembly binary code, leaving instantiation as a separate step.

Can JavaScript be compiled to WebAssembly?

Yes, it is totally possible to call JavaScript-functions from inside your running WebAssembly functions!


1 Answers

Is there a way to instantiate two WASM modules, a and b such that a can call functions from b

Yes, you can instantiate module b exporting one of its functions, then instantiate a importing that function so that the two can interact. However, this is not going to be as fast as one WebAssembly function calling another as the call is going via the host environment.

and also access the distinct memory from b

Yes, once again, this is possible. Linear memory can be shared between a WebAssembly module and its host, and can also be shared between two modules.

can this be done without additional overhead, and without interacting with JavaScript in-between these operations

As iterates above, no, it cannot currently be done without additional overhead.

This will change in the future as the WebAssembly specification is enhanced and matures. One challenge involved in usefully communicating directly between two WebAssembly modules is understanding the API each exposes. WebAssembly is a compilation target, and different source languages (C++, Rust) encode types in different ways — this significantly limits inter-module communication.

An important stepping stone towards this is Interface Types, which encodes the API specification for a module / function. Once this has been implemented, direct communication between WebAssembly modules should be quite possible.

like image 53
ColinE Avatar answered Oct 08 '22 22:10

ColinE