You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Further locking optimizations for the retrieval of non-singleton beans [SPR-12250] #16864
in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: enhancementA general enhancement
The current BeanFactory.getBean algorithm is optimized towards singleton access, always checking the singleton map for a quick check before proceeding with scope handling.
Lock-free access to non-singleton beans is equally important and seems to be easy enough to achieve: through a check on registered singleton names before going into the lock on the singleton map.
In fact, after #14452, all that we still do is a quick check on the "singletonObjects" ConcurrentHashMap and an equally efficient "isSingletonCurrentlyInCreation" check, against a ConcurrentHashMap as well, both with plain bean name String keys. This is very efficient and hard to beat with any singleton names check on an additional data structure.
Let's use this issue as an opportunity for comments towards further improvements, if there are any ideas.
AFAICT the only way to reduce lock contention would be to do a get/containsKey beforehand and return early if it returns true. This creates the possibility of a race condition in which multiple threads actually put the value in the map, but this does not change semantics and is still an improvement to the current situation wrt lock contention.
Good point. Addressed this through an explicit contains check before the add call now - for AbstractBeanFactory.alreadyCreated as well as AbstractAutoProxyCreator.earlyProxyReferences.
in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: enhancementA general enhancement
Activity
spring-projects-issues commentedon Sep 25, 2014
Juergen Hoeller commented
In fact, after #14452, all that we still do is a quick check on the "singletonObjects"
ConcurrentHashMapand an equally efficient "isSingletonCurrentlyInCreation" check, against aConcurrentHashMapas well, both with plain bean name String keys. This is very efficient and hard to beat with any singleton names check on an additional data structure.Let's use this issue as an opportunity for comments towards further improvements, if there are any ideas.
Juergen
spring-projects-issues commentedon Oct 6, 2014
Juergen Hoeller commented
As discussed in #14414, we should be able to optimize
markBeanAsCreatedperformance through the use ofputIfAbsent.Juergen
spring-projects-issues commentedon Oct 6, 2014
Bernhard Frauendienst commented
Unfortunately
putIfAbsentis not optimized that way, it will also lock the segment (note the synchronized block in ConcurrentHashMap.java:1027 and theonlyIfAbsentcheck in ConcurrentHashMap.java:1037).AFAICT the only way to reduce lock contention would be to do a
get/containsKeybeforehand and return early if it returns true. This creates the possibility of a race condition in which multiple threads actually put the value in the map, but this does not change semantics and is still an improvement to the current situation wrt lock contention.spring-projects-issues commentedon Oct 6, 2014
Juergen Hoeller commented
Good point. Addressed this through an explicit
containscheck before theaddcall now - forAbstractBeanFactory.alreadyCreatedas well asAbstractAutoProxyCreator.earlyProxyReferences.Juergen