changeset 1097:f34af7107054

7901267: @Setup/@TearDown(Thread) synthetic code does useless flag checking
author shade
date Wed, 21 Jan 2015 18:54:27 +0300
parents 7098caa2753f
children 955ecf29330a
files jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGenerator.java jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java
diffstat 2 files changed, 27 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGenerator.java	Tue Jan 20 19:13:35 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGenerator.java	Wed Jan 21 18:54:27 2015 +0300
@@ -1053,23 +1053,20 @@
     }
 
     private void invocationProlog(PrintWriter writer, int prefix, MethodInfo method, StateObjectHandler states, boolean pauseMeasurement) {
-        if (!states.getInvocationSetups(method).isEmpty()) {
+        if (states.hasInvocationStubs(method)) {
             for (String s : states.getInvocationSetups(method))
                 writer.println(ident(prefix) + s);
             if (pauseMeasurement)
                 writer.println(ident(prefix) + "long rt = System.nanoTime();");
-            writer.println();
         }
     }
 
     private void invocationEpilog(PrintWriter writer, int prefix, MethodInfo method, StateObjectHandler states, boolean pauseMeasurement) {
-        if (!states.getInvocationTearDowns(method).isEmpty()) {
-            writer.println();
+        if (states.hasInvocationStubs(method)) {
             if (pauseMeasurement)
                 writer.println(ident(prefix) + "realTime += (System.nanoTime() - rt);");
             for (String s : states.getInvocationTearDowns(method))
                 writer.println(ident(prefix) + s);
-            writer.println();
         }
     }
 
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java	Tue Jan 20 19:13:35 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java	Wed Jan 21 18:54:27 2015 +0300
@@ -438,15 +438,12 @@
             if (so.scope != Scope.Thread) continue;
 
             if (type == HelperType.SETUP) {
-                result.add("if (!" + so.localIdentifier + ".ready" + helperLevel + ") {");
                 for (HelperMethodInvocation mi : so.getHelpers()) {
                     if (mi.helperLevel == helperLevel && mi.type == HelperType.SETUP) {
                         Collection<StateObject> args = stateHelperArgs.get(mi.method.getQualifiedName());
-                        result.add("    " + so.localIdentifier + "." + mi.method.getName() + "(" + getArgList(args) + ");");
+                        result.add(so.localIdentifier + "." + mi.method.getName() + "(" + getArgList(args) + ");");
                     }
                 }
-                result.add("    " + so.localIdentifier + ".ready" + helperLevel + " = true;");
-                result.add("}");
             }
         }
 
@@ -454,15 +451,12 @@
             if (so.scope != Scope.Thread) continue;
 
             if (type == HelperType.TEARDOWN) {
-                result.add("if (" + so.localIdentifier + ".ready" + helperLevel + ") {");
                 for (HelperMethodInvocation mi : so.getHelpers()) {
                     if (mi.helperLevel == helperLevel && mi.type == HelperType.TEARDOWN) {
                         Collection<StateObject> args = stateHelperArgs.get(mi.method.getQualifiedName());
-                        result.add("    " + so.localIdentifier + "." + mi.method.getName() + "(" + getArgList(args) + ");");
+                        result.add(so.localIdentifier + "." + mi.method.getName() + "(" + getArgList(args) + ");");
                     }
                 }
-                result.add("    " + so.localIdentifier + ".ready" + helperLevel + " = false;");
-                result.add("}");
             }
         }
 
@@ -516,6 +510,10 @@
         return result;
     }
 
+    public boolean hasInvocationStubs(MethodInfo method) {
+        return !getInvocationSetups(method).isEmpty() || !getInvocationTearDowns(method).isEmpty();
+    }
+
     public Collection<String> getInvocationSetups(MethodInfo method) {
         return getHelperBlock(method, Level.Invocation, HelperType.SETUP);
     }
@@ -586,21 +584,28 @@
             result.add("");
             result.add(so.type + " _jmh_tryInit_" + so.fieldIdentifier + "(InfraControl control, ThreadParams threadParams" + soDependency_TypeArgs(so) + ") throws Throwable {");
 
-            // These classes are copying the external environment.
+            // These special classes are copying the external environment.
             if (so.userType.equals(BenchmarkParams.class.getCanonicalName())) {
-                result.add("    if (true) {");
-                result.add("        " + so.type + " val = new " + so.type + "(control.benchmarkParams);");
+                result.add("    " + so.fieldIdentifier + " = new " + so.type + "(control.benchmarkParams);");
+                result.add("    return " + so.fieldIdentifier + ";");
+                result.add("}");
+                continue;
             } else if (so.userType.equals(IterationParams.class.getCanonicalName())) {
-                result.add("    if (true) {");
-                result.add("        " + so.type + " val = new " + so.type + "(control.iterationParams);");
+                result.add("    " + so.fieldIdentifier + " = new " + so.type + "(control.iterationParams);");
+                result.add("    return " + so.fieldIdentifier + ";");
+                result.add("}");
+                continue;
             } else if (so.userType.equals(ThreadParams.class.getCanonicalName())) {
-                result.add("    if (true) {");
-                result.add("        " + so.type + " val = new " + so.type + "(threadParams);");
-            } else {
-                result.add("    if (" + so.fieldIdentifier + " == null) {");
-                result.add("        " + so.type + " val = new " + so.type + "();");
+                result.add("    " + so.fieldIdentifier + " = new " + so.type + "(threadParams);");
+                result.add("    return " + so.fieldIdentifier + ";");
+                result.add("}");
+                continue;
             }
 
+            result.add("    " + so.type + " val = " + so.fieldIdentifier + ";");
+            result.add("    if (val == null) {");
+            result.add("        val = new " + so.type + "();");
+
             if (!so.getParamsLabels().isEmpty()) {
                 result.add("            Field f;");
             }
@@ -615,10 +620,9 @@
                 Collection<StateObject> args = stateHelperArgs.get(hmi.method.getQualifiedName());
                 result.add("        val." + hmi.method.getName() + "(" + getArgList(args) + ");");
             }
-            result.add("        " + "val.ready" + Level.Trial + " = true;");
             result.add("        " + so.fieldIdentifier + " = val;");
             result.add("    }");
-            result.add("    return " + so.fieldIdentifier + ";");
+            result.add("    return val;");
             result.add("}");
         }
 
@@ -787,9 +791,7 @@
                         }
                         break;
                     case Thread:
-                        for (Level level : Level.values()) {
-                            pw.println("    public boolean ready" + level + ";");
-                        }
+                        // these flags are redundant for single thread
                         break;
                     default:
                         throw new IllegalStateException("Unknown state scope: " + so.scope);