Open kripken opened 8 months ago
(these numbers are all wasm-opt -all poppler.wasm -O3 -o out.wasm
using poppler from the Emscripten test suite)
Actually after more careful measurements of a longer task, removing SINGLE_FILE, and reading elapsed
time and not user
(even with BINARYEN_CORES=1
, V8 uses more cores internally, so user
is higher), the multithreaded difference is just 1.5x and not 2x, and the single-threaded difference is just 1.3x. So we are quite close to native speed here, and there is still some (small) amount of extra multithreading overhead.
Enabling -flto
on the wasm build (but not the native one, I didn't test that, so it's not apples to apples) improves from 1.3x on single-core to 1.25x.
There is a 0.5 second startup lag, however, related to the initial parse of the 6 MB wasm file I assume. Node.js doesn't cache compiled wasm atm AFAIK, but if it did do that (like browsers did) then that issue would mostly go away.
This issue is about porting wasm-opt itself to wasm, and how fast it runs there.
The main blocker here was that it was 10x slower than a native build:
https://github.com/emscripten-core/emscripten/issues/15727
That turned out to be an issue with multithreading, and is fixed by using Emscripten's recent
mimalloc
port (see last comments in that issue). Now instead of 10x slower it is 2x slower, which is not bad considering it is managing to saturate all CPU cores like a native build.Looking more into the remaining 2x, that factor remains even when pinning both builds to a single core, so multithreading is not related. Some quick profiling:
C++ symbols above 1% (linux perf)
Wasm symbols above 1% (node --prof)
In node there is some 5-10% overhead that is not compiled wasm code, and which seems to be V8 compiling the wasm as best I can tell, but that is minor compared to the 2x slowdown so we can ignore it. The compiled wasm breakdown looks like this:
LocalGraph
is high in both, as is malloc/free, which all makes sense. More suspicious is thatReFinalize
takes over 10% in wasm while in native C++ it is hardly noticeable:That might be related to inlining differences, if ReFinalize does lots of malloc/free which show up in C++ as malloc/free but are inlined in wasm and end up there as the calling function.
ReFinalize does not do that much work beyond a quick walk of the module, so this suggests all walks are slower in wasm...