2

I'm reading this article on optimization techniques and there's the following code:

//Function that contains the pattern to be inspected (using an `eval` statement)
function exampleFunction() {
    return 3;
    eval('');
}

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

//Fill type-info
exampleFunction();
// 2 calls are needed to go from uninitialized -> pre-monomorphic -> monomorphic
exampleFunction();

%OptimizeFunctionOnNextCall(exampleFunction);
//The next call
exampleFunction();

//Check
printStatus(exampleFunction);

I have two questions:

  • The functions %GetOptimizationStatus and %OptimizeFunctionOnNextCall are never defined, where do they come from?

  • What does this phrase mean 2 calls are needed to go from uninitialized -> pre-monomorphic -> monomorphic ?

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488

1 Answers1

2

%GetOptimizationStatus and %OptimizeFunctionOnNextCall are injected into Node, when the specified optimization flags are set.

And the monomorphic part is basically saying the optimiser won't kick in until it detects multi-use,.. After this TurboFan is activated.

IOW: if you look at the output you will notice it call's exampleFunction twice (with optimisation disabled), and it's only on the third try does the TurboFan kick in.

John Weisz
  • 30,137
  • 13
  • 89
  • 132
Keith
  • 22,005
  • 2
  • 27
  • 44