Skip to content

Commit b1811c9

Browse files
committed
Ensure no String-based class-lookup is needed for DateFormatSymbols.getInstance at libgraal runtime
1 parent f9e4a62 commit b1811c9

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

compiler/src/jdk.graal.compiler.libgraal/src/jdk/graal/compiler/libgraal/LibGraalSubstitutions.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@
2424
*/
2525
package jdk.graal.compiler.libgraal;
2626

27+
import java.text.DateFormatSymbols;
2728
import java.util.Collections;
29+
import java.util.Locale;
2830
import java.util.Set;
2931

3032
import com.oracle.svm.core.annotate.Alias;
3133
import com.oracle.svm.core.annotate.RecomputeFieldValue;
3234
import com.oracle.svm.core.annotate.Substitute;
3335
import com.oracle.svm.core.annotate.TargetClass;
3436

37+
import jdk.graal.compiler.debug.DebugOptions;
38+
3539
class LibGraalSubstitutions {
3640

3741
@TargetClass(className = "jdk.vm.ci.services.Services", onlyWith = LibGraalFeature.IsEnabled.class)
@@ -103,4 +107,18 @@ static Class<?> findBootstrapClassOrNull(String name) {
103107
return null;
104108
}
105109
}
110+
111+
@TargetClass(value = java.text.DateFormatSymbols.class, onlyWith = LibGraalFeature.IsEnabled.class)
112+
static final class Target_java_text_DateFormatSymbols {
113+
/*
114+
* DateFormatSymbols.getInstance(Locale) relies on String-based class-lookup (to find
115+
* resource bundle sun.text.resources.cldr.FormatData) which we do not want to rely on at
116+
* libgraal runtime because it increases image size too much. Instead, we return the
117+
* DateFormatSymbols instance that we already have in the image heap.
118+
*/
119+
@Substitute
120+
public static DateFormatSymbols getInstance(Locale unused) {
121+
return DebugOptions.getSharedDateFormatSymbols();
122+
}
123+
}
106124
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/debug/DebugOptions.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static jdk.graal.compiler.debug.PathUtilities.getPath;
3131

3232
import java.io.IOException;
33+
import java.text.DateFormatSymbols;
3334
import java.text.SimpleDateFormat;
3435
import java.util.Date;
3536

@@ -324,6 +325,20 @@ public static String getDumpDirectory(OptionValues options) throws IOException {
324325
return dumpDir;
325326
}
326327

328+
/**
329+
* This circumvents instantiating SimpleDateFormat at libgraal runtime. This is needed to avoid
330+
* String-based class-lookup (to find resource bundle sun.text.resources.cldr.FormatData as part
331+
* of SimpleDateFormat construction) and allows us to avoid class-lookup support in the image.
332+
*/
333+
private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss.SSS");
334+
335+
public static DateFormatSymbols getSharedDateFormatSymbols() {
336+
// SimpleDateFormat is not thread-safe
337+
synchronized (SIMPLE_DATE_FORMAT) {
338+
return SIMPLE_DATE_FORMAT.getDateFormatSymbols();
339+
}
340+
}
341+
327342
/**
328343
* Returns the {@link #getDumpDirectory} without attempting to create it.
329344
*/
@@ -333,8 +348,12 @@ public static String getDumpDirectoryName(OptionValues options) {
333348
dumpDir = getPath(DumpPath.getValue(options));
334349
} else {
335350
Date date = new Date(GraalServices.getGlobalTimeStamp());
336-
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss.SSS");
337-
dumpDir = getPath(DumpPath.getValue(options), formatter.format(date));
351+
// SimpleDateFormat is not thread-safe
352+
String dateString;
353+
synchronized (SIMPLE_DATE_FORMAT) {
354+
dateString = SIMPLE_DATE_FORMAT.format(date);
355+
}
356+
dumpDir = getPath(DumpPath.getValue(options), dateString);
338357
}
339358
dumpDir = getAbsolutePath(dumpDir);
340359
return dumpDir;

0 commit comments

Comments
 (0)