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?