Skip to content

Unnecessarily conservative locking in registerDependentBean is a bottleneck [SPR-8814] #13456

Closed

Description

@spring-projects-issues

Dennis Homann opened SPR-8814 and commented

In a highly concurrent application with a high rate of prototype bean instantiations, injection of (singleton) bean references into the prototype beans becomes a bottleneck due to unnecessary synchonization in DefaultSingletonBeanRegistry#registerDependentBean.

registerDependentBean updates the two ConcurrentHashMaps dependentBeanMap and dependenciesForBeanMap. For both maps, the method ensures that it maps the singleton bean name to a LinkedHashSet (will be added, if not present) and that this set contains the dependent prototype bean name. A synchronized block guards both map manipulations, leading to contention and a severe performance bottleneck under high load.

Since both maps are already ConcurrentHashMaps, the map manipulation may be refactored to be non-blocking. The following suggestion for dependentBeanMap applies in the same way to dependenciesForBeanMap. It may also be applied to some other ConcurrentHashMap manipulations in DefaultSingletonBeanRegistry, however those are not as performance-critical.

// change type of field from Map to ConcurrentMap
private final ConcurrentMap<String, Set<String>> dependentBeanMap = new ConcurrentHashMap<String, Set<String>>();

public void registerDependentBean(String beanName, String dependentBeanName) {
  String canonicalName = canonicalName(beanName);
  getMappedSet(dependentBeanMap, canonicalName).add(dependentBeanName);
  getMappedSet(dependenciesForBeanMap, dependentBeanName).add(canonicalName);
}

private Set<String> getMappedSet(ConcurrentMap<String, Set<String>> map, String name) {
  // a simple putIfAbsent would be enough to be functionally correct,
  // but calling get first will avoid creation of the set in the common case that a mapping already exists

  Set<String> set = map.get(name);
  return set != null ? set : map.putIfAbsent(name, Collections.synchronizedSet(new LinkedHashSet<String>(8)));
}

After the change the set will be synchronized, but contention on the contained sets will be much lower than on a global map lock.


Affects: 3.0.5

Issue Links:

3 votes, 6 watchers

Activity

  1. spring-projects-issues commented on Aug 20, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    I'm reducing this to the registerDependentBean problem where there is indeed rather straightforward potential for optimization. We can't remove synchronization completely for the case where we need to add entries to the LinkedHashSet (since that would otherwise be completely unprotected) but we can provide a quick advance check for an existing entry and then skip full synchronization in that case...

    Juergen

  2. spring-projects-issues commented on Aug 20, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    DefaultSingletonBeanRegistry performs non-synchronized checks for existing entries in dependentBeanMap and containedBeanMap now.

    Juergen

  3. added
    type: enhancement
    in: core
    on Jan 11, 2019
  4. added this to the 4.1 GA milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions