Appendix

Examples, explanations and suggestions for v8 bailout reasons. Help alinode users optimize according to CPU-Profiler prompts.

Index

Bailout reasons

Assignment to parameter in arguments object

  • Simple example

// sloppy mode only
function test(a) {
  if (arguments.length < 2) {
    a = 0;
  }
}
  • Why

    • Only occurs when reassigning parameters in a function.

  • Advices

    • You cannot reassign variable a.

    • It is better to use strict mode.

    • V8's latest TurboFan will have optimizations #1.

Bad value context for arguments value

  • Simple example

// strict & sloppy modes
function test1() {
  arguments[0] = 0;
}

// strict & sloppy modes
function test2() {
  arguments.length = 0;
}

// strict & sloppy modes
function test3() {
  return arguments;
}

// strict & sloppy modes
function test4() {
  var args = [].slice.call(arguments);
}

// strict & sloppy modes
function test5() {
  var a = arguments;
  return function() {
    return a;
  };
}
  • Why

    • Requires further specification of the arguments array.

  • Advices

    • You can read: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments

    • You can loop through arguments to create a new array Unsupported phi use of arguments

    • V8's latest TurboFan will have optimizations #1.

  • External links

    • https://github.com/bevry/taskgroup/issues/12

ForInStatement with non-local each variable

  • Simple example

// strict & sloppy modes
function test1() {
  var obj = {};
  for(key in obj);
}

// strict & sloppy modes
function key() {
  return 'a';
}
function test2() {
  var obj = {};
  for(key() in obj);
}
  • Why

    • https://github.com/yjhjstz/v8-git-mirror/blob/master/src/hydrogen.cc#L5254

  • Advices

    • Only pure local variables can be used in for...in.

    • https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#5-for-in

  • External links

    • https://github.com/mbostock/d3/pull/2686

Object literal with complex property

  • Simple example

// strict & sloppy modes
function test() {
  return {
    __proto__: 3
  };
}
  • Why

  • Advices

    • Simplify the Object.

ForInStatement is not fast case

  • Simple example

for (var prop in obj) {
  /* lots of code */
}
  • Why

    • Too much code in the for loop.

  • Advices

    • Extract the code in the for loop into a function.

Reference to a variable which requires dynamic lookup

  • Simple example

// sloppy mode only
function test() {
  with ({x:1}) {
    return x;
  }
}
  • Why

    • Compilation fails to locate the variable at compile time, and Crankshaft needs to dynamically look it up again. #3

  • Advices

    • TurboFan can optimize.

TryCatchStatement

  • Simple example

// strict & sloppy modes OR // sloppy mode only
function func() {
  return 3;
  try {} catch(e) {}
}
  • Why

    • try/catch makes the control flow unstable and difficult to optimize at runtime.

  • Advices

    • Do not use try/catch in heavily loaded functions.

    • Can be refactored as try { func() } catch

TryFinallyStatement

  • Simple example

// strict & sloppy modes OR // sloppy mode only
function func() {
  return 3;
  try {} finally {}
}

Unsupported phi use of arguments

  • 简单例子

// strict & sloppy modes
function test1() {
  var _arguments = arguments;
  if (0 === 0) { // anything evaluating to true, except a number or `true`
    _arguments = [0]; // Unsupported phi use of arguments
  }
}

// strict & sloppy modes
function test2() {
  var _arguments = arguments;
  for (var i = 0; i < 1; i++) {
    _arguments = [0]; // Unsupported phi use of arguments
  }
}

// strict & sloppy modes
function test3() {
  var _arguments = arguments;
  var again = true;
  while (again) {
    _arguments = [0]; // Unsupported phi use of arguments
    again = false;
  }
}
  • Why

    • Crankshaft cannot determine whether _arguments is an object or an array.

  • Advices

    • It is best to operate on a copy of arguments.

    • TurboFan can optimize #1.




### Yield

* Simple example

```js
// strict & sloppy modes
function* test() {
  yield 0;
}
  • Why

    • Generator state preservation and restoration is achieved by copying function stack frames, which is not suitable for optimized compilers.

  • Advices

    • Currently, there is no need to consider this issue. TurboFan can optimize.

  • External links:

    • https://groups.google.com/forum/#!topic/v8-users/KnnUb-u4rA8


Resources

Last updated