当前位置:网站首页>Get to know webassembly quickly
Get to know webassembly quickly
2022-06-23 06:40:00 【devpoint】

WebAssembly namely Wasm Just like an efficient low-level programming language for the browser runtime , It can compile high-level languages into binary format , And in WEB Run them on , Without a browser or plug-in .
Why WebAssembly
Although modern JavaScript Is a fairly fast language , But it is unpredictable , Inconsistent implementation . and , It is not a good compilation target . Utility plug-ins are quite unfriendly and infeasible to the user experience , Such as Elm、Dart Wait for a new language to compile the scripting language . Google and Mozilla Created Native Client and asm.js To meet the needs of complex applications . However , All these attempts failed to resolve WEB The problem of safe and fast code on .
WebAssembly The first is in the 2015 Year by year JavaScript Creator Brendan Eich Proposed . Following JavaScript after , It was the first language to gain universal support . World wide web consortium (W3C) stay 2017 Developed in WebAssembly, Goals at the time :
- Fast , Efficiency and platform : It uses common hardware functions to execute near native code performance on different platforms . Compact code can be decoded in one pass 、 Verify and compile , Just like instant or early compilation .
- Readable and debuggable : Well defined code , Convenient for inspection and commissioning , Programs can be broken down into modules that can work independently ( Smaller parts ).
- decode 、 Validation and compilation are streaming ( Start before you see all the data ) And parallelizable ( It can be split into many independent parallel tasks ).
- Don't destroy WEB, Keep backward compatibility .
WebAssembly Definition
Wasm Is based on the stack of virtual machine binary instruction format . Wasm Designed to be portable , Used to compile C/C++ or Rust Wait for advanced language , Support in Web Deploy client and server applications .
- Binary instruction format : Text formatted WebAssembly The code is used to define the abstract syntax tree , It describes the components of the program and simplifies verification and compilation .AST Compiled ( Use Emscripten SDK Wait for the compiler ) In binary format
.wasmfile , The file is loaded into the web page . Browser's JavaScript The engine then uses the decoding stack to decode the file back AST To explain it . - Portable target for compilation : because WebAssembly Designed to run on virtual machines ; Regardless of the operating system and instruction set architecture , It can run on various devices .
WebAssembly advantage
WebAssembly Is rapidly becoming a mainstream technology , It is adopted by all major browser vendors , Especially because of the near native code performance . besides WebAssembly It also has the following advantages :
- Better performance :WebAssembly Provides enhanced performance in two ways , That is, startup speed and throughput . Smaller JavaScript Need to be parsed 、 explain 、 Compile and optimize . On the other hand ,
wasmMore compact , And because of its simple design , The binary format allows for faster parsing and fast optimization . - Portable and safe : It's platform independent 、 Hardware independent and language independent , It has no special requirements for devices or browsers , This enhances its portability , The code is validated and executed in a memory safe sandbox environment , It can prevent security vulnerabilities and data corruption .
- Integrate legacy libraries : If the application uses
C/C++Or any other compatible language ,WebAssembly You can easily make code or desktop applications available to Web. Two libraries are usually used ; be used for Rust Ofwasm-packAnd forC/C++OfEmscripten.
WebAssembly Application scenarios
WebAssembly Usually used for computing intensive applications that require high performance . These include metacosmic related technologies AR/VR Real time development 、 Video editing 、VPN、 Image recognition, etc . To understand learning wasm All kinds of technology , please Click here .
- stay
tensorflow.jsAddwasmAfter backend support , The performance of the model is improved 10 About times . - Because it was originally used
C++Compiling , thereforeFigmaUseEmscriptenExport toAsm.js, By adding the appropriateEmscriptenSign activated WebAssembly after , Probably 3 Times performance improvement . - Switch to WebAssembly after ,
OpenCV PythonThe performance improvement of the library is very obvious .ResNet50The reasoning time has increased 15 times , The speed of kernel performance test has been improved 3.5 times . - Unity Use Emscripten Output WebAssembly Export a web player for a game , Traditionally due to JavaScript The speed is too slow to export to Web The game of , stay Web Consistently good performance on .
After being widely supported by browsers , Some heavyweight applications are gradually transplanted to Web On , Include :
- Google Earth: One mainly presents the earth based on satellite images 3D Represents the software .
- AutoCAD: Computer aided design and drawing software applications , It is a desktop application running on a microcomputer with an internal graphics controller .
- TensorFlow: Free open source software library for machine learning and artificial intelligence .
WebAssembly Realization
Depending on the language used , There are four ways to Web Implemented in the application WebAssembly.
- Use Emscripten transplant
C/C++Applications . - Write or build directly at the assembly level WebAssembly
- Write a Rust Application and will WebAssembly As its output .
- Use to compile as WebAssembly Binary files AssemblyScript.

Embed... In the application WebAssembly
As shown in the figure below , Whether it's Web Application or non application Web application , Need to be embedded in the host program WebAssembly Can only be used at runtime WebAssembly. The only difference is , stay Web In the application , The host program is the browser , But not right Web Scene , The host program is its own application , Specific to the back-end application that this article focuses on , The host program is a back-end service .

Currently available WebAssembly The runtime includes Wasmtime、WasmEdge、WAVM、Wasmer etc. , Each has its own advantages and disadvantages .
WebAssembly limitations
Although new functions are constantly developed , but WebAssembly Its function is limited .
- No garbage collection : With garbage collection JavaScript Different ,
WasmUse a flat surface / Linear memory model , Allocate a large amount of memory during instantiation and do not automatically reclaim memory . - No direct access DOM:WebAssembly Unable to access document object model (DOM), whatever DOM All operations need to use JavaScript Indirectly complete . perhaps , Through JavaScript Glue code complete DOM In case of operation , You can also use any tool chain , for example Emscripten. Performance depends on the library used .
- Old browsers don't support : Usually older browsers don't have the ability to instantiate and load
WasmRequired objects for the module .

边栏推荐
- C language obtains second, millisecond, subtle and nanosecond timestamps
- 光谱共焦的测量原理及厚度测量模式
- Give up Visio, this drawing tool is really fragrant!
- For non dpdk kvm1.0 machines, set init Maxrxbuffers changed from 256 to 1024 to improve packet receiving capacity
- Day_ 08 smart health project - mobile terminal development - physical examination appointment
- Day_ 04 smart health project - appointment management - package management
- json转化为proto
- Day_ 05 smart communication health project - appointment management - appointment settings
- 【已解决】“The Unity environment took too long to respond. Make sure that :\n“
- 中台库存中的实仓与虚仓的业务逻辑设计
猜你喜欢
随机推荐
haas506 2.0开发教程-高级组件库-modem.net(仅支持2.2以上版本)
Qt 中 QVariant 使用总结
A review: neural oscillation and brain stimulation in Alzheimer's disease
Day_ 07 smart communication health project FreeMarker
js创建数组(元素都是对象)
百度URL参数之LINK?URL参数加密解密研究(代码实例)
Day_ 10 smart health project - permission control, graphic report
Home address exchange
什么是PDCA循环?如何整合 PDCA 循环和 OKR
Xray linkage crawlergo automatic scanning pit climbing record
数值计算方法 Chapter7. 计算矩阵的特征值和特征向量
2020 smart power plant industry insight white paper
Topic35——34. Find the first and last positions of elements in a sorted array
Measurement principle and thickness measurement mode of spectral confocal
Day_02 传智健康项目-预约管理-检查项管理
Easy EDA #学习笔记09# | ESP32-WROOM-32E模组ESP32-DevKitC-V4开发板 一键下载电路
Link of Baidu URL parameter? Research on URL parameter encryption and decryption (code example)
[resolved] "the unity environment took too long to respond. make sure that: \n“
【踩坑记录】数据库连接未关闭连接,释放资源的坑
How to build a data application system based on overall value for energy enterprises









