-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-20054 Optimize BasicTypeImpl allocation #11574
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
Conversation
|
Thanks for your pull request! This pull request appears to follow the contribution rules. › This message was automatically generated. |
|
I've created a JMH benchmark to show the type of improvement package red.hat.puzzles.string;
import org.openjdk.jmh.annotations.*;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
@State(Scope.Benchmark)
@Warmup(iterations = 10, time = 400, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 400, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@BenchmarkMode(Mode.AverageTime)
@Fork(2)
public class StringBuilderVsFormat {
private static final String EXTERNALIZED_PREFIX = "ExternalizedType";
// Simulated fixed values
private int count;
private String typeClassName;
private int defaultSqlTypeCode;
@Setup(Level.Trial)
public void setup() {
count = 12345;
typeClassName = "java.lang.String";
defaultSqlTypeCode = 4;
}
@Benchmark
public String stringFormat() {
return String.format(
Locale.ROOT,
"%s@%s(%s,%s)",
EXTERNALIZED_PREFIX,
count,
typeClassName,
defaultSqlTypeCode
);
}
@Benchmark
public String stringConcat() {
return EXTERNALIZED_PREFIX + '@' + count + '(' + typeClassName + ',' + defaultSqlTypeCode + ')';
}
}which will result, on my machine with OpenJDK 25 |
hibernate-core/src/main/java/org/hibernate/type/internal/BasicTypeImpl.java
Show resolved
Hide resolved
|
For who is not a fan of string concat indy, I've added @Benchmark
public String stringBuilderWithPrecomputedSize() {
int chars = EXTERNALIZED_PREFIX.length() + 1 // '@'
+ stringSize(count)
+ 2 // '(' and ','
+ typeClassName.length()
+ stringSize(defaultSqlTypeCode)
+ 1; // ')'
StringBuilder nameBuilder = new StringBuilder(chars);
nameBuilder.append(EXTERNALIZED_PREFIX)
.append('@')
.append(count)
.append('(')
.append(typeClassName)
.append(',')
.append(defaultSqlTypeCode)
.append(')');
return nameBuilder.toString();
}
public static int stringSize(int x) {
int sign = 1;
if (x >= 0) {
sign = 0;
x = -x;
}
if (x >= -10) {
return sign + 1;
}
if (x >= -100) {
return sign + 2;
}
if (x >= -1000) {
return sign + 3;
}
if (x >= -10000) {
return sign + 4;
}
if (x >= -100000) {
return sign + 5;
}
if (x >= -1000000) {
return sign + 6;
}
if (x >= -10000000) {
return sign + 7;
}
if (x >= -100000000) {
return sign + 8;
}
if (x >= -1000000000) {
return sign + 9;
}
return sign + 10;
}to the benchmark, getting which shows that the increased allocation of the
but it won't matter that much tbh |
|
@marko-bekhta I've updated the HHH issue with the flamegraph screenshot 🙏 |
mbellade
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @franz1981!
https://hibernate.atlassian.net/browse/HHH-20054
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license
and can be relicensed under the terms of the LGPL v2.1 license in the future at the maintainers' discretion.
For more information on licensing, please check here.