Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How browser makes WebAssembly Stack Machine more efficient

In this article on WebAssembly, it says:

...we have to talk about one more thing: stack machines. Although the browser compiles it to something more efficient...

Wondering how the browser makes a stack machine more efficient, what sort of techniques does it use.

like image 730
Lance Avatar asked Apr 22 '18 16:04

Lance


People also ask

How efficient is WebAssembly?

Wasm is 1.15-1.67 times faster than JavaScript on Google Chrome on a desktop. Wasm is 1.95-11.71 times faster than JavaScript on Firefox on a desktop. Wasm is 1.02-1.38 times faster than JavaScript on Safari on a desktop. Wasm is 1.25-2.59 times faster than JavaScript on Chrome on a Moto G5 Plus smartphone.

What makes WebAssembly fast?

Because WebAssembly is more compact than JavaScript, fetching it is faster. Even though compaction algorithms can significantly reduce the size of a JavaScript bundle, the compressed binary representation of WebAssembly is still smaller. This means it takes less time to transfer it between the server and the client.

What are the benefits of WebAssembly?

Faster, Efficient and Portable − Due to the small size of the code, it loads and executes faster. Easy to understand − Developers don't have to do much stress in understanding WebAssembly coding, as they don't have to write the code in WebAssembly.


1 Answers

It compiles it into the use of registers.

One way to think about Wasm's operand stack is that it is simply an (infinite) set of virtual registers. But instead of referencing these registers explicitly, each instruction implicitly references certain slots from the stack. But thanks to type checking and validation, the compiler always statically knows the number and types of values on the stack at any point in the program.

Hence the generated code never needs to materialise the stack, but can straightforwardly assign every stack slot, i.e. every virtual register, to a real hardware register (or spill them over to the hardware stack in the usual manner when register allocation runs out of registers).

The only reason Wasm is a stack machine and not a register machine is code compactness. It is a particularly compact way to describe register use.

like image 133
Andreas Rossberg Avatar answered Nov 10 '22 12:11

Andreas Rossberg