Cloudflare, Mozilla and Facebook develop BinaryAST to speed up JavaScript loading

Engineers from Cloudflare, Mozilla, Facebook and Bloomberg proposed new format binaryAST to speed up the delivery and processing of JavaScript code when opening sites in a browser. BinaryAST takes the parsing phase to the server side and supplies an already formed abstract syntax tree (AST). Upon receiving BinaryAST, the browser can immediately go to the compilation stage, bypassing the parsing of the JavaScript source code.

For testing prepared by reference implementation supplied under the MIT license. For parsing, Node.js components are used, and the code for optimizing and generating AST is written in Rust. Browser side support
BinaryAST is already available in night assembly Firefox. The encoder in BinaryAST can be used both at the toolkit level of the final site, and for packaging scripts of external sites on the side of a proxy or content delivery network. At present, the process of standardization of BinaryAST by the working group has already begun. ECMA TC39, after which the format can coexist with existing methods of compressing the given content, such as gzip and brotli.

Cloudflare, Mozilla and Facebook develop BinaryAST to speed up JavaScript loading

Cloudflare, Mozilla and Facebook develop BinaryAST to speed up JavaScript loading

A significant amount of time in JavaScript processing is taken up by the loading and parsing phase of the code. Considering that the amount of JavaScript downloaded on many popular sites is approaching 10 MB (for example, for LinkedIn - 7.2 MB, Facebook - 7.1 MB, Gmail - 3.9 MB), the initial processing of JavaScript introduces a significant delay. The parsing stage on the browser side is also slowed down due to the impossibility of fully building the AST on the fly as the code is loaded (the browser has to wait for code blocks to finish loading, for example, the end of functions, in order to get the information missing to parse the current elements).

In part, they try to solve the problem by distributing the code in a minimized and compressed form, as well as by caching the generated bytecode by the browser. On modern sites, the code is updated quite often, so caching only partially solves the problem. WebAssembly could be the solution, but it requires the use of explicit typing in the code and is not well suited for speeding up the processing of already existing JavaScript code.

Another option is to ship pre-compiled bytecode instead of JavaScript scripts, but browser engine developers are opposed because third-party bytecode is difficult to verify, its direct processing can lead to Web layering, additional security risks arise, and the development of a universal bytecode format is required.

BinaryAST allows you to fit into the current code development and delivery model without creating new bytecode and without changing the JavaScript language. The size of the data in the BinaryAST format is comparable to the compressed minified JavaScript code, and the processing speed is noticeably increased due to the exclusion of the source code parsing phase. In addition, the format allows compilation to bytecode as BinaryAST is loaded, without waiting for all the data to be received. In addition, parsing on the server side allows you to exclude unused functions and extra code from the returned BinaryAST representation, which, when parsed on the browser side, takes time both to parse and to transmit extra traffic.

A feature of BinaryAST is also the ability to restore readable JavaScript that does not match the original version, but is semantically equivalent and includes the same names of variables and functions (BinaryAST saves names, but does not save information about positions in the code, formatting and comments). The other side of the coin is the emergence of new attack vectors, but according to the developers, they are much smaller and more controllable than alternatives such as bytecode distribution.

Facebook.com code tests have shown that 10-15% of CPU resources are spent on JavaScript parsing and parsing takes more time than bytecode generation and initial code generation for JIT. In the SpiderMonkey engine, the time to fully build an AST takes 500-800 ms, and the use of BinaryAST reduced this figure by 70-90%.
In general, for most web fireworks, when using BinaryAST, JavaScript parsing time is reduced by 3-10% in the mode without optimization and by 90-97% when the mode of ignoring unused functions is enabled.
When executing a 1.2 MB JavaScript test suite, using BinaryAST accelerated the startup start time from 338 to 314 ms on a desktop system (Intel i7) and from 2019 to 1455 ms on a mobile device (HTC One M8).

Source: opennet.ru

Add a comment