Repository navigation
Conversation
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>
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
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.
Problem
Since 6.2, every dependency resolved through the by-name shortcut in
doResolveDependencycallshasPrimaryConflict, which checks eachregistered 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
isTypeMatchon 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
isTypeMatchcalls. In profiles thatwas ~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
isTypeMatchcomputes for such a bean and a raw dependencytype. Anything else goes through
isTypeMatchas before.The cached type is dropped when it may no longer hold:
(
afterSingletonCreation), since a target type resolved during creationchanges the prediction, before the instance is registered and the
Performance degradation when using singleton beans with Provider<T> #35330 cache takes over;
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
PrimaryConflictResolutionTestshas one test for each condition above.Each test fails if its condition is removed, and all of them pass without
the change, except
primaryTypePredictedOnceWithFrozenConfiguration, whichchecks that the type is not predicted again:
primaryTypePredictedOnceWithFrozenConfigurationprimaryTypeNotCachedWithTempClassLoadermanualFactoryBeanSingletonForPrimaryDefinitionprimaryInCreationOnAnotherThreadprimaryResolvedBetweenCreationAndRegistrationprimaryPrototypeWithBroaderDeclaredTypeprimaryFactoryBeanWithObjectTypeKnownOnlyAfterInstantiationprimaryReplacedAfterResolutionBenchmark
DefaultListableBeanFactoryBenchmark.resolveByNameWithPrimaryBeans(by-name resolution with N primary beans of an unrelated type, not
instantiated, frozen configuration):
(JDK 25.0.1, Apple M4 Max, 2 forks × 5 iterations.)