Skip to content
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

8336831: Optimize StringConcatHelper.simpleConcat #20253

Closed
wants to merge 10 commits into from
18 changes: 14 additions & 4 deletions src/java.base/share/classes/java/lang/StringConcatHelper.java
Original file line number Diff line number Diff line change
@@ -369,8 +369,8 @@ static String simpleConcat(Object first, Object second) {
return new String(s1);
}
byte coder = (byte) (s1.coder() | s2.coder());
int len = s1.length() + s2.length();
byte[] buf = (byte[]) UNSAFE.allocateUninitializedArray(byte.class, len << coder);
int newLength = (s1.length() + s2.length()) << coder;
byte[] buf = newArray(newLength);
s1.getBytes(buf, 0, coder);
s2.getBytes(buf, s1.length(), coder);
return new String(buf, coder);
@@ -441,10 +441,20 @@ static byte[] newArrayWithSuffix(String suffix, long indexCoder) {
static byte[] newArray(long indexCoder) {
byte coder = (byte)(indexCoder >> 32);
int index = ((int)indexCoder) << coder;
if (index < 0) {
return newArray(index);
}

/**
* Allocates an uninitialized byte array based on the length
* @param length
* @return the newly allocated byte array
*/
@ForceInline
static byte[] newArray(int length) {
if (length < 0) {
throw new OutOfMemoryError("Overflow: String length out of range");
}
return (byte[]) UNSAFE.allocateUninitializedArray(byte.class, index);
return (byte[]) UNSAFE.allocateUninitializedArray(byte.class, length);
}

/**