zabetak commented on a change in pull request #1080: [CALCITE-2887] Improve 
performance of RelLiteral#toJavaString
URL: https://github.com/apache/calcite/pull/1080#discussion_r261845926
 
 

 ##########
 File path: 
ubenchmark/src/main/java/org/apache/calcite/benchmarks/StringConstructBenchmark.java
 ##########
 @@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.benchmarks;
+
+
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Threads;
+import org.openjdk.jmh.annotations.Warmup;
+import org.openjdk.jmh.infra.Blackhole;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.RunnerException;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * A benchmark of the most common patterns that are used to construct 
gradually String objects.
+ *
+ * The benchmark emphasizes on the build patterns that appear in the Calcite 
project.
+ */
+@Fork(value = 1, jvmArgsPrepend = "-Xmx2048m")
+@Measurement(iterations = 10, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+@Warmup(iterations = 10, time = 100, timeUnit = TimeUnit.MILLISECONDS)
+@Threads(1)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+@BenchmarkMode(Mode.Throughput)
+public class StringConstructBenchmark {
+
+  /**
+   * A state holding a Writer object which is initialized only once at the 
beginning of the
+   * benchmark.
+   */
+  @State(Scope.Thread)
+  public static class WriterState {
+    public Writer writer;
+
+    @Setup(Level.Trial)
+    public void setup() {
+      this.writer = new StringWriter();
+    }
+  }
+
+  /**
+   * A state holding an Appendable object which is initialized once at the 
beginning of every
+   * iteration.
+   */
+  @State(Scope.Thread)
+  public static class AppenderState {
+    /**
+     * The type of the appender to be initialised.
+     */
+    @Param({"StringBuilder", "StringWriter", "PrintWriter"})
+    public String appenderType;
+
+    public Appendable appender;
+
+    @Setup(Level.Iteration)
+    public void setup() {
+      if (appenderType.equals("StringBuilder")) {
+        this.appender = new StringBuilder();
+      }
+      if (appenderType.equals("StringWriter")) {
+        this.appender = new StringWriter();
+      }
+      if (appenderType.equals("PrintWriter")) {
+        this.appender = new PrintWriter(new StringWriter());
+      }
+      throw new IllegalStateException("No correct appender type is 
specified.");
+    }
+  }
+
+  @Benchmark
+  public StringBuilder initStringBuilder() {
+    return new StringBuilder();
+  }
+
+  @Benchmark
+  public StringWriter initStringWriter() {
+    return new StringWriter();
+  }
+
+  @Benchmark
+  public PrintWriter initPrintWriter(WriterState writerState) {
+    return new PrintWriter(writerState.writer);
+  }
+
+
+  @Benchmark
+  public void appendString(Blackhole bh, AppenderState appenderState) throws 
IOException {
+    bh.consume(appenderState.appender.append("A small string"));
 
 Review comment:
   I didn't know about Koloboke. Thanks for sharing! 
   
   > By the way: I have seen that PrintWriter, however I thought "it was decent 
enough", and I did not feel brave enough to break public API.
   
   I was thinking to try to get rid off PrintWriter from everywhere but this 
would introduce too many changes and many of them unjustifiable. However, this 
piece of code is important to be fast and it makes sense to improve it a bit, 
especially if the change needed is so small.  

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to