changeset 992:e7f0bf479bcf

generators: cache padding generators, saves a lot of benchmark generation time.
author shade
date Tue, 02 Sep 2014 16:57:46 +0400
parents 6a06014f7a38
children a8f5b91bcefc
files jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGenerator.java jmh-core/src/main/java/org/openjdk/jmh/generators/core/Paddings.java jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java
diffstat 3 files changed, 71 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGenerator.java	Tue Sep 02 15:39:52 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/BenchmarkGenerator.java	Tue Sep 02 16:57:46 2014 +0400
@@ -498,7 +498,9 @@
         writer.println("@Generated(\"" + BenchmarkGenerator.class.getCanonicalName() + "\")");
         writer.println("public final class " + info.generatedClassName + " {");
         writer.println();
-        generatePadding(writer);
+
+        // generate padding
+        Paddings.padding(writer, "p");
 
         writer.println(ident(1) + "int startRndMask;");
 
@@ -544,19 +546,6 @@
         writer.close();
     }
 
-    private void generatePadding(PrintWriter writer) {
-        for (int p = 0; p < 16; p++) {
-            StringBuilder sb = new StringBuilder();
-            sb.append(ident(1));
-            sb.append(String.format("boolean p%03d", p*16));
-            for (int q = 1; q < 16; q++) {
-                sb.append(String.format(", p%03d", p*16 + q));
-            }
-            sb.append(";");
-            writer.println(sb.toString());
-        }
-    }
-
     private void generateImport(PrintWriter writer) {
         Class<?>[] imports = new Class<?>[]{
                 List.class, AtomicInteger.class, AtomicIntegerFieldUpdater.class,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/Paddings.java	Tue Sep 02 16:57:46 2014 +0400
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.jmh.generators.core;
+
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class Paddings {
+
+    private static final Map<String, List<String>> PADDING_CACHE = new HashMap<String, List<String>>();
+
+    public static void padding(List<String> lines, String suffix) {
+        List<String> ps = PADDING_CACHE.get(suffix);
+        if (ps != null) {
+            lines.addAll(ps);
+            return;
+        }
+
+        List<String> tl = new ArrayList<String>();
+        for (int p = 0; p < 16; p++) {
+            StringBuilder sb = new StringBuilder();
+            sb.append("    ");
+            sb.append("boolean ").append(suffix).append(String.format("_%03d", p * 16 + 0));
+            for (int q = 1; q < 16; q++) {
+                sb.append(", ").append(suffix).append(String.format("_%03d", p * 16 + q));
+            }
+            sb.append(";");
+            tl.add(sb.toString());
+        }
+        PADDING_CACHE.put(suffix, tl);
+        lines.addAll(tl);
+    }
+
+    public static void padding(PrintWriter writer, String suffix) {
+        List<String> ss = new ArrayList<String>();
+        padding(ss, suffix);
+        for (String s : ss) {
+            writer.println(s);
+        }
+    }
+}
--- a/jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java	Tue Sep 02 15:39:52 2014 +0400
+++ b/jmh-core/src/main/java/org/openjdk/jmh/generators/core/StateObjectHandler.java	Tue Sep 02 16:57:46 2014 +0400
@@ -749,7 +749,7 @@
             if (!visited.add(so.userType)) continue;
 
             result.add("static class " + so.type + "_B1 extends " + so.userType + " {");
-            padding(result, "b1");
+            Paddings.padding(result, "b1");
             addSuperCall(result, so, "_B1");
             result.add("}");
             result.add("");
@@ -787,7 +787,7 @@
             result.add("}");
             result.add("");
             result.add("static class " + so.type + "_B3 extends " + so.type + "_B2 {");
-            padding(result, "b3");
+            Paddings.padding(result, "b3");
             addSuperCall(result, so, "_B3");
             result.add("}");
             result.add("");
@@ -799,18 +799,6 @@
         return result;
     }
 
-    public static void padding(List<String> lines, String suffix) {
-        for (int p = 0; p < 16; p++) {
-            StringBuilder sb = new StringBuilder();
-            sb.append("    boolean ").append(suffix).append(String.format("_%03d", p * 16));
-            for (int q = 1; q < 16; q++) {
-                sb.append(", ").append(suffix).append(String.format("_%03d", p*16 + q));
-            }
-            sb.append(";");
-            lines.add(sb.toString());
-        }
-    }
-
     public Collection<String> getFields() {
         return Collections.emptyList();
     }