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

Further locking optimizations for the retrieval of non-singleton beans [SPR-12250] #16864

Closed
spring-projects-issues opened this issue Sep 24, 2014 · 4 comments
Assignees
Labels
in: core status: backported type: enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Sep 24, 2014

Juergen Hoeller opened SPR-12250 and commented

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.


Affects: 3.2.11, 4.0.7, 4.1 GA

Issue Links:

  • #14414 AbstractBeanFactory#markBeanAsCreated performance issue due to lock contention
  • #14452 Non-singleton beans performance issue

Referenced from: commits ac4103d, 9d83281, a1538a4

Backported to: 4.0.8, 3.2.12

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Sep 25, 2014

Juergen Hoeller commented

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.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 6, 2014

Juergen Hoeller commented

As discussed in #14414, we should be able to optimize markBeanAsCreated performance through the use of putIfAbsent.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 6, 2014

Bernhard Frauendienst commented

Unfortunately putIfAbsent is not optimized that way, it will also lock the segment (note the synchronized block in ConcurrentHashMap.java:1027 and the onlyIfAbsent check in ConcurrentHashMap.java:1037).

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.

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Oct 6, 2014

Juergen Hoeller commented

Good point. Addressed this through an explicit contains check before the add call now - for AbstractBeanFactory.alreadyCreated as well as AbstractAutoProxyCreator.earlyProxyReferences.

Juergen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core status: backported type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants