src/com/google/javascript/jscomp/FunctionInjector.java
/**
   * @return The difference between the function definition cost and
   *     inline cost.
   */
@return The difference between the function definition cost and
inline cost.
@return The difference between the function definition cost and inline cost .
private static int inlineCostDelta(
      Node fnNode, Set<String> namesToAlias, InliningMode mode) {
    int paramCount = NodeUtil.getFunctionParameters(fnNode).getChildCount();
    int commaCount = (paramCount > 1) ? paramCount - 1 : 0;
    int costDeltaFunctionOverhead =
        15 + commaCount + (paramCount * InlineCostEstimator.ESTIMATED_IDENTIFIER_COST);
    Node block = fnNode.getLastChild();
    if (!block.hasChildren()) {
      return -costDeltaFunctionOverhead;
    }
    if (mode == InliningMode.DIRECT) {
      return -(costDeltaFunctionOverhead + 7);
    } else {
      int aliasCount = namesToAlias.size();
      final int inlineBlockOverhead = 4; 
      final int perReturnOverhead = 2;   
      final int perReturnResultOverhead = 3; 
      final int perAliasOverhead = 3; 
      int returnCount = NodeUtil.getNodeTypeReferenceCount(
          block, Token.RETURN, new NodeUtil.MatchShallowStatement());
      int resultCount = (returnCount > 0) ? returnCount - 1 : 0;
      int baseOverhead = (returnCount > 0) ? inlineBlockOverhead : 0;
      int overhead = baseOverhead
          + returnCount * perReturnOverhead
          + resultCount * perReturnResultOverhead
          + aliasCount * perAliasOverhead;
      return (overhead - costDeltaFunctionOverhead);
    }
  }
private static int inlineCostDelta ( Node fnNode , Set < String > namesToAlias , InliningMode mode ) { int paramCount = NodeUtil . getFunctionParameters ( fnNode ) . getChildCount ( ) ; int commaCount = ( paramCount > 1 ) ? paramCount - 1 : 0 ; int costDeltaFunctionOverhead = 15 + commaCount + ( paramCount * InlineCostEstimator . ESTIMATED_IDENTIFIER_COST ) ; Node block = fnNode . getLastChild ( ) ; if ( ! block . hasChildren ( ) ) { return - costDeltaFunctionOverhead ; } if ( mode == InliningMode . DIRECT ) { return - ( costDeltaFunctionOverhead + 7 ) ; } else { int aliasCount = namesToAlias . size ( ) ; final int inlineBlockOverhead = 4 ; final int perReturnOverhead = 2 ; final int perReturnResultOverhead = 3 ; final int perAliasOverhead = 3 ; int returnCount = NodeUtil . getNodeTypeReferenceCount ( block , Token . RETURN , new NodeUtil . MatchShallowStatement ( ) ) ; int resultCount = ( returnCount > 0 ) ? returnCount - 1 : 0 ; int baseOverhead = ( returnCount > 0 ) ? inlineBlockOverhead : 0 ; int overhead = baseOverhead + returnCount * perReturnOverhead + resultCount * perReturnResultOverhead + aliasCount * perAliasOverhead ; return ( overhead - costDeltaFunctionOverhead ) ; } }
@return The difference between the function definition cost and
     inline cost.
@return The difference between the function definition cost and inline cost .
// The part of the function that is never inlined: //    "function xx(xx,xx){}" (15 + (param count * 3) -1; // Assume the inline cost is zero for empty functions. // The part of the function that is inlined using direct inlining: //    "return " (7) // Originally, we estimated purely base on the function code size, relying // on later optimizations. But that did not produce good results, so here // we try to estimate the something closer to the actual inlined coded. // NOTE 1: Result overhead is only if there is an assignment, but // getting that information would require some refactoring. // NOTE 2: The aliasing overhead is currently an under-estimate, // as some parameters are aliased because of the parameters used. // Perhaps we should just assume all parameters will be aliased? // "X:{}" // "return" --> "break X" // "XX=" // "XX=" // TODO(johnlenz): Counting the number of returns is relatively expensive. //   This information should be determined during the traversal and cached.