Skip to content

Commit 484d531

Browse files
committed
[GR-58691] Track metadata conditions only when they are used.
PullRequest: graal/18978
2 parents ab2d3a8 + e679196 commit 484d531

File tree

6 files changed

+39
-9
lines changed

6 files changed

+39
-9
lines changed

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/impl/ConfigurationCondition.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,18 @@
4444

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

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

6471
private final boolean runtimeChecked;
6572

73+
/**
74+
* Creates the default type-reached condition that is satisfied when the type is reached at
75+
* runtime.
76+
*
77+
* @param type that has to be reached for this condition to be satisfied
78+
* @return instance of the condition
79+
*/
80+
public static ConfigurationCondition create(Class<?> type) {
81+
return create(type, true);
82+
}
83+
84+
/**
85+
* Creates either a type-reached condition ({@code runtimeChecked = true}) or a type-reachable
86+
* condition.
87+
*
88+
* @param type that has to be reached (or reachable) for this condition to be satisfied
89+
* @param runtimeChecked makes this a type-reachable condition when false
90+
* @return instance of the condition
91+
*/
6692
public static ConfigurationCondition create(Class<?> type, boolean runtimeChecked) {
6793
Objects.requireNonNull(type);
6894
if (JAVA_LANG_OBJECT_REACHED.getType().equals(type)) {

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/impl/UnresolvedConfigurationCondition.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public final class UnresolvedConfigurationCondition implements Comparable<Unreso
5353
private final String typeName;
5454
private final boolean runtimeChecked;
5555

56+
public static UnresolvedConfigurationCondition create(String typeName) {
57+
return create(typeName, true);
58+
}
59+
5660
public static UnresolvedConfigurationCondition create(String typeName, boolean runtimeChecked) {
5761
Objects.requireNonNull(typeName);
5862
if (JAVA_LANG_OBJECT_REACHED.getTypeName().equals(typeName)) {

substratevm/src/com.oracle.svm.configure/src/com/oracle/svm/configure/config/conditional/ConditionalConfigurationComputer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private ConfigurationSet deduceConditionalConfiguration(Map<MethodInfo, List<Met
171171
for (List<MethodCallNode> value : methodCallNodes.values()) {
172172
for (MethodCallNode node : value) {
173173
String className = node.methodInfo.getJavaDeclaringClassName();
174-
UnresolvedConfigurationCondition condition = UnresolvedConfigurationCondition.create(className, true);
174+
UnresolvedConfigurationCondition condition = UnresolvedConfigurationCondition.create(className);
175175
var resolvedCondition = ConfigurationConditionResolver.identityResolver().resolveCondition(condition);
176176
addConfigurationWithCondition(configurationSet, node.configuration, resolvedCondition.get());
177177
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/configure/ConfigurationParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ protected UnresolvedConfigurationCondition parseCondition(EconomicMap<String, Ob
241241
var condition = parseTypeContents(object);
242242
if (condition.isPresent()) {
243243
String className = ((NamedConfigurationTypeDescriptor) condition.get()).name();
244-
return UnresolvedConfigurationCondition.create(className, true);
244+
return UnresolvedConfigurationCondition.create(className);
245245
}
246246
} else if (conditionObject.containsKey(TYPE_REACHABLE_KEY)) {
247247
if (runtimeCondition && !TreatAllTypeReachableConditionsAsTypeReached.getValue()) {

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ResourcesFeature.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ private record CompiledConditionalPattern(ConfigurationCondition condition, Reso
174174
private ImageClassLoader imageClassLoader;
175175

176176
private class ResourcesRegistryImpl extends ConditionalConfigurationRegistry implements ResourcesRegistry<ConfigurationCondition> {
177+
private final ClassInitializationSupport classInitializationSupport = ClassInitializationSupport.singleton();
178+
177179
private final Set<String> alreadyAddedResources = new HashSet<>();
178180

179181
ResourcesRegistryImpl() {
@@ -199,6 +201,7 @@ public void addGlob(ConfigurationCondition condition, String module, String glob
199201
public void addCondition(ConfigurationCondition condition, Module module, String resourcePath) {
200202
var conditionalResource = Resources.singleton().getResourceStorage().get(createStorageKey(module, resourcePath));
201203
if (conditionalResource != null) {
204+
classInitializationSupport.addForTypeReachedTracking(condition.getType());
202205
conditionalResource.getConditions().addCondition(condition);
203206
}
204207
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/reflect/NativeImageConditionResolver.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ public TypeResult<ConfigurationCondition> resolveCondition(UnresolvedConfigurati
5252
* reachability checks.
5353
*/
5454
var runtimeChecked = !classInitializationSupport.isAlwaysReached(type) && unresolvedCondition.isRuntimeChecked();
55-
if (runtimeChecked) {
56-
classInitializationSupport.addForTypeReachedTracking(type);
57-
}
5855
return ConfigurationCondition.create(type, runtimeChecked);
5956
});
6057
}

0 commit comments

Comments
 (0)