Asm.js效能逼近原生碼

來源:互聯網
上載者:User

Asm.js是JavaScript的高效能子集,簡化特性便於最佳化。Mozilla現在宣布,asm.js效能進一步逼近原生,asm.js的速度已達到原生碼的三分之二, 而以前只能達到二分之一甚至更低。Mozilla是通過改變浮點演算法處理方式改進asm.js效能。

JavaScript預設使用float64資料類型,以提供最大的資料精度,但相比範圍較小的int、int32、float和float32數 據類型,float64的效率並不高,Mozilla的 SpiderMonkey引擎在asm.js中加入了float32資料類型,允許將C/C++程式中的float32演算法直接翻譯到asm.js的 float32演算法。

Mozilla相信asm.js的效能還能進一步提升。

asm.js 是一個底層、格外為 JavaScript 子集最佳化的編譯器。這是 Mozilla 研究項目,類似 Emscripten, Mandreel, 和 LLJS.

範例程式碼:

 
  1. function mymodule(global, foreign, buffer) { 
  2.     "use asm"; 
  3.  
  4.     // ------------------------------------------------------------------------- 
  5.     // SECTION 1: imports 
  6.  
  7.     var H32 = new global.Int32Array(buffer); 
  8.     var HU32 = new global.Uint32Array(buffer); 
  9.     var log = foreign.consoleDotLog; 
  10.  
  11.     // ------------------------------------------------------------------------- 
  12.     // SECTION 2: functions 
  13.  
  14.     function f(x, y, z, w) { 
  15.         // SECTION A: parameter type declarations 
  16.         x = x|0;      // int parameter 
  17.         y = +y;       // double parameter 
  18.  
  19.         // SECTION B: function body 
  20.         log(x|0);     // call into FFI -- must force the sign 
  21.         log(y);       // call into FFI -- already know it's a double 
  22.         x = (x+3)|0;  // signed addition 
  23.  
  24.         // SECTION C: unconditional return 
  25.         return ((((x+1)|0)>>>0)/(x|0))>>>0; // compound expression 
  26.     } 
  27.  
  28.     function g() { 
  29.         g_f = +g_i; // read/write globals 
  30.         return; 
  31.     } 
  32.  
  33.     function g2() { 
  34.         return; 
  35.     } 
  36.  
  37.     function h(i, x) { 
  38.         i = i|0; 
  39.         x = x|0; 
  40.         H32[(i&0xffffffff)>>4] = x; // masked by 2^k-1, shifted by byte count 
  41.         ftable_2[(x-2)&2]();        // dynamic call of functions in table 2 
  42.     } 
  43.  
  44.     // ------------------------------------------------------------------------- 
  45.     // SECTION 3: function tables 
  46.  
  47.     var ftable_1 = [f]; 
  48.     var ftable_2 = [g, g2]; // all of the same type 
  49.  
  50.     // ------------------------------------------------------------------------- 
  51.     // SECTION 4: globals 
  52.  
  53.     var g_i = 0;   // int global 
  54.     var g_f = 0.0; // double global 
  55.  
  56.     // ------------------------------------------------------------------------- 
  57.     // SECTION 5: exports 
  58.  
  59.     return { f_export: f, goop: g }; 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.