如何禁用V8的优化编译器 [英] How to Disable V8's Optimizing Compiler

查看:107
本文介绍了如何禁用V8的优化编译器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个常量字符串比较函数(用于node.js),并希望禁用V8的这个单一函数的优化编译器;使用命令行标志是不可能的。



我知道使用带有{} 的(或尝试/ catch)块会禁用优化编译器 now ,但恐怕这个功能(bug)将在未来版本中修复。






示例函数:

$ p是否存在禁用V8优化编译器的不可变(且已记录)的方法? b
$ b

 函数constantTimeStringCompare(a,b){
//通过在这里添加`with`块,我们禁用了v8的优化编译器。
//使用Object.create(null)确保我们没有任何对象原型属性以我们的方式获取。
with(Object.create(null)){
var valid = true,
length = Math.max(a.length,b.length);
while(length--){
valid& = a.charCodeAt(length)=== b.charCodeAt(length);
}
//如果有效== 1则返回true,如果有效则返回== 0
return !! valid;
}
}

perf test 只是为了好玩。

解决方案

如果你想要坚实的方法来完成它,你需要用 - allow-natives-syntax 标志运行节点并调用它:

 %NeverOptimizeFunction(constantTimeStringCompare); 

请注意,在调用 constantTimeStringCompare

否则加上语句是最好的选择,如果函数已经优化,那么这违反了断言。打赌作为优化将是绝对的疯狂,而支持 try / catch 将是合理的。你不需要它来影响你的代码,但这已经足够了:

 函数constantTimeStringCompare(a,b){
与({});

var valid = true,
length = Math.max(a.length,b.length);
while(length--){
valid& = a.charCodeAt(length)=== b.charCodeAt(length);
}
//如果有效== 1则返回true,如果有效则返回== 0
return !! valid;


$ / code>

仅仅用提及 c $ c>语句损坏了整个包含函数 - 优化是在函数级粒度上完成的,而不是按语句执行。


I'm writing a constant-time string comparison function (for node.js) and would like to disable V8's optimizing compiler for this single function; using command-line flags are out of the question.

I know that using a with{} (or try/catch) block will disable the optimizing compiler now, but I'm afraid this "feature" (bug) will be fixed in future versions.

Is there an immutable (and documented) way disabling V8's optimizing compiler?


Example function:

function constantTimeStringCompare( a, b ) {
    // By adding a `with` block here, we disable v8's optimizing compiler.
    // Using Object.create(null) ensures we don't have any object prototype properties getting in our way.our way.
    with ( Object.create( null ) ){
        var valid = true,
            length = Math.max( a.length, b.length );
        while ( length-- ) {
            valid &= a.charCodeAt( length ) === b.charCodeAt( length );
        }
        // returns true if valid == 1, false if valid == 0
        return !!valid;
    }
}

And a perf test just for fun.

解决方案

If you want solid way to do it, you need to run node with --allow-natives-syntax flag and call this:

%NeverOptimizeFunction(constantTimeStringCompare);

Note that you should call this before you have called constantTimeStringCompare, if the function is already optimized then this violates an assertion.

Otherwise with statement is your best bet as making it optimizable would be absolute lunacy whereas supporting try/catch would be reasonable. You don't need it to affect your code though, this will be sufficient:

function constantTimeStringCompare( a, b ) {
    with({});

    var valid = true,
        length = Math.max( a.length, b.length );
    while ( length-- ) {
        valid &= a.charCodeAt( length ) === b.charCodeAt( length );
    }
    // returns true if valid == 1, false if valid == 0
    return !!valid;

}

Merely mentioning with statement corrupts the entire containing function - the optimizations are done at function-level granularity, not per statement.

这篇关于如何禁用V8的优化编译器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆