Skip to content

Cache generic bean type lookups - #37142

Open
gregjotau wants to merge 1 commit into
spring-projects:mainfrom
gregjotau:cache-generic-bean-type-lookups
Open

Cache generic bean type lookups#37142
gregjotau wants to merge 1 commit into
spring-projects:mainfrom
gregjotau:cache-generic-bean-type-lookups

Conversation

@gregjotau

Copy link
Copy Markdown

Repeated getBeanNamesForType(ResolvableType) calls currently rescan every bean definition even after configuration is frozen. This differs from the Class overload, 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 HttpSecurity instances repeated the same top-level Customizer<T> lookups.

A backport of this change into the application's Spring Framework 7.0.8 dependency reduced sampled allocation under doGetBeanNamesForType from an average of 490.1 MiB to 308.3 MiB (-37.1%). Allocation below isTypeMatch and ResolvableType fell 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:

Throughput Allocation
Before 32,333 ops/s 192,648 B/op
After 422,750,679 ops/s approximately 0 B/op

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.

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>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Aug 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged or decided on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants