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
· game
· 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)
WebAssembly VS Javascript
Now that I mentioned Web technology , I have to mention another one in Web Brilliant script language in project development Javascript.1995 year ,Brendan Eich In less than 10 Heaven created Javascript, It was originally mainly used for form validation , Not speed . With the complexity of various application functions , Limited by JavaScript The design and execution of the language itself , The problem of its performance is becoming more and more obvious .
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 .
So , The two are often used to compare , There was even a time when WebAssembly It will replace Javascript In the speech . You bet , As a class assembly language ,WebAssembly It's solved Javascript The most frequently criticized performance issue , It's based on this ,WebAssembly It's not meant to be written by developers , Only one compilation target can be provided for other languages .
therefore , The relationship between these two technologies is not competition , It's more like a win-win cooperation . adopt Javascript API, You can take WebAssembly The module is loaded into your page . in other words , You can go through WebAssembly To take full advantage of the performance of compiled code , Keep at the same time Javascript The flexibility of the .
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 .
This is it. WebAssembly The compilation principle of , since WebAssembly The core goal of is to work with Javascript etc. Web Technical compatibility , So how compatible is it ? below , We're going to test it through the actual project .
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.
Project practice :WebAssembly + Javascript
Before entering the actual combat of the project , You need to understand a core concept , namely JavaScript Why can we completely control WebAssembly Code , And execute download and compile run :
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 .
so ,JavaScript API Provides the creation module for developers 、 Memory 、 The ability of tables and instances .
Through one WebAssembly example ,JavaScript Be able to call the functions exposed by this instance , hold JavaScript Function import to WebAssembly In the example ,WebAssembly Can also call JavaScript function .
in addition ,WebAssembly Can't read or write directly DOM, Can only call JavaScript, And you can only pass in the raw data of integer and floating point as parameters . therefore ,JavaScript Be able to completely control WebAssembly Code to download 、 compile 、 function , JavaScript Developers can also put WebAssembly Think of it as a high-performance function JavaScript characteristic .
Code example
wasm(Rust):
wasm_bindgen Mainly used to generate some glue code , Simplify developers in JS and wasm Method call between .
JS:
Project structure
IDE
VSCode+ plug-in unit Rust
Execution steps
- 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
Conclusion
Through a brief introduction to WebAssembly Application scenarios and main features of , We can better understand WebAssembly The evolution of Technology . stay 11 month 11 In Japan's technology sharing , We will be based on Webpack+Rust Examples continue to explore WebAssembly, And through the project actual combat demonstration WebAssembly And JavaScript Implementation process of code combination . Welcome interested students , Sign up for , Registration address :http://live.vhall.com/756593738.