Skip to content

Cache predicted types of non-instantiated primary beans in hasPrimaryConflict - #37814

Open
egross wants to merge 2 commits into
spring-projects:mainfrom
egross:primary-conflict-cache
Open

egross wants to merge 2 commits into
spring-projects:mainfrom
egross:primary-conflict-cache

Conversation

@egross

@egross egross commented Dec 2, 2026

Copy link
Copy Markdown

Problem

Since 6.2, every dependency resolved through the by-name shortcut in
doResolveDependency calls hasPrimaryConflict, which checks each
registered primary bean against the dependency type. gh-35330 (6.2.11)
caches the type of a primary bean once its singleton instance exists, but
a primary bean that is not instantiated yet still goes through
isTypeMatch on every call, which predicts its type again each time.

During startup, that is the common case. In a large application here
(3,640 primary beans, ~2,400 of them not instantiated yet when most
dependencies get resolved), one startup phase performs ~25k by-name
resolutions, which comes to ~70M isTypeMatch calls. In profiles that
was ~57 % of the CPU time of the threads doing that work, and the phase
went from ~103 s to ~86-92 s with this change.

Change

With a frozen configuration and no temporary class loader, the type of a
primary singleton bean that is not instantiated yet and is not a
FactoryBean is predicted once and matched with ClassUtils.isAssignable,
which is what isTypeMatch computes for such a bean and a raw dependency
type. Anything else goes through isTypeMatch as before.

The cached type is dropped when it may no longer hold:

  • while the bean is in creation, and after each creation
    (afterSingletonCreation), since a target type resolved during creation
    changes the prediction, before the instance is registered and the
    Performance degradation when using singleton beans with Provider<T> #35330 cache takes over;
  • when a singleton instance is registered manually under that name;
  • with the other by-type caches (clearByTypeCache).

Prototype and other scoped beans are not cached, since creating an instance
can change their predicted type. FactoryBeans are not cached, since their
object type may only become known later.

Tests

PrimaryConflictResolutionTests has one test for each condition above.
Each test fails if its condition is removed, and all of them pass without
the change, except primaryTypePredictedOnceWithFrozenConfiguration, which
checks that the type is not predicted again:

Condition Test
frozen configuration primaryTypePredictedOnceWithFrozenConfiguration
no temporary class loader primaryTypeNotCachedWithTempClassLoader
no singleton instance registered manualFactoryBeanSingletonForPrimaryDefinition
not in creation primaryInCreationOnAnotherThread
dropped after creation primaryResolvedBetweenCreationAndRegistration
singleton scope only primaryPrototypeWithBroaderDeclaredType
not a FactoryBean primaryFactoryBeanWithObjectTypeKnownOnlyAfterInstantiation
cleared with the by-type caches primaryReplacedAfterResolution

Benchmark

DefaultListableBeanFactoryBenchmark.resolveByNameWithPrimaryBeans
(by-name resolution with N primary beans of an unrelated type, not
instantiated, frozen configuration):

Primary beans Before After
10 0.195 ± 0.001 µs/op 0.126 ± 0.007 µs/op
1000 20.96 ± 0.19 µs/op 8.26 ± 0.26 µs/op

(JDK 25.0.1, Apple M4 Max, 2 forks × 5 iterations.)

Every dependency resolved through the by-name shortcut checks each
primary bean for a conflict. A primary bean that is not instantiated
yet goes through isTypeMatch on each check, predicting its type again,
which dominates startup time with many primary beans.

With a frozen configuration and no temporary class loader, the predicted
type of a primary singleton bean that is not a FactoryBean is now cached
until the bean is created or the by-type caches are cleared.

See spring-projectsgh-35330

Signed-off-by: Eric Gross <eric.gross@salesforce.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 2, 2026
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Dec 3, 2026
A prediction computed while another thread created the primary bean, or
while the by-type caches were cleared, could be cached after the removal
in afterSingletonCreation. If that creation failed, the stale prediction
stayed in use. Track a generation that is incremented before each removal
and discard such a prediction once cached.

Signed-off-by: Eric Gross <eric.gross@salesforce.com>

This branch has not been deployed

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

Labels

in: core Issues in core modules (aop, beans, core, context, expression) 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.

3 participants