a WebAssembly Squeak virtual machine is running
I’ve replaced the inner instruction-dispatch loop of a running SqueakJS virtual machine with a handwritten WebAssembly (WASM) function, and run several thousand instructions of the Caffeine object memory. The WASM module doesn’t yet have its own memory. It’s using the same JavaScript objects that the old dispatch loop did, and the supporting JS state and functions (like Squeak.Interpreter class). I wrote a simple object proxy scheme, whereby WASM can use unique integer identifiers to refer to the Smalltalk objects.
Because of this indirection, the current performance is very slow. The creation of an object proxy is based on stable object pointer (OOP) values; young objects require full garbage collection to stabilize their OOPs. There is also significant overhead in calling JavaScript functions from WASM. At this stage, the performance penalties are worthwhile. We can verify that the hybrid JS/WASM interpreter is working, without having to write a full WASM implementation first.
a hybrid approach
My original approach was to recapitulate the Slang experience, by using Epigram to decompile the Smalltalk methods of a virtual machine to WASM. I realized, though, that it’s better to take advantage of the livecoding capacity of the SqueakJS VM. I can replace individual functions of the SqueakJS VM, maintaining a running system all the while. I can also switch those functions back and forth while the system is running, perhaps many millions of instructions into a Caffeine session. This will be invaluable for debugging.
The next goal is to duplicate the object memory in a WASM memory, and operate on it directly, rather than using the object proxy system. I’ll start by implementing the garbage collector, and testing that it produces correct results with an actual object memory, by comparing its behavior to that of the SqueakJS functions.
Minimal object memories will be useful in this process, because garbage collection is faster, and there is less work to do when resuming a snapshot.
performance improvement expected
From my experiment with decompiling a Smalltalk method for the Fibonacci algorithm into WASM, I saw that WASM improves the performance of send-heavy Smalltalk code by about 250 times. I was able to achieve significant speedups from the targeted use of WASM for the inner functions of BitBLT. From surveying performance comparisons between JS and WASM, I’m expecting a significant improvement for the interpreter, too.
14 April 2023 at 11:33 pm
You are a genius to translate JS to WASM in a live session. Wonders of dynamic programming.
LikeLiked by 1 person
17 April 2023 at 5:47 pm
Any chance you could record a video of live replacement of functions in the SqueakJS VM?
LikeLiked by 1 person
17 April 2023 at 6:52 pm
Sure, I can do that.
LikeLike
17 April 2023 at 7:14 pm
Looking forward to it. Hearing about the new things you are doing with Squeak makes me want to program in squeak more. What could be next… ?
LikeLiked by 1 person
8 May 2023 at 8:42 pm
[…] adventures in livecoding « a WebAssembly Squeak virtual machine is running […]
LikeLike