本帖最后由 lisliz 于 2021-1-9 10:38 编辑
js里也有类似的技术,V8的snapshot。
看来像js和ruby的常规模式都是这样【分析代码->生成语法树->字节码->执行】
但js的V8引擎还会在运行时尝试把热点代码编译成【特定于CPU架构的本地代码(机器码)】,让代码跑得更快。
此外js里还有还有AOT模式,就是代码一加载就预先编译成特定于CPU架构的本地代码(机器码)的模式。
即为【分析代码->机器码->执行】,因为V8引擎有这种强大的特点,我个人倒是觉得js的运行效率比ruby更快。
例如下面一段代码在js里就是AOT模式。
function PhysicsASM(stdlib, foreign, heap) { "use asm"; var HEAP = new stdlib.Float64Array(heap); function vectorProduct(x1, y1, x2, y2) { x1 = +x1; y1 = +y1; x2 = +x2; y2 = +y2; return +(+(x1 * y2) - +(x2 * y1)); } return { vectorProduct:vectorProduct } }
function PhysicsASM(stdlib, foreign, heap) {
"use asm";
var HEAP = new stdlib.Float64Array(heap);
function vectorProduct(x1, y1, x2, y2) {
x1 = +x1; y1 = +y1; x2 = +x2; y2 = +y2;
return +(+(x1 * y2) - +(x2 * y1));
}
return {
vectorProduct:vectorProduct
}
}
运行游戏,打印这个函数,你会发现函数体已经变成了[ native code ],已经被AOT模式编译成机器码。
|