Skip to content

Improve auto-configuration sorting performance - #51867

Merged
philwebb merged 2 commits into
spring-projects:mainfrom
aalsanie:perf/auto-configuration-sorter
Sep 28, 2026
Merged

philwebb merged 2 commits into
spring-projects:mainfrom
aalsanie:perf/auto-configuration-sorter

Conversation

@aalsanie

@aalsanie aalsanie commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

AutoConfigurationSorter currently performs repeated linear scans while resolving ordering relationships. getClassesRequestedAfter() scans all known auto configrations to reverse @AutoConfigureBefore, while annotation traversal repeatedly uses indexOf(), contains(), and remove from the front of ArrayList.

Related to #51643, which identified the same scaling problem. This change takes a different approach by building reverse @AutoConfigureBefore relationships while the auto-configuration graph is created and using stable sort ranks during traversal.

Here, I remove those repeated scans while keeping the existing ordering behavior.

Performance

Measured against main.

JMH configuration: 3 forks, 5 × 1-second warmup iterations, and 8 × 1-second measurement iterations.

Workload main This change Reduction
NONE, 1024 10,881.55 ms/op 276.46 ms/op 97.46%
MIXED, 1024 26,345.35 ms/op 436.63 ms/op 98.34%
FAN_IN, 1024 59,197.58 ms/op 393.20 ms/op 99.34%

FAN_IN scaling:

Candidates main This change
256 2,838.82 ms/op 81.13 ms/op
1024 59,197.58 ms/op 393.20 ms/op
2048 264,051.02 ms/op 903.91 ms/op

I also tested the change in a spring boot application with 1024 ordered synthetic auto-configurations. I ran 20 fresh JVM starts for each version, alternating between baseline and optimized runs.

main This change
P25 1698.92 ms 1648.76 ms
Median 1712.04 ms 1661.24 ms
P75 1731.78 ms 1669.25 ms

Median context startup improved by 50.806 ms (2.97%). Here is the benchmark sources with reproduction commands:

https://github.com/aalsanie/spring-boot-auto-configuration-sorter-benchmarks

Verification

Additional tests cover stable ordering for mixed before/after/order relationships, reversed input, duplicate input names, transitively discovered classes, and exact cycle reporting.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Sep 27, 2026
@philwebb philwebb added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Sep 28, 2026
@philwebb philwebb added this to the 4.2.x milestone Sep 28, 2026
@philwebb philwebb self-assigned this Sep 28, 2026
aalsanie and others added 2 commits September 28, 2026 15:33
Update `AutoConfigurationSorter` to improve performance by  building
reverse `@AutoConfigureBefore` relationships while the
auto-configuration graph is created and using stable sort ranks during
traversal.

See spring-projectsgh-51867

Signed-off-by: aalsanie <ahmad.alsanie@hotmail.com>
@philwebb
philwebb force-pushed the perf/auto-configuration-sorter branch from eaf1e10 to 875efab Compare September 28, 2026 22:36
@philwebb
philwebb merged commit b00a873 into spring-projects:main Sep 28, 2026
2 of 3 checks passed
@philwebb philwebb modified the milestones: 4.2.x, 4.2.0, 4.2.0-RC1 Sep 28, 2026
@philwebb

Copy link
Copy Markdown
Member

Thanks very much @aalsanie!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants