Skip to content

Commit 61327a4

Browse files
committed
[GR-64061] Footprint improvements in Native Image Builder.
PullRequest: graal/20651
2 parents 0fe565e + dc9621a commit 61327a4

File tree

19 files changed

+59
-36
lines changed

19 files changed

+59
-36
lines changed

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/c/function/CFunctionPointer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 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

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/AnalysisMethod.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -119,6 +119,8 @@ public abstract class AnalysisMethod extends AnalysisElement implements WrappedJ
119119
private static final AtomicReferenceFieldUpdater<AnalysisMethod, Boolean> reachableInCurrentLayerUpdater = AtomicReferenceFieldUpdater
120120
.newUpdater(AnalysisMethod.class, Boolean.class, "reachableInCurrentLayer");
121121

122+
public static final AnalysisMethod[] EMPTY_ARRAY = new AnalysisMethod[0];
123+
122124
public record Signature(String name, AnalysisType[] parameterTypes) {
123125
}
124126

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/AnalysisType.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ public abstract class AnalysisType extends AnalysisElement implements WrappedJav
115115

116116
private static final AtomicReferenceFieldUpdater<AnalysisType, Object> RESOLVED_METHODS_UPDATER = AtomicReferenceFieldUpdater.newUpdater(AnalysisType.class, Object.class, "resolvedMethods");
117117

118+
public static final AnalysisType[] EMPTY_ARRAY = new AnalysisType[0];
119+
118120
protected final AnalysisUniverse universe;
119121
private final ResolvedJavaType wrapped;
120122
private final String qualifiedName;
@@ -360,7 +362,7 @@ private AnalysisType[] convertTypes(ResolvedJavaType[] originalTypes) {
360362
}
361363
result.add(universe.lookup(originalType));
362364
}
363-
return result.toArray(new AnalysisType[result.size()]);
365+
return result.toArray(AnalysisType.EMPTY_ARRAY);
364366
}
365367

366368
public AnalysisType getArrayClass(int dim) {

substratevm/src/com.oracle.graal.pointsto/src/com/oracle/graal/pointsto/meta/AnalysisUniverse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -471,7 +471,7 @@ public AnalysisMethod[] lookup(JavaMethod[] inputs) {
471471
}
472472
}
473473
}
474-
return result.toArray(new AnalysisMethod[result.size()]);
474+
return result.toArray(AnalysisMethod.EMPTY_ARRAY);
475475
}
476476

477477
@Override

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import com.oracle.svm.hosted.meta.HostedMethod;
3737

3838
class CodeBreakdownProvider {
39-
private final Map<String, Long> codeBreakdown;
39+
private Map<String, Long> codeBreakdown;
4040

4141
CodeBreakdownProvider(Collection<CompileTask> compilationTasks) {
4242
Map<String, Long> nameToSizeMap = new HashMap<>();
@@ -60,8 +60,17 @@ class CodeBreakdownProvider {
6060
codeBreakdown = Collections.unmodifiableMap(nameToSizeMap);
6161
}
6262

63-
public static Map<String, Long> get() {
64-
return ImageSingletons.lookup(CodeBreakdownProvider.class).codeBreakdown;
63+
/**
64+
* Provides the code breakdown as map from name to size, and clears the cache to avoid memory
65+
* footprint.
66+
*
67+
* @return the code breakdown
68+
*/
69+
public static Map<String, Long> getAndClear() {
70+
CodeBreakdownProvider singleton = ImageSingletons.lookup(CodeBreakdownProvider.class);
71+
Map<String, Long> map = singleton.codeBreakdown;
72+
singleton.codeBreakdown = null;
73+
return map;
6574
}
6675

6776
private static String findJARFile(Class<?> javaClass) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ private void printBreakdowns() {
641641
return;
642642
}
643643
l().printLineSeparator();
644-
Map<String, Long> codeBreakdown = CodeBreakdownProvider.get();
644+
Map<String, Long> codeBreakdown = CodeBreakdownProvider.getAndClear();
645645
Iterator<Entry<String, Long>> packagesBySize = codeBreakdown.entrySet().stream()
646646
.sorted(Entry.comparingByValue(Comparator.reverseOrder())).iterator();
647647

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/annotation/SubstrateAnnotationExtractor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ private AnnotationValue[][] getParameterAnnotationDataFromRoot(Executable rootEl
279279
ByteBuffer buf = ByteBuffer.wrap(rawParameterAnnotations);
280280
try {
281281
int numParameters = buf.get() & 0xFF;
282+
if (numParameters == 0) {
283+
return NO_PARAMETER_ANNOTATIONS;
284+
}
282285
AnnotationValue[][] parameterAnnotations = new AnnotationValue[numParameters][];
283286
for (int i = 0; i < numParameters; i++) {
284287
List<AnnotationValue> parameterAnnotationList = new ArrayList<>();

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/code/CEntryPointData.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public static CEntryPointData createCustomUnpublished() {
9191
private static CEntryPointData create(CEntryPoint annotation, CEntryPointOptions options, Supplier<String> alternativeNameSupplier) {
9292
String annotatedName = annotation.name();
9393
Class<? extends Function<String, String>> nameTransformation = DEFAULT_NAME_TRANSFORMATION;
94-
String documentation = String.join(System.lineSeparator(), annotation.documentation());
94+
String documentation = annotation.documentation().length == 0 ? "" : String.join(System.lineSeparator(), annotation.documentation());
9595
CEntryPoint.Builtin builtin = annotation.builtin();
9696
Class<?> prologue = DEFAULT_PROLOGUE;
9797
Class<?> prologueBailout = DEFAULT_PROLOGUE_BAILOUT;

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/code/RestrictHeapAccessCalleesImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void aggregateMethods(Collection<AnalysisMethod> methods) {
114114
}
115115
MethodAggregator visitor = new MethodAggregator(aggregation, assertionErrorConstructorList);
116116
AnalysisMethodCalleeWalker walker = new AnalysisMethodCalleeWalker();
117-
for (AnalysisMethod method : aggregation.keySet().toArray(new AnalysisMethod[0])) {
117+
for (AnalysisMethod method : aggregation.keySet().toArray(AnalysisMethod.EMPTY_ARRAY)) {
118118
walker.walkMethod(method, visitor);
119119
}
120120
calleeToCallerMap = Collections.unmodifiableMap(aggregation);

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/code/RuntimeMetadataEncoderImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,8 @@ public void addRecordComponentsLookupError(HostedType declaringClass, Throwable
720720
}
721721

722722
private static HostedType[] getParameterTypes(HostedMethod method) {
723-
HostedType[] parameterTypes = new HostedType[method.getSignature().getParameterCount(false)];
723+
int len = method.getSignature().getParameterCount(false);
724+
HostedType[] parameterTypes = len == 0 ? HostedType.EMPTY_ARRAY : new HostedType[len];
724725
for (int i = 0; i < parameterTypes.length; ++i) {
725726
parameterTypes[i] = method.getSignature().getParameterType(i);
726727
}
@@ -737,7 +738,7 @@ private static String[] getParameterTypeNames(HostedMethod method) {
737738

738739
private static HostedType[] getExceptionTypes(MetaAccessProvider metaAccess, Executable reflectMethod) {
739740
Class<?>[] exceptionClasses = reflectMethod.getExceptionTypes();
740-
HostedType[] exceptionTypes = new HostedType[exceptionClasses.length];
741+
HostedType[] exceptionTypes = exceptionClasses.length == 0 ? HostedType.EMPTY_ARRAY : new HostedType[exceptionClasses.length];
741742
for (int i = 0; i < exceptionClasses.length; ++i) {
742743
exceptionTypes[i] = (HostedType) metaAccess.lookupJavaType(exceptionClasses[i]);
743744
}

0 commit comments

Comments
 (0)