Skip to content

Commit cf13e25

Browse files
committed
[GR-62316] Reduce system property access in ImageInfo.
PullRequest: graal/20268
2 parents cb0beb3 + a43f3ee commit cf13e25

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/ImageInfo.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -60,7 +60,7 @@ private ImageInfo() {
6060
* {@link #PROPERTY_IMAGE_CODE_VALUE_BUILDTIME} the code is executing in the context of image
6161
* building (e.g. in a static initializer of a class that will be contained in the image). If
6262
* the property returns the string given by {@link #PROPERTY_IMAGE_CODE_VALUE_RUNTIME} the code
63-
* is executing at image runtime. Otherwise the property is not set.
63+
* is executing at image runtime. Otherwise, the property is not set.
6464
*
6565
* @since 19.0
6666
*/
@@ -118,6 +118,7 @@ private ImageInfo() {
118118
* @since 19.0
119119
*/
120120
public static boolean inImageCode() {
121+
// A plugin in SubstrateGraphBuilderPlugins constant-folds this method to return true.
121122
return inImageBuildtimeCode() || inImageRuntimeCode();
122123
}
123124

@@ -129,17 +130,21 @@ public static boolean inImageCode() {
129130
* @since 19.0
130131
*/
131132
public static boolean inImageRuntimeCode() {
132-
return PROPERTY_IMAGE_CODE_VALUE_RUNTIME.equals(System.getProperty(PROPERTY_IMAGE_CODE_KEY));
133+
// A plugin in SubstrateGraphBuilderPlugins constant-folds this method to return true.
134+
return false;
133135
}
134136

137+
private static final boolean BUILDTIME = PROPERTY_IMAGE_CODE_VALUE_BUILDTIME.equals(System.getProperty(PROPERTY_IMAGE_CODE_KEY));
138+
135139
/**
136140
* Returns true if (at the time of the call) code is executing in the context of image building
137141
* (e.g. in a static initializer of class that will be contained in the image).
138142
*
139143
* @since 19.0
140144
*/
141145
public static boolean inImageBuildtimeCode() {
142-
return PROPERTY_IMAGE_CODE_VALUE_BUILDTIME.equals(System.getProperty(PROPERTY_IMAGE_CODE_KEY));
146+
// A plugin in SubstrateGraphBuilderPlugins constant-folds this method to return false.
147+
return BUILDTIME;
143148
}
144149

145150
/**

substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
import java.util.stream.Collectors;
7272
import java.util.stream.Stream;
7373

74+
import org.graalvm.nativeimage.ImageInfo;
7475
import org.graalvm.nativeimage.Platform;
7576
import org.graalvm.nativeimage.ProcessProperties;
7677

@@ -998,6 +999,8 @@ private void prepareImageBuildArgs() {
998999
addImageBuilderJavaArgs("-Dcom.oracle.graalvm.isaot=true");
9991000
addImageBuilderJavaArgs("-Djava.system.class.loader=" + CUSTOM_SYSTEM_CLASS_LOADER);
10001001

1002+
addImageBuilderJavaArgs("-D" + ImageInfo.PROPERTY_IMAGE_CODE_KEY + "=" + ImageInfo.PROPERTY_IMAGE_CODE_VALUE_BUILDTIME);
1003+
10011004
/*
10021005
* The presence of CDS and custom system class loaders disables the use of archived
10031006
* non-system class and triggers a warning.

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ public void run(Map<Method, CEntryPointData> entryPoints,
541541
}
542542

543543
protected static void setSystemPropertiesForImageEarly() {
544-
System.setProperty(ImageInfo.PROPERTY_IMAGE_CODE_KEY, ImageInfo.PROPERTY_IMAGE_CODE_VALUE_BUILDTIME);
544+
VMError.guarantee(ImageInfo.inImageBuildtimeCode(), "Expected ImageInfo.inImageBuildtimeCode() to return true");
545545
}
546546

547547
private static void setSystemPropertiesForImageLate(NativeImageKind imageKind) {
@@ -554,7 +554,6 @@ private static void setSystemPropertiesForImageLate(NativeImageKind imageKind) {
554554
}
555555

556556
public static void clearSystemPropertiesForImage() {
557-
System.clearProperty(ImageInfo.PROPERTY_IMAGE_CODE_KEY);
558557
System.clearProperty(ImageInfo.PROPERTY_IMAGE_KIND_KEY);
559558
}
560559

0 commit comments

Comments
 (0)