Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching of autowire candidate metadata for lazy beans #24904

Closed
rahulsh1 opened this issue Apr 14, 2020 · 0 comments
Closed

Caching of autowire candidate metadata for lazy beans #24904

rahulsh1 opened this issue Apr 14, 2020 · 0 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Milestone

Comments

@rahulsh1
Copy link

rahulsh1 commented Apr 14, 2020

Affects: \5.1.10.RELEASE


Ran into a performance regression when some of the beans in my application were lazified.
Running this with Spring 5.1.10.RELEASE.

The hotspot was identified to be the synchronized lock taken in SimpleAliasRegistry::getAliases.
See stack trace

at org.springframework.core.SimpleAliasRegistry.getAliases(SimpleAliasRegistry.java:131)
   at org.springframework.beans.factory.support.AbstractBeanFactory.getAliases(AbstractBeanFactory.java:670)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.isAutowireCandidate(DefaultListableBeanFactory.java:763)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.isAutowireCandidate(DefaultListableBeanFactory.java:725)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.isAutowireCandidate(DefaultListableBeanFactory.java:709)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1434)
   at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1218)
   at org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver$1.getTarget(ContextAnnotationAutowireCandidateResolver.java:90)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:192)

The issue appears to be coming from the synchronized block in the following code.

public String[] getAliases(String name) {
  List<String> result = new ArrayList<>();
  synchronized (this.aliasMap) {  // <--- THIS ONE
    retrieveAliases(name, result);
  }
  return StringUtils.toStringArray(result);
}

Since there is a single bean factory instance, the contention makes sense to me.
However looking further down in the stack trace, I see findAutowireCandidates being called.
The above stack frame can be seen for every invocation of a method on the several beans that were lazified.
I was hoping this gets cached the first time the resolution was needed and then it is a simple method indirection but that doesn't seem to be the case.

When I benchmarked, findAutowireCandidates took between 30-100 microseconds with ~50 invocations total per second on the various beans that has caused the regression.

Is this a bug?
Wondering if there is a better approach to caching the candidates so that the lookup cost in not incurred for each invocation. This overhead is only seen for Lazy beans.
What possible solution(s) could be used to address the regression caused by making beans lazy?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 14, 2020
@jhoeller jhoeller self-assigned this Apr 14, 2020
@jhoeller jhoeller added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 14, 2020
@jhoeller jhoeller added this to the 5.2.6 milestone Apr 14, 2020
@jhoeller jhoeller changed the title Caching of Autowired candidates for lazy beans Caching of autowire candidate metadata for lazy beans Apr 15, 2020
@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.1.x labels Apr 25, 2020
zx20110729 pushed a commit to zx20110729/spring-framework that referenced this issue Feb 18, 2022
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: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants