-
Notifications
You must be signed in to change notification settings - Fork 478
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion : continuous calls of StringBuilder.append #425
Comments
I'm not sure if this brings any performance benefits, can you try with a test whats the performance gain ? |
Disclaimer: I'm not a JVM specialist and take this as a naive test as I did just for fun. My first thought when I read this message was "nah, JIT will eventually optimize the 'hot' code and inline the multi-line calls because that BatchPoints.toString() is very short" (this seems to be a reliable answer on how the hotspot decides how and when to optimize). Well, I was wrong; :) With JMH, I got these numbers:
on a 8-core machine
running
And here is the source-code of my test: 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.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
@State(Scope.Thread)
@BenchmarkMode(Mode.SingleShotTime)
@Measurement(batchSize = 100000, iterations = 20)
@Warmup(batchSize = 100000, iterations = 10)
@Fork(5)
public class MyBenchmark {
private String database;
private String retentionPolicy;
private String consistency;
private String tags;
private String points;
@Setup(Level.Iteration)
public void setup() {
database = "database";
retentionPolicy = "retentionPolicy";
consistency = "consistency";
tags = "tags";
points = "points";
}
@Benchmark
public void testAppendMultipleLines(Blackhole bh) {
StringBuilder builder = new StringBuilder();
builder.append("BatchPoints [database=");
builder.append(this.database);
builder.append(", retentionPolicy=");
builder.append(this.retentionPolicy);
builder.append(", consistency=");
builder.append(this.consistency);
builder.append(", tags=");
builder.append(this.tags);
builder.append(", points=");
builder.append(this.points);
builder.append("]");
bh.consume(builder.toString());
}
@Benchmark
public void testAppendChained(Blackhole bh) {
StringBuilder builder = new StringBuilder()
.append("BatchPoints [database=")
.append(this.database)
.append(", retentionPolicy=")
.append(this.retentionPolicy)
.append(", consistency=")
.append(this.consistency)
.append(", tags=")
.append(this.tags)
.append(", points=")
.append(this.points)
.append("]");
bh.consume(builder.toString());
}
@Benchmark
public void testSimpleStringConcat(Blackhole bh) {
bh.consume("BatchPoints [database="
+ this.database
+ ", retentionPolicy="
+ this.retentionPolicy
+ ", consistency="
+ this.consistency
+ ", tags="
+ this.tags
+ ", points="
+ this.points
+ "]");
}
} |
Nice, so we should transform to chained StringBuilder calls at least in the hot path. |
Hi,
I have found some continuous calls about
StringBuilder.append(..)
in some files. For example, there are eleven continuous calls in the function toString from the file influxdb-java/src/main/java/org/influxdb/dto/BatchPoints.java.If it was achieved like
StringBuilder.append(..).append(..)append(..)
, it will promote its performance.The text was updated successfully, but these errors were encountered: