Skip to content

Commit c5d892d

Browse files
committed
Reduce usage of lambdas to reduce image size
1 parent 285e734 commit c5d892d

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

substratevm/src/com.oracle.svm.configure/src/com/oracle/svm/configure/ConfigurationTypeDescriptor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import java.util.Collection;
3030
import java.util.stream.Stream;
3131

32+
import com.oracle.svm.util.StringUtil;
33+
3234
import jdk.graal.compiler.java.LambdaUtils;
3335
import jdk.graal.compiler.util.json.JsonPrintable;
3436

@@ -54,7 +56,7 @@ static ConfigurationTypeDescriptor fromClass(Class<?> clazz) {
5456
if (Proxy.isProxyClass(clazz)) {
5557
return ProxyConfigurationTypeDescriptor.fromInterfaceReflectionNames(interfacesStream.toList());
5658
} else if (LambdaUtils.isLambdaClass(clazz)) {
57-
String declaringClass = LambdaUtils.capturingClass(clazz.getTypeName());
59+
String declaringClass = StringUtil.split(clazz.getTypeName(), LambdaUtils.LAMBDA_CLASS_NAME_SUBSTRING)[0];
5860
return LambdaConfigurationTypeDescriptor.fromReflectionNames(declaringClass, interfacesStream.toList());
5961
} else {
6062
return NamedConfigurationTypeDescriptor.fromReflectionName(clazz.getTypeName());

substratevm/src/com.oracle.svm.configure/src/com/oracle/svm/configure/LambdaConfigurationTypeDescriptor.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,32 @@
3333
import java.util.ArrayList;
3434
import java.util.Arrays;
3535
import java.util.Collection;
36-
import java.util.Comparator;
3736
import java.util.List;
3837

3938
import jdk.graal.compiler.util.json.JsonPrinter;
4039
import jdk.graal.compiler.util.json.JsonWriter;
4140

4241
public record LambdaConfigurationTypeDescriptor(ConfigurationTypeDescriptor declaringClass, ConfigurationParser.ConfigurationMethodDescriptor declaringMethod,
4342
List<NamedConfigurationTypeDescriptor> interfaces) implements ConfigurationTypeDescriptor {
43+
44+
public static final ConfigurationTypeDescriptor[] EMPTY_TYPE_DESCRIPTOR_ARRAY = new ConfigurationTypeDescriptor[0];
45+
4446
public static LambdaConfigurationTypeDescriptor fromReflectionNames(String declaringClass, List<String> interfaces) {
45-
return new LambdaConfigurationTypeDescriptor(NamedConfigurationTypeDescriptor.fromReflectionName(declaringClass), null,
46-
interfaces.stream().map(NamedConfigurationTypeDescriptor::fromReflectionName).toList());
47+
return new LambdaConfigurationTypeDescriptor(NamedConfigurationTypeDescriptor.fromReflectionName(declaringClass),
48+
null, getNamedConfigurationTypeDescriptors(interfaces));
4749
}
4850

4951
public static LambdaConfigurationTypeDescriptor fromTypeNames(String declaringClass, List<String> interfaces) {
5052
return new LambdaConfigurationTypeDescriptor(NamedConfigurationTypeDescriptor.fromTypeName(declaringClass), null,
51-
interfaces.stream().map(NamedConfigurationTypeDescriptor::fromTypeName).toList());
53+
getNamedConfigurationTypeDescriptors(interfaces));
54+
}
55+
56+
private static List<NamedConfigurationTypeDescriptor> getNamedConfigurationTypeDescriptors(List<String> interfaces) {
57+
List<NamedConfigurationTypeDescriptor> reflectionInterfaces = new ArrayList<>(interfaces.size());
58+
for (var interfaceName : interfaces) {
59+
reflectionInterfaces.add(NamedConfigurationTypeDescriptor.fromReflectionName(interfaceName));
60+
}
61+
return reflectionInterfaces;
5262
}
5363

5464
@Override
@@ -67,11 +77,26 @@ public Collection<String> getAllQualifiedJavaNames() {
6777

6878
@Override
6979
public int compareTo(ConfigurationTypeDescriptor other) {
70-
if (other instanceof LambdaConfigurationTypeDescriptor lambdaOther) {
71-
return Comparator.comparing(LambdaConfigurationTypeDescriptor::declaringClass)
72-
.thenComparing(LambdaConfigurationTypeDescriptor::declaringMethod, Comparator.nullsFirst(ConfigurationParser.ConfigurationMethodDescriptor::compareTo))
73-
.thenComparing((a, b) -> Arrays.compare(a.interfaces.toArray(ConfigurationTypeDescriptor[]::new), b.interfaces.toArray(ConfigurationTypeDescriptor[]::new)))
74-
.compare(this, lambdaOther);
80+
if (other instanceof LambdaConfigurationTypeDescriptor otherLambda) {
81+
int result = declaringClass.compareTo(otherLambda.declaringClass());
82+
if (result != 0) {
83+
return result;
84+
}
85+
// Compare declaringMethod with nullsFirst
86+
if (this.declaringMethod() == null && otherLambda.declaringMethod() != null) {
87+
return -1;
88+
} else if (this.declaringMethod() != null && otherLambda.declaringMethod() == null) {
89+
return 1;
90+
} else if (this.declaringMethod() != null) {
91+
result = this.declaringMethod().compareTo(otherLambda.declaringMethod());
92+
if (result != 0) {
93+
return result;
94+
}
95+
}
96+
97+
return Arrays.compare(
98+
interfaces.toArray(EMPTY_TYPE_DESCRIPTOR_ARRAY),
99+
otherLambda.interfaces().toArray(EMPTY_TYPE_DESCRIPTOR_ARRAY));
75100
} else {
76101
return getDescriptorType().compareTo(other.getDescriptorType());
77102
}

substratevm/src/com.oracle.svm.configure/src/com/oracle/svm/configure/config/ConfigurationType.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -498,10 +498,9 @@ public synchronized void printJson(JsonWriter writer) throws IOException {
498498
Set<ConfigurationMethod> accessedMethods = getMethodsByAccessibility(ConfigurationMemberAccessibility.ACCESSED);
499499
if (!accessedMethods.isEmpty()) {
500500
writer.appendSeparator().quote("methods").appendFieldSeparator();
501-
JsonPrinter.printCollection(writer,
502-
accessedMethods,
503-
Comparator.comparing(ConfigurationMethod::getName).thenComparing(Comparator.nullsFirst(Comparator.comparing(ConfigurationMethod::getInternalSignature))),
504-
JsonPrintable::printJson);
501+
Comparator<ConfigurationMethod> methodComparator = Comparator.comparing(ConfigurationMethod::getName)
502+
.thenComparing(Comparator.nullsFirst(Comparator.comparing(ConfigurationMethod::getInternalSignature)));
503+
JsonPrinter.printCollection(writer, accessedMethods, methodComparator, JsonPrintable::printJson);
505504
}
506505
}
507506

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/MissingRegistrationUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.oracle.svm.configure.config.ConfigurationType;
4848
import com.oracle.svm.core.util.ExitStatus;
4949
import com.oracle.svm.core.util.VMError;
50+
import com.oracle.svm.util.StringUtil;
5051

5152
import jdk.graal.compiler.java.LambdaUtils;
5253
import jdk.graal.compiler.util.json.JsonPrettyWriter;
@@ -204,7 +205,7 @@ protected static String typeDescriptor(Class<?> clazz) {
204205
if (Proxy.isProxyClass(clazz)) {
205206
return "proxy class inheriting " + interfacesString(clazz.getInterfaces());
206207
} else if (LambdaUtils.isLambdaClass(clazz)) {
207-
String declaringClass = LambdaUtils.capturingClass(clazz.getTypeName());
208+
String declaringClass = StringUtil.split(clazz.getTypeName(), LambdaUtils.LAMBDA_CLASS_NAME_SUBSTRING)[0];
208209
return "lambda-proxy class declared in " + quote(declaringClass) + " inheriting " + interfacesString(clazz.getInterfaces());
209210
} else {
210211
return quote(clazz.getTypeName());

0 commit comments

Comments
 (0)