📜 ⬆️ ⬇️

Frequently asked questions about asm.js

asm.js is an extremely optimized, low-level subset of JavaScript. - asmjs.org

asm.js - new language?


No, this is just a subset of JavaScript. The program on asm.js will behave in the same way in the existing JavaScript engines, and in the engine with a preliminary (ahead-of-time, AOT) compilation that can recognize and optimize asm.js; its speed will vary, of course!

What performance gain can you expect from asm.js?


It is still too early to assert. However, our preliminary measurements of the performance of programs compiled from C to asm.js show no more than a two-fold slowdown compared to compiled to native code by means of clang . We will publish further measurements when we collect them.

How can I follow the progress of the implementation?


Mozilla is working on the first implementation of the optimizing asm.js compiler for SpiderMonkey. The Mozilla Foundation Wiki also published a plan for developing further releases and optimizations. If the authors of other JavaScript engines publish their own implementation plans for the asm.js compilers, we will mention them here.
')

Why don't you develop bytecode syntax instead of an unusual javascript dialect?


For compilers like Emscripten or Mandreel, the syntax of bytecode language is simply not particularly significant. Moreover, most bytecodes and machine languages ​​in general have a binary format that is not readable by humans. However, we can create, at the asm.js level, a more human-readable syntax that will be convenient for disassembling and suitable for people to read and write.

The fact that asm.js is JavaScript doesn’t turn into unpredictable code execution?


The preliminary (ahead-of-time, AOT) compilation of asm.js can generate code that is highly predictable because the valid code of asm.js is limited to a very small subset of JavaScript, consisting only of strongly typed integers, floating point numbers, arithmetic operations, function calls, and heap calls.

Why not NaCl or PNaCl instead? Are you just persisting about javascript?


The main advantage of asm.js compared to new technologies like NaCl and PNaCl is that asm.js is working today: the existing JavaScript engines already optimize well the code written in this style. Which means that developers can release code on asm.js today, and over time its work will accelerate. Another important benefit is the noticeably greater ease of implementation, which will require very few additional mechanisms on top of existing JavaScript engines - and will not require an API compatibility layer.

Why then not to continue optimizing JIT JavaScript compilers instead?


And there is no need to stop her! However, the behavior of JIT compilers is less predictable, being based on complex heuristics. The asm.js model is more similar to the C or C ++ model , saving the language from dynamic typing, wrapped values, and garbage collection.

Isn't it inefficient to run the code through the JavaScript interpreter, and then through the compiler?


The prologue directive allows the JavaScript engine to immediately recognize the asm.js code at compile time and immediately translate it into assembly language. Such code does not get into the interpreter at all.

What feedback is provided for developers in case their code fails validation?


The prologue directive indicates the developer’s intention to consider the following code asm.js valid. If the code fails validation, the engine can inform the developer about the error, for example, in the debugging console, or by other means.

How can developers debug asm.js code?


In general, this problem has not yet been solved in relation to languages ​​compiled for WWW. Source maps can help with the cause, but browsers still have a lot of work to improve the debugging processes of the compiled code.

Can asm.js be a virtual machine for managed languages, like a JVM or CLR?


Currently, the asm.js code does not have direct access to the data subject to garbage collection: the asm.js program can access external data only indirectly, using their numerical identifiers. In future versions, we intend to implement garbage collection and structured data based on the ES6 structured binary data API , which will increase the appeal of asm.js as a means of executing managed languages.

Is asm.js compatible with eval and function calls? Is it possible to dynamically run the asm.js compiler?


Of course! The asm.js module is compiled during the processing of its source code. If you start this processing by calling eval or Function , then get dynamic compilation.

How big are the costs caused by the asm.js compilation time?


Validation and compilation occur fairly quickly, but it still leads to some costs, although only in relation to the asm.js code: if the JavaScript code does not contain the prolog asm.js directive , then it does not undergo any additional processing. If you want to avoid the delay associated with the immediate validation and compilation of asm.js code, you can later use eval or Function and postpone compilation until that time.

Can asm.js have a beneficial effect on the launch time of applications?


We plan to offer additional APIs that will allow web developers to compile asm.js in the background, as well as save the compilation results to an offline repository, which will speed up subsequent initial launches of the application.

Source: https://habr.com/ru/post/171561/


All Articles