Skip to content

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

Closed
@spring-projects-issues

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:

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions