Why does asm.js deteriorate performance?

前端 未结 2 913
旧巷少年郎
旧巷少年郎 2021-01-31 02:41

Just to see how it performs, I wrote a very short asm.js module by hand, which simulates the 2D wave equation using 32-bit integer math and typed arrays (Int32Array). I have thr

相关标签:
2条回答
  • 2021-01-31 03:11

    Actually asm.js has not been created to write code by hand but only as result of a compilation from other languages. As far as I know there are no tools that validate the asm.js code. Have you tried to write the code in C lang and use Emscripten to generate the asm.js code? I strongly suspect that the result would be quite different and optimized for asm.js.

    I think that mixing typed and untyped vars you only add complexity without any benefits. On the contrary the "asm.js" code is more complex: I tried to parse the asm.js and the plain functions on jointjs.com/demos/javascript-ast and the results are:

    • the plain js function has 137 nodes and 746 tokens
    • the asm.js function has 235 nodes and 1252 tokens

    I would say that if you have more instructions to execute in each loop it easily will be slower.

    0 讨论(0)
  • 2021-01-31 03:22

    There is some fix cost to switching asm.js contexts. Ideally you do it once and run all of your code within your app as asm.js. Then you can control memory management using typed arrays and avoid lots of garbage collections. I'd suggest to rewrite the profiler and measure asm.js within asm.js - without context switching.

    0 讨论(0)
提交回复
热议问题