Cache generic bean type lookups - #37142
Open
gregjotau wants to merge 1 commit into
Open
Conversation
Cache ResolvableType-based bean-name queries once configuration is frozen, matching existing raw Class lookup behavior. Invalidate entries when a matching singleton appears and clear them with other by-type caches. Signed-off-by: GT <gregjotau@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Repeated
getBeanNamesForType(ResolvableType)calls currently rescan every bean definition even after configuration is frozen. This differs from theClassoverload, which caches its results.This showed up in an allocation profile of a large Spring Boot application. Its startup made 271 generic type queries for only 51 distinct types; 220 were repeated after configuration had been frozen. In particular, creating seven
HttpSecurityinstances repeated the same top-levelCustomizer<T>lookups.A backport of this change into the application's Spring Framework 7.0.8 dependency reduced sampled allocation under
doGetBeanNamesForTypefrom an average of 490.1 MiB to 308.3 MiB (-37.1%). Allocation belowisTypeMatchandResolvableTypefell from 283.3 MiB to 112.8 MiB (-60.2%). Whole-application startup time was too noisy to claim a reliable improvement because Hibernate query parsing and concurrent JIT compilation dominate that measurement.The included JMH benchmark performs a repeated generic lookup in a frozen bean factory containing 1,001 bean definitions. On JDK 25.0.4, Apple Silicon:
The new caches mirror the existing raw-type caches. They are used only for frozen configuration with eager initialization allowed, reject keys containing classes that are unsafe for the bean class loader, invalidate matching entries when a singleton appears, and are cleared with the other by-type caches. Tests cover both all-bean and singleton-only caches, invalidation by matching and nonmatching singletons, and recursive generic bounds.