New WebAssembly backend #963
Replies: 4 comments 47 replies
-
Magnificent job, Alexey! I can try my pet project with WASM GC. It has 40k lines of code, interoperates with DOM. It matches your description (I assume that I'm using nothing you mentioned in "except"). Project currently compiles with maven. Probably there should be some kind of FAQ or thread for people who are trying WASM GC and facing unknown issues. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Hey, guys. I fixed several bugs according to feedback and published new preview build. I'm going for vacation, so I'll not be able to fix any further bugs for the next week. |
Beta Was this translation helpful? Give feedback.
-
Great job! I really appreciate you put in the time to get this all working. Please don’t kill off the legacy WASM backend yet though, my impression of WASM GC right now is that the authors if the WebAssembly spec rushed it, and it’s still less than even an MVP with their stupid arrays that can’t be copied efficiently. I don’t care that the legacy WASM backend makes a larger binary, I was gonna compress it with LZMA anyway which seems to reduce its size by like 75%. Wait until the WASM GC spec itself is actually complete before killing off the legacy WASM backend you spent nearly a decade working on. |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm glad to announce that I just published preview of new WebAssembly GC backend.
Let me dig dive in history a little bit. First WebAssembly backend was ready somewhere around 2016/2017, when WebAssembly MVP spec was in development and browsers still had bugs in Wasm support and could not even print disassembly. This old backend works perfectly, but I never had enough motivation to publish some docs and encourage community to switch to WebAssembly. The main reason was the inability to create seamless interoperation layer between Java and JS objects.
This is caused by the fact that WebAssembly does not (or at least did not) operate in terms of objects, but rather has a large flat piece of memory called "heap". TeaVM Wasm BE has its own allocator and its own tracing GC implementation, whereas JS lives with its own GC. And it's the fact: one can't just make two different managed heaps each with own GC to communicate to each other. Or at least, communicate seamlessly. This means, that from both sides objects needed to be explicitly "acquired" and "released", which would lead to some sort or reference counting, even when one want to pass callback to JS API.
Another issue was the size of generated binary and its performance. Performance is better than that of JavaScript, but not significantly better. But the generated binary is huge, usually 5x times larger than JS generated from the same source. On one hand, this could be optimized by shrinking data section, which had just unpacked representation of Java class metadata, by providing some sort of compression. On the other hand, I could do nothing with code section.
Why did these issues occur? Because of GC! No, GC itself is tiny, about 10K, but the problem with WebAssembly is that it does not allow to access CPU stack. Usually, managed runtimes access stack directly when they perform mark phase of GC, but in the absence of access to the stack the only viable alternative is maintaining shadow stack, which in turn has tremendous overhead, both size- and performance-wise.
WebAssembly GC proposal adds objects (or structures in its terms) to WebAssembly, so it becomes possible to make references between JS and WebAssembly objects, which effectively allows to re-create JSO, Java-to-JavaScript interaction layer. This in turn allows to take existing programs and almost without modifications compile them with new backend. Chrome and Firefox added support for this proposal almost a year ago, Safari provides it as technology preview.
I spent several months implementing support for WebAssembly GC. This is not just "small extension", this proposal is a game changer that requires to re-think and re-write entire backend, which I did. Instead of modifying existing WebAssembly BE, I just created a new one, so both co-exist together. My experiments have shown that with Wasm GC performance is a bit better than with Wasm MVP and much better than with JS (although still far from near native speed), while binary size is comparable to JS. Given this, and given that WebAssembly has native int64 support (which allows fast
long
implementation) and float32 (which allows to avoid some rare issues with rounding in case of usingfloat
), I'm determined to push new Wasm GC BE to release stage and to encourage community to switch to it.As for now, Wasm GC BE supports almost everything the JS BE does, except for:
@Async
annotation, which means not only support of this annotation in user code, but also those methods in TeaVM classlib that utilize@Async
internally, likeThread.sleep
,Object.wait
, etc.ArrayBuffer
. I'm planning to introduce extension to JSO to pass NIO buffers between JS and Java, which would allow to distinguish same semantics.To try WebAssembly GC in action, you can open old good sample. Unfortunately, I still did not have a chance to run WebAssembly GC with something really big, only some small examples like this.
If you want to try WebAssembly GC as a developer, you better ask me directly or learn code, there's still no documentation at all. As a quick start with gradle, learn this sample. In short, everything related to Wasm GC is located in
teavm.wasmGC
DSL. And don't forget that new backend is only available in latest preview build.To start with Maven, you need to set
tagetType
property of your plugin execution toWEBASSEMBLY_GC
. Additionally, you need to configure copying of WasmGC runtime:From HTML side you need something like this:
During next few months I'm going to prepare backend for release, which does not mean that I'm going to implement missing features, but rather fix bugs and write some documentation. I hope for your feedback and good luck with trying TeaVM.
Beta Was this translation helpful? Give feedback.
All reactions