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

[GR-59809] Filter StringConcatFactory.CACHE to only include reachable types in the image #10146

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import java.lang.invoke.VarHandle;
import java.lang.ref.SoftReference;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
Expand Down Expand Up @@ -234,6 +236,52 @@ public Object transform(Object receiver, Object originalValue) {
}
});

if (JavaVersionUtil.JAVA_SPEC >= 24) {
/*
* StringConcatFactory$InlineHiddenClassStrategy was added in JDK 24, as well as its
* CACHE field, so there is no need to filter for previous JDK versions.
*/
Class<?> referencedKeyMapClazz = ReflectionUtil.lookupClass("jdk.internal.util.ReferencedKeyMap");
Method createMethod = ReflectionUtil.lookupMethod(referencedKeyMapClazz, "create", boolean.class, Supplier.class);
Method concurrentHashMapSupplierMethod = ReflectionUtil.lookupMethod(referencedKeyMapClazz, "concurrentHashMapSupplier");
Class<?> methodHandlePair = ReflectionUtil.lookupClass("java.lang.invoke.StringConcatFactory$InlineHiddenClassStrategy$MethodHandlePair");
Method constructorGetter = ReflectionUtil.lookupMethod(methodHandlePair, "constructor");
Method concatenatorGetter = ReflectionUtil.lookupMethod(methodHandlePair, "concatenator");

/*
* StringConcatFactory$InlineHiddenClassStrategy.CACHE is a cache like
* SpeciesData.transformHelpers, so it needs a similar transformation for the same
* reasons.
*/
access.registerFieldValueTransformer(
ReflectionUtil.lookupField(ReflectionUtil.lookupClass("java.lang.invoke.StringConcatFactory$InlineHiddenClassStrategy"), "CACHE"),
new FieldValueTransformerWithAvailability() {
@Override
public boolean isAvailable() {
return BuildPhaseProvider.isHostedUniverseBuilt();
}

@Override
@SuppressWarnings("unchecked")
public Object transform(Object receiver, Object originalValue) {
Map<Object, SoftReference<Object>> cache = (Map<Object, SoftReference<Object>>) originalValue;
Map<Object, Object> result = ReflectionUtil.invokeMethod(createMethod, null, true, ReflectionUtil.invokeMethod(concurrentHashMapSupplierMethod, null));

for (var entry : cache.entrySet()) {
SoftReference<Object> value = entry.getValue();
Object object = value.get();
MethodHandle constructor = ReflectionUtil.invokeMethod(constructorGetter, object);
MethodHandle concatenator = ReflectionUtil.invokeMethod(concatenatorGetter, object);
if (constructor != null && concatenator != null && heapScanner.isObjectReachable(constructor) && heapScanner.isObjectReachable(concatenator)) {
result.put(entry.getKey(), value);
}
}

return result;
}
});
}

/*
* Retrieve all six basic types from the java.lang.invoke.LambdaForm$BasicType class (void,
* int, long, float, double, Object) and invoke the
Expand Down