8335182: Consolidate and streamline String concat code shapes

Reviewed-by: liach, jvernee
This commit is contained in:
Claes Redestad 2024-07-23 11:50:57 +00:00
parent 4c7b3e7fc3
commit e83b4b236e
6 changed files with 452 additions and 154 deletions

View file

@ -565,17 +565,17 @@ public final class StringConcatFactory {
// Fold in byte[] instantiation at argument 0
MethodHandle newArrayCombinator;
if (suffix != null) {
// newArray variant that deals with prepending any trailing constant
//
// initialLengthCoder is adjusted to have the correct coder
// and length: The newArrayWithSuffix method expects only the coder of the
// suffix to be encoded into indexCoder
initialLengthCoder -= suffix.length();
newArrayCombinator = newArrayWithSuffix(suffix);
} else {
newArrayCombinator = newArray();
if (suffix == null || suffix.isEmpty()) {
suffix = "";
}
// newArray variant that deals with prepending any trailing constant
//
// initialLengthCoder is adjusted to have the correct coder
// and length: The newArrayWithSuffix method expects only the coder of the
// suffix to be encoded into indexCoder
initialLengthCoder -= suffix.length();
newArrayCombinator = newArrayWithSuffix(suffix);
mh = MethodHandles.foldArgumentsWithCombiner(mh, 0, newArrayCombinator,
1 // index
);
@ -738,9 +738,7 @@ public final class StringConcatFactory {
int idx = classIndex(cl);
MethodHandle prepend = NO_PREFIX_PREPENDERS[idx];
if (prepend == null) {
NO_PREFIX_PREPENDERS[idx] = prepend = JLA.stringConcatHelper("prepend",
methodType(long.class, long.class, byte[].class,
Wrapper.asPrimitiveType(cl))).rebind();
NO_PREFIX_PREPENDERS[idx] = prepend = MethodHandles.insertArguments(prepender(cl), 3, "");
}
return prepend;
}
@ -902,16 +900,6 @@ public final class StringConcatFactory {
return MethodHandles.insertArguments(mh, 0, suffix);
}
private @Stable static MethodHandle NEW_ARRAY;
private static MethodHandle newArray() {
MethodHandle mh = NEW_ARRAY;
if (mh == null) {
NEW_ARRAY = mh =
JLA.stringConcatHelper("newArray", methodType(byte[].class, long.class));
}
return mh;
}
/**
* Public gateways to public "stringify" methods. These methods have the
* form String apply(T obj), and normally delegate to {@code String.valueOf},