-
Enhancement
-
Resolution: Unresolved
-
P4
-
9, 10
This issue affects most copying constructors, notably String(String val).
E.g. if you run this benchmark with current HotSpot, then "copy()" would be slower, because it will
first pre-zero all the fields in MyClass, then check the incoming argument for nullity, and then overwrite
the fields with incoming argument fields. When incoming argument is not null, we are doing excess
writes.
@Benchmark
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public MyClass instant() {
return new MyClass(x1, x2, x3, x4);
}
@Benchmark
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public MyClass copy() {
return new MyClass(inst);
}
private static class MyClass {
private long x1, x2, x3, x4;
public MyClass(long x1, long x2, long x3, long x4) {
this.x1 = x1;
this.x2 = x2;
this.x3 = x3;
this.x4 = x4;
}
public MyClass(MyClass other) {
this.x1 = other.x1;
this.x2 = other.x2;
this.x3 = other.x3;
this.x4 = other.x4;
}
}
Full test, performance results, and disassembly interpretation is here:
http://cr.openjdk.java.net/~shade/8074566/InitStoreCoalesce.java
Runnable benchmarks JAR is here:
http://cr.openjdk.java.net/~shade/8074566/benchmarks.jar
E.g. if you run this benchmark with current HotSpot, then "copy()" would be slower, because it will
first pre-zero all the fields in MyClass, then check the incoming argument for nullity, and then overwrite
the fields with incoming argument fields. When incoming argument is not null, we are doing excess
writes.
@Benchmark
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public MyClass instant() {
return new MyClass(x1, x2, x3, x4);
}
@Benchmark
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public MyClass copy() {
return new MyClass(inst);
}
private static class MyClass {
private long x1, x2, x3, x4;
public MyClass(long x1, long x2, long x3, long x4) {
this.x1 = x1;
this.x2 = x2;
this.x3 = x3;
this.x4 = x4;
}
public MyClass(MyClass other) {
this.x1 = other.x1;
this.x2 = other.x2;
this.x3 = other.x3;
this.x4 = other.x4;
}
}
Full test, performance results, and disassembly interpretation is here:
http://cr.openjdk.java.net/~shade/8074566/InitStoreCoalesce.java
Runnable benchmarks JAR is here:
http://cr.openjdk.java.net/~shade/8074566/benchmarks.jar