Technology sharing: can web assembly redefine the front-end development model?

Grape City 2020-11-09 14:29:29
technology sharing web assembly redefine

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 :

  1. 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
  2. Language independent intermediate code , It can be infinitely expanded without harming debugging
  3. 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 :

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_bindgen Mainly used to generate some glue code , Simplify developers in JS and wasm Method call between .



Project structure



VSCode+ plug-in unit Rust

Execution steps

  1. Install Rust: open cmd, Input cargo Make sure the installation is correct .
  2. install wasm Compiler tools :$ cargo install wasm-pack
  3. establish rust-wasm engineering : $ cargo new –-lib wasmlib
  4. Build: $ wasm-pack build


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 :

本文为[Grape City]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考->实现->不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple