Skip to content

AnnotationUtils should use threadsafe ConcurrentMap (contention) [SPR-12878] #17476

Closed

Description

@spring-projects-issues

Johann Burkard opened SPR-12878 and commented

	private static final Map<Class<?>, Boolean> annotatedInterfaceCache = new WeakHashMap<Class<?>, Boolean>();
...
	private static boolean isInterfaceWithAnnotatedMethods(Class<?> iface) {
		synchronized (annotatedInterfaceCache) {
			Boolean flag = annotatedInterfaceCache.get(iface);

is a point of contention in my app and should be replaced by something like ConcurrentSkipListMap (but not ConcurrentHashMap because ConcurrentSkipListMap uses 1/10th the memory initially). Here's a Comparator for WeakReference:

import java.lang.ref.WeakReference;
import java.util.Comparator;

import org.springframework.util.ObjectUtils;

public class WeakReferenceClassComparator implements Comparator<WeakReference<Class>> {

    @Override
    public int compare(WeakReference<Class> o1, WeakReference<Class> o2) {
        if (o1.get() == null && o2.get() == null) {
            return 0;
        }
        return new Integer(ObjectUtils.nullSafeHashCode(o1.get())).compareTo(ObjectUtils.nullSafeHashCode(o2.get()));
    }

}

Affects: 4.0.5, 4.0.8

Issue Links:

Activity

  1. spring-projects-issues commented on Apr 1, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    This has been addressed in the 4.1 line already, as part of #16501, using our own ConcurrentReferenceHashMap there now.

    Note that the 4.0.x line has ended in the meantime, with no further releases planned. Please upgrade to 4.1.6 right away!

    Juergen

  2. spring-projects-issues commented on Apr 1, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Johann Burkard commented

    Thanks for the update, Jürgen. I'll tro to update.

  3. added
    type: enhancement
    in: core
    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