Description
$ time asc --version
Version 0.20.6
________________________________________________________
Executed in 2.32 secs fish external
usr time 7.88 secs 0.00 micros 7.88 secs
sys time 0.15 secs 951.00 micros 0.15 secs
The problem is even worse when limited to one CPU core:
$ time taskset -c 0 asc --version
Version 0.20.6
________________________________________________________
Executed in 7.06 secs fish external
usr time 6.86 secs 541.00 micros 6.86 secs
sys time 0.15 secs 829.00 micros 0.15 secs
2 to 7 seconds? Does that matter?
On large codebases that don't get compiled often, maybe not so much, but when iterating quickly on a small piece of code that takes a fraction of a second to actually compile, this is a big problem. Imagine someone with a much worse PC than me trying to learn AssemblyScript - they may have to wait 30 seconds to compile their Hello World. Maybe that seems like an exaggeration but on the website repl.it (which, admittedly, has very low compute resources) it does take 20 seconds to compile a Hello World, with a warning about 100% CPU usage the whole time. Multiply this by hundreds of thousands of compilations in the development process and this is a big problem.
Why is it taking so long?
I asked on the AssemblyScript Discord and apparently this is caused by loading a 5MBish WASM file of Binaryen on each start. I was told it was probably because of parsing/base64 decoding, as it is stored in a base64 string in a JS file. but because the time reduces with the amount of CPU cores I think this is WASM module compilation.
nodejs does not yet have a WASM module cache, so it has to compile the Binaryen WASM file to native code from scratch on every start.
Possible solutions
With my limited knowledge. here are some solutions that seem like they could work:
- Make Binaryen into a native module
- Wait for nodejs to have a compiled module cache
- Have a server that runs in the background to compile things on demand
- Split Binaryen into parts and only load what's needed (e.g. don't load the optimization code unless optimizing)