8318457: Use prefix-less prepend methods directly to reduce branches in String concat expressions

Reviewed-by: jlaskey, liach
This commit is contained in:
Claes Redestad 2023-10-20 11:37:07 +00:00
parent 71c99a0e59
commit fe52917054
3 changed files with 35 additions and 30 deletions

View File

@ -138,7 +138,6 @@ final class StringConcatHelper {
@PreviewFeature(feature=PreviewFeature.Feature.STRING_TEMPLATES)
static long mix(long lengthCoder, FormatConcatItem value) {
lengthCoder = value.mix(lengthCoder);
return checkOverflow(lengthCoder);
}
@ -152,7 +151,7 @@ final class StringConcatHelper {
* @param value boolean value to encode
* @return updated index (coder value retained)
*/
private static long prepend(long indexCoder, byte[] buf, boolean value) {
static long prepend(long indexCoder, byte[] buf, boolean value) {
int index = (int)indexCoder;
if (indexCoder < UTF16) {
if (value) {
@ -198,7 +197,7 @@ final class StringConcatHelper {
*/
static long prepend(long indexCoder, byte[] buf, boolean value, String prefix) {
indexCoder = prepend(indexCoder, buf, value);
if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
indexCoder = prepend(indexCoder, buf, prefix);
return indexCoder;
}
@ -212,7 +211,7 @@ final class StringConcatHelper {
* @param value char value to encode
* @return updated index (coder value retained)
*/
private static long prepend(long indexCoder, byte[] buf, char value) {
static long prepend(long indexCoder, byte[] buf, char value) {
if (indexCoder < UTF16) {
buf[(int)(--indexCoder)] = (byte) (value & 0xFF);
} else {
@ -234,7 +233,7 @@ final class StringConcatHelper {
*/
static long prepend(long indexCoder, byte[] buf, char value, String prefix) {
indexCoder = prepend(indexCoder, buf, value);
if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
indexCoder = prepend(indexCoder, buf, prefix);
return indexCoder;
}
@ -248,7 +247,7 @@ final class StringConcatHelper {
* @param value integer value to encode
* @return updated index (coder value retained)
*/
private static long prepend(long indexCoder, byte[] buf, int value) {
static long prepend(long indexCoder, byte[] buf, int value) {
if (indexCoder < UTF16) {
return StringLatin1.getChars(value, (int)indexCoder, buf);
} else {
@ -269,7 +268,7 @@ final class StringConcatHelper {
*/
static long prepend(long indexCoder, byte[] buf, int value, String prefix) {
indexCoder = prepend(indexCoder, buf, value);
if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
indexCoder = prepend(indexCoder, buf, prefix);
return indexCoder;
}
@ -283,7 +282,7 @@ final class StringConcatHelper {
* @param value long value to encode
* @return updated index (coder value retained)
*/
private static long prepend(long indexCoder, byte[] buf, long value) {
static long prepend(long indexCoder, byte[] buf, long value) {
if (indexCoder < UTF16) {
return StringLatin1.getChars(value, (int)indexCoder, buf);
} else {
@ -304,7 +303,7 @@ final class StringConcatHelper {
*/
static long prepend(long indexCoder, byte[] buf, long value, String prefix) {
indexCoder = prepend(indexCoder, buf, value);
if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
indexCoder = prepend(indexCoder, buf, prefix);
return indexCoder;
}
@ -318,7 +317,7 @@ final class StringConcatHelper {
* @param value String value to encode
* @return updated index (coder value retained)
*/
private static long prepend(long indexCoder, byte[] buf, String value) {
static long prepend(long indexCoder, byte[] buf, String value) {
indexCoder -= value.length();
if (indexCoder < UTF16) {
value.getBytes(buf, (int)indexCoder, String.LATIN1);
@ -341,7 +340,7 @@ final class StringConcatHelper {
*/
static long prepend(long indexCoder, byte[] buf, String value, String prefix) {
indexCoder = prepend(indexCoder, buf, value);
if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
indexCoder = prepend(indexCoder, buf, prefix);
return indexCoder;
}
@ -357,8 +356,7 @@ final class StringConcatHelper {
* @since 21
*/
@PreviewFeature(feature=PreviewFeature.Feature.STRING_TEMPLATES)
private static long prepend(long indexCoder, byte[] buf,
FormatConcatItem value) {
static long prepend(long indexCoder, byte[] buf, FormatConcatItem value) {
try {
return value.prepend(indexCoder, buf);
} catch (Error ex) {
@ -384,7 +382,7 @@ final class StringConcatHelper {
static long prepend(long indexCoder, byte[] buf,
FormatConcatItem value, String prefix) {
indexCoder = prepend(indexCoder, buf, value);
if (prefix != null) indexCoder = prepend(indexCoder, buf, prefix);
indexCoder = prepend(indexCoder, buf, prefix);
return indexCoder;
}
@ -586,7 +584,8 @@ final class StringConcatHelper {
static MethodHandle lookupStatic(String name, MethodType methodType) {
try {
return MethodHandles.lookup().findStatic(StringConcatHelper.class, name, methodType);
return MethodHandles.lookup()
.findStatic(StringConcatHelper.class, name, methodType);
} catch (NoSuchMethodException|IllegalAccessException e) {
throw new AssertionError(e);
}

View File

@ -700,19 +700,12 @@ public final class StringConcatFactory {
// Simple prependers, single argument. May be used directly or as a
// building block for complex prepender combinators.
private static MethodHandle prepender(String prefix, Class<?> cl) {
MethodHandle prepend;
int idx = classIndex(cl);
if (prefix == null) {
prepend = NULL_PREPENDERS[idx];
if (prepend == null) {
NULL_PREPENDERS[idx] = prepend = MethodHandles.insertArguments(
prepender(cl), 3, (String)null);
}
if (prefix == null || prefix.isEmpty()) {
return noPrefixPrepender(cl);
} else {
prepend = MethodHandles.insertArguments(
return MethodHandles.insertArguments(
prepender(cl), 3, prefix);
}
return prepend;
}
private static MethodHandle prepender(Class<?> cl) {
@ -729,6 +722,20 @@ public final class StringConcatFactory {
return prepend;
}
private static MethodHandle noPrefixPrepender(Class<?> cl) {
int idx = classIndex(cl);
MethodHandle prepend = NO_PREFIX_PREPENDERS[idx];
if (prepend == null) {
if (idx == STRING_CONCAT_ITEM) {
cl = FormatConcatItem.class;
}
NO_PREFIX_PREPENDERS[idx] = prepend = JLA.stringConcatHelper("prepend",
methodType(long.class, long.class, byte[].class,
Wrapper.asPrimitiveType(cl))).rebind();
}
return prepend;
}
private static final int INT_IDX = 0,
CHAR_IDX = 1,
LONG_IDX = 2,
@ -995,7 +1002,7 @@ public final class StringConcatFactory {
}
}
private static final @Stable MethodHandle[] NULL_PREPENDERS = new MethodHandle[TYPE_COUNT];
private static final @Stable MethodHandle[] NO_PREFIX_PREPENDERS = new MethodHandle[TYPE_COUNT];
private static final @Stable MethodHandle[] PREPENDERS = new MethodHandle[TYPE_COUNT];
private static final @Stable MethodHandle[] MIXERS = new MethodHandle[TYPE_COUNT];
private static final long INITIAL_CODER = JLA.stringConcatInitialCoder();
@ -1117,7 +1124,7 @@ public final class StringConcatFactory {
Class<?> ttype = ttypes[pos];
// (long,byte[],ttype) -> long
MethodHandle prepender = prepender(fragment.isEmpty() ? null : fragment, ttype);
MethodHandle prepender = prepender(fragment, ttype);
// (byte[],long,ttypes...) -> String (unchanged)
mh = MethodHandles.filterArgumentsWithCombiner(mh, 1, prepender,1, 0, 2 + pos);

View File

@ -61,7 +61,7 @@ class FormatItem {
private static final MethodHandle STRING_PREPEND =
JLA.stringConcatHelper("prepend",
MethodType.methodType(long.class, long.class, byte[].class,
String.class, String.class));
String.class));
private static final MethodHandle SELECT_GETCHAR_MH =
JLA.stringConcatHelper("selectGetChar",
@ -87,8 +87,7 @@ class FormatItem {
private static long stringPrepend(long lengthCoder, byte[] buffer,
String value) throws Throwable {
return (long)STRING_PREPEND.invokeExact(lengthCoder, buffer, value,
(String)null);
return (long)STRING_PREPEND.invokeExact(lengthCoder, buffer, value);
}
private static MethodHandle selectGetChar(long indexCoder) throws Throwable {