src/com/google/javascript/jscomp/FunctionInjector.java
/**
   * Determine if inlining the function is likely to reduce the code size.
   * @param namesToAlias
   */
Determine if inlining the function is likely to reduce the code size.
Determine if inlining the function is likely to reduce the code size .
boolean inliningLowersCost(
      JSModule fnModule, Node fnNode, Collection<? extends Reference> refs,
      Set<String> namesToAlias, boolean isRemovable, boolean referencesThis) {
    int referenceCount = refs.size();
    if (referenceCount == 0) {
      return true;
    }
    int referencesUsingBlockInlining = 0;
    boolean checkModules = isRemovable && fnModule != null;
    JSModuleGraph moduleGraph = compiler.getModuleGraph();
    for (Reference ref : refs) {
      if (ref.mode == InliningMode.BLOCK) {
        referencesUsingBlockInlining++;
      }
      if (checkModules && ref.module != null) {
        if (ref.module != fnModule && !moduleGraph.dependsOn(ref.module, fnModule)) {
          isRemovable = false;
          checkModules = false;  
        }
      }
    }
    int referencesUsingDirectInlining = referenceCount - referencesUsingBlockInlining;
    if (referenceCount == 1 && isRemovable && referencesUsingDirectInlining == 1) {
      return true;
    }
    int callCost = estimateCallCost(fnNode, referencesThis);
    int overallCallCost = callCost * referenceCount;
    int costDeltaDirect = inlineCostDelta(fnNode, namesToAlias, InliningMode.DIRECT);
    int costDeltaBlock = inlineCostDelta(fnNode, namesToAlias, InliningMode.BLOCK);
    return doesLowerCost(fnNode, overallCallCost,
        referencesUsingDirectInlining, costDeltaDirect,
        referencesUsingBlockInlining, costDeltaBlock,
        isRemovable);
  }
boolean inliningLowersCost ( JSModule fnModule , Node fnNode , Collection < ? extends Reference > refs , Set < String > namesToAlias , boolean isRemovable , boolean referencesThis ) { int referenceCount = refs . size ( ) ; if ( referenceCount == 0 ) { return true ; } int referencesUsingBlockInlining = 0 ; boolean checkModules = isRemovable && fnModule != null ; JSModuleGraph moduleGraph = compiler . getModuleGraph ( ) ; for ( Reference ref : refs ) { if ( ref . mode == InliningMode . BLOCK ) { referencesUsingBlockInlining ++ ; } if ( checkModules && ref . module != null ) { if ( ref . module != fnModule && ! moduleGraph . dependsOn ( ref . module , fnModule ) ) { isRemovable = false ; checkModules = false ; } } } int referencesUsingDirectInlining = referenceCount - referencesUsingBlockInlining ; if ( referenceCount == 1 && isRemovable && referencesUsingDirectInlining == 1 ) { return true ; } int callCost = estimateCallCost ( fnNode , referencesThis ) ; int overallCallCost = callCost * referenceCount ; int costDeltaDirect = inlineCostDelta ( fnNode , namesToAlias , InliningMode . DIRECT ) ; int costDeltaBlock = inlineCostDelta ( fnNode , namesToAlias , InliningMode . BLOCK ) ; return doesLowerCost ( fnNode , overallCallCost , referencesUsingDirectInlining , costDeltaDirect , referencesUsingBlockInlining , costDeltaBlock , isRemovable ) ; }
Determine if inlining the function is likely to reduce the code size.
Determine if inlining the function is likely to reduce the code size .
// Check if any of the references cross the module boundaries. // Calculate the cost as if the function were non-removable, // if it still lowers the cost inline it. // no need to check additional modules. // Don't bother calculating the cost of function for simple functions where // possible. // However, when inlining a complex function, even a single reference may be // larger than the original function if there are many returns (resulting // in additional assignments) or many parameters that need to be aliased // so use the cost estimating.