8135075: Reorder short-circuit tests in ApplySpecialization to run cheapest first
authorattila
Mon, 07 Sep 2015 11:11:41 +0200
changeset 32524 bd52a0be10a5
parent 32523 d8e365854b3c
child 32525 3244ec62a5cb
8135075: Reorder short-circuit tests in ApplySpecialization to run cheapest first Reviewed-by: hannesw, mhaupt, sundar
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/ApplySpecialization.java
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/ApplySpecialization.java	Fri Sep 04 17:11:06 2015 +0530
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/ApplySpecialization.java	Mon Sep 07 11:11:41 2015 +0200
@@ -298,7 +298,28 @@
 
     @Override
     public boolean enterFunctionNode(final FunctionNode functionNode) {
-        if (!USE_APPLY2CALL) {
+        // Cheap tests first
+        if (!(
+                // is the transform globally enabled?
+                USE_APPLY2CALL
+
+                // Are we compiling lazily? We can't known the number and types of the actual parameters at
+                // the caller when compiling eagerly, so this only works with on-demand compilation.
+                && compiler.isOnDemandCompilation()
+
+                // Does the function even reference the "arguments" identifier (without redefining it)? If not,
+                // it trivially can't have an expression of form "f.apply(self, arguments)" that this transform
+                // is targeting.
+                && functionNode.needsArguments()
+
+                // Does the function have eval? If so, it can arbitrarily modify arguments so we can't touch it.
+                && !functionNode.hasEval()
+
+                // Finally, does the function declare any parameters explicitly? We don't support that. It could
+                // be done, but has some complications. Therefore only a function with no explicit parameters
+                // is considered.
+                && functionNode.getNumOfParams() == 0))
+        {
             return false;
         }
 
@@ -308,18 +329,6 @@
             return false;
         }
 
-        if (!compiler.isOnDemandCompilation()) {
-            return false;
-        }
-
-        if (functionNode.getNumOfParams() != 0) {
-            return false;
-        }
-
-        if (functionNode.hasEval()) {
-            return false;
-        }
-
         if (!hasApplies(functionNode)) {
             return false;
         }