Boris
Boris

Reputation: 769

Multithreaded WebAssembly slower in browser than singlethreaded, why?

After not using Emscripten for a couple of years, I've recently discovered that it now supports compilation of multithreaded C++ code to WebAssembly. I've put together simple merge sort code that sorts 10 million floats (native code can easily sort more than that, but browsers seem to limit you to 1GB memory):

https://github.com/bsergeev/MtMergeSort

Surprisingly, while this code compiles to WebAssembly and runs in Chrome, the sort in the browser is getting slower as multiple threads are used (while single thread performance is, as expected, 1.5...2 times as slow as native one: native code 1.80 seconds, WebAssembly 3.1...3.3 seconds, and JavaScript 4.69 seconds):

Comparison of x64 vs. WebAssembly vs. JavaScript

Is the diminishing performance on multiple threads caused by browsers throttling down WebWorkers? But what is the point of multiple threads in WebAssembly then?

Upvotes: 4

Views: 2560

Answers (1)

Boris
Boris

Reputation: 769

It turned out the culprit was allocation of left/right temporary arrays on different threads in merge(). As soon as I pre-allocated the temporary array on the main thread, WebAssembly scaled nicely: Multi-threaded performance

Upvotes: 6

Related Questions