From e7f91bab78b05460966f24eded91632776c17d1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Thu, 17 Nov 2022 15:43:22 +0100 Subject: [PATCH] Refine BackgroundPreinitializer code reachability GraalVM code reachability algorithm is not able to evaluate at build time that ENABLED static field is always false in a native image, shipping unreachable code. Before this commit, building a native image with -H:+PrintAnalysisCallTree shows that the following methods are included: - BackgroundPreinitializer$1.(BackgroundPreinitializer):void - BackgroundPreinitializer$1.run():void - BackgroundPreinitializer$1.runSafely(Runnable):void - BackgroundPreinitializer$CharsetInitializer.run():void - BackgroundPreinitializer$ConversionServiceInitializer.run():void - BackgroundPreinitializer$JacksonInitializer.run():void - BackgroundPreinitializer$MessageConverterInitializer.run():void - BackgroundPreinitializer$ValidationInitializer.run():void - BackgroundPreinitializer.():void - BackgroundPreinitializer.getOrder():int - BackgroundPreinitializer.onApplicationEvent(ApplicationEvent):void - BackgroundPreinitializer.onApplicationEvent(SpringApplicationEvent):void - BackgroundPreinitializer.performPreinitialization():void After this commit, only those are included: - BackgroundPreinitializer.():void - BackgroundPreinitializer.getOrder():int - BackgroundPreinitializer.onApplicationEvent(ApplicationEvent):void - BackgroundPreinitializer.onApplicationEvent(SpringApplicationEvent):void See gh-33240 --- .../boot/autoconfigure/BackgroundPreinitializer.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java index 56268dbf59c..def33294a41 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/BackgroundPreinitializer.java @@ -64,12 +64,8 @@ public class BackgroundPreinitializer implements ApplicationListener 1; - } + private static final boolean ENABLED = !Boolean.getBoolean(IGNORE_BACKGROUNDPREINITIALIZER_PROPERTY_NAME) + && Runtime.getRuntime().availableProcessors() > 1; @Override public int getOrder() { @@ -78,7 +74,7 @@ public class BackgroundPreinitializer implements ApplicationListener