Search code examples
javascriptnode.jsoptimizationv8

What happens to v8 status code on optimization / function run?


I saw a question about v8 Optimization which led me to play a bit with v8 Optimization. I've also seen bluebird post about v8 Optimization killers.

According to v8 repo, optimization status codes are in multiplications of 2: 1,2,4 ,8 and so on (see OptimizationStatus enum )

However, the following code gave me strange status codes like 17 and 65, and only in these specific cases (see the last few lines of code). Any ideas about why this is happening?

function adder(a, b) {
    return new Function('a', 'b', 'return b%2 ? a + b : b%3 ? a - b : b%5 ? b / a : a * b')(a, b);
}
function addereval(a, b) {
    return eval('b%2 ? a + b : b%3 ? a - b : b%5 ? b / a : a * b');
}

function printStatus(fn) {
    var status = %GetOptimizationStatus(fn)
    switch (status) {
        case 1: console.log(fn.name, "function is optimized"); break;
        case 2: console.log(fn.name, "function is not optimized"); break;
        case 3: console.log(fn.name, "function is always optimized"); break;
        case 4: console.log(fn.name, "function is never optimized"); break;
        case 6: console.log(fn.name, "function is maybe deoptimized"); break;
        case 7: console.log(fn.name,"Function is optimized by TurboFan"); break;
        default: console.log(fn.name, "Unknown optimization status: ", status); break;
    }
}
printStatus(adder);
printStatus(addereval);


for(let i = 0; i < 263; i++) {
    adder(1, 2);
}
console.log('\n', '==== adder after invocation - result is on node v8.2.1 17 or 65 on node v8.7.0 ===');
printStatus(adder);

addereval(1, 2);
console.log('\n', '==== addereval after invocation - result is 65 ===');
printStatus(addereval);

Run this code with:

node --trace_deopt --allow-natives-syntax FILENAME.js

You can use my gist if you find it more comfortable


Solution

  • status is a bitwise flag value and the code should look more like this:

    var status = GetOptimizationStatus(fn);
    if ((status & (1 << 0)) {
      console.log(fn.name, "kIsFunction");
    }
    if ((status & (1 << 1)) {
      console.log(fn.name, "kNeverOptimize");
    }
    // etc .. can be 'true' for several different combinations;
    // most notably, many different status will also include 'kIsFunction'
    

    Consider a "status code" of 17 ~or~ 16 + 1 ~or~ (1 << 4) | (1 << 0) ~which means~ "kIsFunction" and "kIsOptimized".

    See the bit arrays for general manipulation - and why the use of & in the conditions shown in the code.