About the lecturer ： Zhai Ben , GrapeCity GCDS Senior Development Engineer of project team , Good at the front end 、.NET Desktop technology , Committed to tool chain development , Like to try various cutting-edge technologies and share .
If you mention the most exciting new technology in recent years , Not WebAssembly Perhaps judge of particulars, . As A low-level class assembly language ,WebAssembly Store in compact binary format , by C/C++, Rust Model languages with low-level memory provide new compilation targets . Because of this ,WebAssembly A smaller , Can run at a speed close to native performance .
WebAssembly Technology itself has many advantages , Although it started in browser, it has been integrated by various languages and platforms , In the actual industrialization landing , Blockchain 、 Edge of computing 、 Games, images, videos and many other fields rely on WebAssembly Created a product that was admired .
WebAssembly Application scenarios of
· compiler （ Compile chain ）
· Multimedia editing
· Image recognition
· VR+ Virtual reality
· Live video effects
· game 、 Application distribution service
· Server side running untrusted code
· Mobile hybrid applications
· P2P application
WebAssembly The main characteristics of
· Fast 、 Efficient 、 portable
· Can be read 、 It can be debugged
· Security , Follow the browser's same origin policy and authorization policy , Running in a sandbox environment
· And others web Technical compatibility (JS)
2008 year , The battle over browser performance has finally broken out among major browser manufacturers , After going through the instant compiler （JITs）, And with Node.js and Electron After the period when the application was built ,WebAssembly It is expected to be JS The turning point for the engine to break through the next performance bottleneck .
Performance comparison between the two
The picture below shows us JS The engine runs the program and runs Wasm Time consuming comparison of ：
JS When the engine runs the program , Need to go through source code conversion （parse）、 Generated bytecode （compile + optimize）、 Compiler optimization （re-optimize）、 Code execution （execute） And memory cleanup （GC） These five stages ：
· parse : Convert the source code into an abstract syntax tree , Pass it to the interpreter .
· compile + optimize : The interpreter generates bytecode , And through the compiler （JIT） Compile optimized partial bytecode , Generate machine code .
· re-optimize : When it is found that the optimization code cannot be optimized by the compiler , Reproduction is transferred to the interpreter .
· execute : The process of executing code .
· GC: Time to clear memory .
In most cases ,JS Compile bytecode into machine code at run time , This stage is very time consuming .（ This is because JS The dynamic nature of , The same code is recompiled by different types ）. and Wasm It doesn't need to be parsed , There is no need to dynamically detect data types at run time , Because it's already bytecode , So it's just a simple decoding , It contains all types of information .
Precisely because Wasm Most of the optimization work is already in LLVM The front end of the , So little work is done on compiler optimization , This is the main embodiment of its high performance .
Compile model (LLVM)
LLVM（Low-Level-Virtural-Machine）, The underlying virtual machine architecture , Advantages include ：
- Modular design （ Three paragraph ： front end 、 Optimizer 、 Back end ）, The code is clearer and easier to troubleshoot , The front end is responsible for parsing , Generated bytecode ; The optimizer is responsible for the good bytecode ; The back end is responsible for generating the machine code of the corresponding platform
- Language independent intermediate code , It can be infinitely expanded without harming debugging
- As a tool and function library , Easy to implement new programming language based optimization compiler or VM
WebAssembly And LLVM combination
WebAssembly And LLVM combination , There is no need to add additional front-end compiler tools for each language , In the middle of the IL Can be continuously optimized , Just add one “ Back end ”, So that most languages can be compiled into wasm. This “ Back end ” Different from the back end mentioned earlier , It doesn't generate machine code directly , It generates wasm, It's going to be done by the browser wasm The runtime is responsible for compiling and running .
notes ： Specific code and Demo The example will be in 11 month 11 On the afternoon of Sunday 2 In the open class , Welcome to sign up to watch , Registration address ：http://live.vhall.com/756593738.
The core concept
· Module（ modular ）： This module represents an executable machine code that has been compiled by the browser wasm Binary sequence . Modules are stateless , It can be cached in IndexedDB In or in workers To share , Can also be like JS Import and export as well .
· Memory（ Memory ）： A contiguous variable size array of bytes , It can be Wasm and JS Simultaneous reading and writing . It can be used in JS and Wasm Transfer data between , communicate .
· Table（ form ）： Variable size array with type , The table stores references to objects that cannot be stored in memory as raw bytes .
· Instance（ example ）： A module and all its states at runtime , Including memory , form , And the imported values .
wasm_bindgen Mainly used to generate some glue code , Simplify developers in JS and wasm Method call between .
VSCode+ plug-in unit Rust
- Install Rust：https://www.rust-lang.org/tools/install open cmd, Input cargo Make sure the installation is correct .
- install wasm Compiler tools ：$ cargo install wasm-pack
- establish rust-wasm engineering ： $ cargo new –-lib wasmlib
- Build： $ wasm-pack build