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-58691] Track metadata conditions only when they are used. #9847

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
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 @@ -44,11 +44,18 @@

/**
* A condition that describes if a reflectively-accessed element in Native Image is visible by the
* user.
* user at run time.
* <p>
* Currently, there is only one type of condition (<code>typeReached</code>) so this is a final
* class instead of the class hierarchy. The {@link ConfigurationCondition#type} represents the
* {@link Class<>} that needs to be reached by analysis in order for an element to be visible.
* Currently, there is only two types of condition:
* <li><code>typeReached</code> (the default) that signifies that the type must be both reachable by
* static analysis at build time, and reached at run time. A type is reached at run time, right
* before the class-initialization routine starts for that type, or any of the type's subtypes are
* reached.</li>
* <li><code>typeReachable</code> (legacy) that signifies that the type must be reachable by static
* analysis at build time.</li>
* <p>
* When {@link ConfigurationCondition#runtimeChecked} is <code>true</code> denotes that this is a
* <code>typeReached</code> condition.
*/
public final class ConfigurationCondition {

Expand All @@ -63,6 +70,25 @@ public static ConfigurationCondition alwaysTrue() {

private final boolean runtimeChecked;

/**
* Creates the default type-reached condition that is satisfied when the type is reached at
* runtime.
*
* @param type that has to be reached for this condition to be satisfied
* @return instance of the condition
*/
public static ConfigurationCondition create(Class<?> type) {
return create(type, true);
}

/**
* Creates either a type-reached condition ({@code runtimeChecked = true}) or a type-reachable
* condition.
*
* @param type that has to be reached (or reachable) for this condition to be satisfied
* @param runtimeChecked makes this a type-reachable condition when false
* @return instance of the condition
*/
public static ConfigurationCondition create(Class<?> type, boolean runtimeChecked) {
Objects.requireNonNull(type);
if (JAVA_LANG_OBJECT_REACHED.getType().equals(type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public final class UnresolvedConfigurationCondition implements Comparable<Unreso
private final String typeName;
private final boolean runtimeChecked;

public static UnresolvedConfigurationCondition create(String typeName) {
return create(typeName, true);
}

public static UnresolvedConfigurationCondition create(String typeName, boolean runtimeChecked) {
Objects.requireNonNull(typeName);
if (JAVA_LANG_OBJECT_REACHED.getTypeName().equals(typeName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private ConfigurationSet deduceConditionalConfiguration(Map<MethodInfo, List<Met
for (List<MethodCallNode> value : methodCallNodes.values()) {
for (MethodCallNode node : value) {
String className = node.methodInfo.getJavaDeclaringClassName();
UnresolvedConfigurationCondition condition = UnresolvedConfigurationCondition.create(className, true);
UnresolvedConfigurationCondition condition = UnresolvedConfigurationCondition.create(className);
var resolvedCondition = ConfigurationConditionResolver.identityResolver().resolveCondition(condition);
addConfigurationWithCondition(configurationSet, node.configuration, resolvedCondition.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ protected UnresolvedConfigurationCondition parseCondition(EconomicMap<String, Ob
var condition = parseTypeContents(object);
if (condition.isPresent()) {
String className = ((NamedConfigurationTypeDescriptor) condition.get()).name();
return UnresolvedConfigurationCondition.create(className, true);
return UnresolvedConfigurationCondition.create(className);
}
} else if (conditionObject.containsKey(TYPE_REACHABLE_KEY)) {
if (runtimeCondition && !TreatAllTypeReachableConditionsAsTypeReached.getValue()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ private record CompiledConditionalPattern(ConfigurationCondition condition, Reso
private ImageClassLoader imageClassLoader;

private class ResourcesRegistryImpl extends ConditionalConfigurationRegistry implements ResourcesRegistry<ConfigurationCondition> {
private final ClassInitializationSupport classInitializationSupport = ClassInitializationSupport.singleton();

private final Set<String> alreadyAddedResources = new HashSet<>();

ResourcesRegistryImpl() {
Expand All @@ -199,6 +201,7 @@ public void addGlob(ConfigurationCondition condition, String module, String glob
public void addCondition(ConfigurationCondition condition, Module module, String resourcePath) {
var conditionalResource = Resources.singleton().getResourceStorage().get(createStorageKey(module, resourcePath));
if (conditionalResource != null) {
classInitializationSupport.addForTypeReachedTracking(condition.getType());
conditionalResource.getConditions().addCondition(condition);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ public TypeResult<ConfigurationCondition> resolveCondition(UnresolvedConfigurati
* reachability checks.
*/
var runtimeChecked = !classInitializationSupport.isAlwaysReached(type) && unresolvedCondition.isRuntimeChecked();
if (runtimeChecked) {
classInitializationSupport.addForTypeReachedTracking(type);
}
return ConfigurationCondition.create(type, runtimeChecked);
});
}
Expand Down