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

Question

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


Show source
| javascript   | node.js   | optimization   | v8   2017-10-14 23:10 2 Answers

Answers to What happens to v8 status code on optimization / function run? ( 2 )

  1. 2017-10-15 01:10

    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 << 5) | (1 << 0) ~which means~ "kIsFunction" and "kTurboFanned".

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

  2. 2017-10-19 18:10

    %GetOptimizationStatus has been updated to return a set of flags instead of a single value, and the article Optimization Killers is outdated. The available status information has also been slightly changed.

    The following is the list of available status flags as of now, and how they are to be used:

    var status = %GetOptimizationStatus(fn);
    
    if (status & 2) console.log("function is never optimized");
    if (status & 4) console.log("function is always optimized");
    if (status & 8) console.log("function is maybe deoptimized");
    if (status & 16) console.log("function is optimized");
    if (status & 32) console.log("function is optimized by TurboFan");
    if (status & 64) console.log("function is interpreted");
    

    There is also status & 1 which is truthy if the function is a function. The key aspect here is that more than one of these conditions can evaluate to true, such as in the case of 17, as explained in the other answer.

Leave a reply to - What happens to v8 status code on optimization / function run?

◀ Go back