It's nice that you can precompile Module objects and create Instances from them, saving at least some work, but would even more work and memory sharing be possible between instances?
Let's say I wanted to host lots and lots of little instances of the same code but with separate linear memories for isolation.
If the instances are short-lived, there's a rather obvious strategy for saving JIT work - for each {wasm binary, import table}, keep a little cache of instances. When you're done with one, restore global initialized values (can't see a way to do that, but should be possible), wipe the rest of its memory to zero (Is there a fast way to do so? Setting the memory to zero, cell by cell, seems unwieldy), and immediately re-use it.
However if the instances are many and long-lived, it might be good if they could share not just the IR representation but also the jitted code, to save memory. In the jitted code, I guess this would require keeping a base pointer to the current instance's memory, maybe statically allocated to a host register, and make all memory accesses offset from that pointer (effectively making the memory relocatable, kind of the reverse thing from PIC code shared by processes mapped at different addresses). There would be a small performance hit to the code of course due to the additional dynamic offset required.
Wasmer has a cache feature to reuse jitted code, there are some example usages for the Cache API in the codebase: https://github.com/wasmerio/wasmer/blob/1ac689c70b9296bf242d61ee3ebb02f460017a4c/src/bin/wasmer.rs#L185
There is also a blog post regarding caching: https://medium.com/wasmer/running-webassembly-100x-faster-%EF%B8%8F-a8237e9a372d
The team has discussed sharing/reusing memories in some way in the past and will consider this for future implementation.
Oh, neat! Somehow missed that post, thanks for pointing me to it.
We have a similar, but different use case.
We have a network/cluster simulator where we simulate many devices, all running the same C code. Currently we load the device's code once, we let it run for one tick, then we backup its memory, restore the next instance's memory, let it run for a "tick" and so on. All this memory copying makes the simulator quite slow (around 100 nodes can be simulated in real time). If we could just point to a different memory instances at each "tick" call, we could increase the speed quite a lot.
Having multiple instances of the code will make the instruction cache read a lot more from memory instead. Probably not as much as our current backup & restore, but still quite a lot.
Right now, in the upcoming Wasmer 1.0 release the Modules can be serialized and deserialized quite easily.
Here's an example of it:
https://github.com/wasmerio/wasmer/blob/master/examples/engine_headless.rs
Closing the issue
Most helpful comment
Right now, in the upcoming Wasmer 1.0 release the Modules can be serialized and deserialized quite easily.
Here's an example of it:
https://github.com/wasmerio/wasmer/blob/master/examples/engine_headless.rs
Closing the issue