Skip to content

Commit b21b2e8

Browse files
christophstroblmp911de
authored andcommitted
Align internal caches with core framework patterns.
Initialize maps and sets with size where possible. Closes #3067 Original pull request: #3073
1 parent d4301ee commit b21b2e8

27 files changed

+59
-58
lines changed

src/main/java/org/springframework/data/auditing/MappingAuditableBeanWrapperFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.time.temporal.TemporalAccessor;
2020
import java.util.Map;
2121
import java.util.Optional;
22+
import java.util.concurrent.ConcurrentHashMap;
2223
import java.util.function.Predicate;
2324
import java.util.stream.Stream;
2425

@@ -40,7 +41,6 @@
4041
import org.springframework.data.mapping.context.PersistentEntities;
4142
import org.springframework.data.util.Lazy;
4243
import org.springframework.util.Assert;
43-
import org.springframework.util.ConcurrentReferenceHashMap;
4444

4545
/**
4646
* {@link AuditableBeanWrapperFactory} that will create am {@link AuditableBeanWrapper} using mapping information
@@ -67,7 +67,7 @@ public MappingAuditableBeanWrapperFactory(PersistentEntities entities) {
6767
Assert.notNull(entities, "PersistentEntities must not be null");
6868

6969
this.entities = entities;
70-
this.metadataCache = new ConcurrentReferenceHashMap<>();
70+
this.metadataCache = new ConcurrentHashMap<>();
7171
}
7272

7373
@Override

src/main/java/org/springframework/data/convert/SimplePropertyValueConverterRegistry.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232
public class SimplePropertyValueConverterRegistry<P extends PersistentProperty<P>>
3333
implements ValueConverterRegistry<P> {
3434

35-
private final Map<Key, PropertyValueConverter<?, ?, ? extends ValueConversionContext<P>>> converterRegistrationMap = new LinkedHashMap<>();
35+
private final Map<Key, PropertyValueConverter<?, ?, ? extends ValueConversionContext<P>>> converterRegistrationMap;
3636

37-
public SimplePropertyValueConverterRegistry() {}
37+
public SimplePropertyValueConverterRegistry() {
38+
this.converterRegistrationMap = new LinkedHashMap<>();
39+
}
3840

3941
SimplePropertyValueConverterRegistry(SimplePropertyValueConverterRegistry<P> source) {
40-
this.converterRegistrationMap.putAll(source.converterRegistrationMap);
42+
this.converterRegistrationMap = new LinkedHashMap<>(source.converterRegistrationMap);
4143
}
4244

4345
@Override

src/main/java/org/springframework/data/domain/ExampleMatcher.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -775,12 +775,14 @@ protected boolean canEqual(final Object other) {
775775
*/
776776
class PropertySpecifiers {
777777

778-
private final Map<String, PropertySpecifier> propertySpecifiers = new LinkedHashMap<>();
778+
private final Map<String, PropertySpecifier> propertySpecifiers;
779779

780-
PropertySpecifiers() {}
780+
PropertySpecifiers() {
781+
this. propertySpecifiers = new LinkedHashMap<>();
782+
}
781783

782784
PropertySpecifiers(PropertySpecifiers propertySpecifiers) {
783-
this.propertySpecifiers.putAll(propertySpecifiers.propertySpecifiers);
785+
this.propertySpecifiers = new LinkedHashMap<>(propertySpecifiers.propertySpecifiers);
784786
}
785787

786788
public void add(PropertySpecifier specifier) {

src/main/java/org/springframework/data/geo/format/DistanceFormatter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public enum DistanceFormatter implements Converter<String, Distance>, Formatter<
4747

4848
static {
4949

50-
Map<String, Metric> metrics = new LinkedHashMap<>();
50+
Map<String, Metric> metrics = new LinkedHashMap<>(Metrics.values().length);
5151

5252
for (Metric metric : Metrics.values()) {
5353
metrics.put(metric.getAbbreviation(), metric);

src/main/java/org/springframework/data/mapping/callback/DefaultEntityCallbacks.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.lang.reflect.Method;
1919
import java.util.Map;
20+
import java.util.concurrent.ConcurrentHashMap;
2021
import java.util.function.BiFunction;
2122

2223
import org.apache.commons.logging.Log;
@@ -26,7 +27,6 @@
2627
import org.springframework.core.ResolvableType;
2728
import org.springframework.util.Assert;
2829
import org.springframework.util.ClassUtils;
29-
import org.springframework.util.ConcurrentReferenceHashMap;
3030
import org.springframework.util.ReflectionUtils;
3131

3232
/**
@@ -40,7 +40,7 @@
4040
*/
4141
class DefaultEntityCallbacks implements EntityCallbacks {
4242

43-
private final Map<Class<?>, Method> callbackMethodCache = new ConcurrentReferenceHashMap<>(64);
43+
private final Map<Class<?>, Method> callbackMethodCache = new ConcurrentHashMap<>(64);
4444
private final SimpleEntityCallbackInvoker callbackInvoker = new SimpleEntityCallbackInvoker();
4545
private final EntityCallbackDiscoverer callbackDiscoverer;
4646

src/main/java/org/springframework/data/mapping/callback/DefaultReactiveEntityCallbacks.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.lang.reflect.Method;
2121
import java.util.Map;
22+
import java.util.concurrent.ConcurrentHashMap;
2223
import java.util.function.BiFunction;
2324

2425
import org.apache.commons.logging.Log;
@@ -28,7 +29,6 @@
2829
import org.springframework.core.ResolvableType;
2930
import org.springframework.util.Assert;
3031
import org.springframework.util.ClassUtils;
31-
import org.springframework.util.ConcurrentReferenceHashMap;
3232
import org.springframework.util.ReflectionUtils;
3333

3434
/**
@@ -41,7 +41,7 @@
4141
*/
4242
class DefaultReactiveEntityCallbacks implements ReactiveEntityCallbacks {
4343

44-
private final Map<Class<?>, Method> callbackMethodCache = new ConcurrentReferenceHashMap<>(64);
44+
private final Map<Class<?>, Method> callbackMethodCache = new ConcurrentHashMap<>(64);
4545
private final ReactiveEntityCallbackInvoker callbackInvoker = new DefaultReactiveEntityCallbackInvoker();
4646
private final EntityCallbackDiscoverer callbackDiscoverer;
4747

src/main/java/org/springframework/data/mapping/callback/EntityCallbackDiscoverer.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.springframework.lang.Nullable;
3939
import org.springframework.util.Assert;
4040
import org.springframework.util.ClassUtils;
41-
import org.springframework.util.ConcurrentReferenceHashMap;
4241
import org.springframework.util.ReflectionUtils;
4342
import org.springframework.util.comparator.Comparators;
4443

@@ -54,7 +53,7 @@ class EntityCallbackDiscoverer {
5453

5554
private final CallbackRetriever defaultRetriever = new CallbackRetriever();
5655
private final Map<CallbackCacheKey, CallbackRetriever> retrieverCache = new ConcurrentHashMap<>(64);
57-
private final Map<Class<?>, ResolvableType> entityTypeCache = new ConcurrentReferenceHashMap<>(64);
56+
private final Map<Class<?>, ResolvableType> entityTypeCache = new ConcurrentHashMap<>(64);
5857

5958
@Nullable private ClassLoader beanClassLoader;
6059

src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ private E doAddPersistentEntity(TypeInformation<?> typeInformation) {
461461

462462
if (shouldCreateProperties(userTypeInformation)) {
463463
PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(type);
464-
Map<String, PropertyDescriptor> descriptors = new HashMap<>();
464+
Map<String, PropertyDescriptor> descriptors = new HashMap<>(pds.length);
465465

466466
for (PropertyDescriptor descriptor : pds) {
467467
descriptors.put(descriptor.getName(), descriptor);

src/main/java/org/springframework/data/mapping/context/PersistentPropertyPathFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.mapping.context;
1717

1818
import java.util.*;
19+
import java.util.concurrent.ConcurrentHashMap;
1920
import java.util.function.Function;
2021
import java.util.function.Predicate;
2122
import java.util.stream.Collectors;
@@ -33,7 +34,6 @@
3334
import org.springframework.data.util.TypeInformation;
3435
import org.springframework.lang.Nullable;
3536
import org.springframework.util.Assert;
36-
import org.springframework.util.ConcurrentReferenceHashMap;
3737
import org.springframework.util.StringUtils;
3838

3939
/**
@@ -49,7 +49,7 @@ class PersistentPropertyPathFactory<E extends PersistentEntity<?, P>, P extends
4949

5050
private static final Predicate<PersistentProperty<? extends PersistentProperty<?>>> IS_ENTITY = PersistentProperty::isEntity;
5151

52-
private final Map<TypeAndPath, PathResolution> propertyPaths = new ConcurrentReferenceHashMap<>();
52+
private final Map<TypeAndPath, PathResolution> propertyPaths = new ConcurrentHashMap<>();
5353
private final MappingContext<E, P> context;
5454

5555
public PersistentPropertyPathFactory(MappingContext<E, P> context) {

src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Optional;
2828
import java.util.Set;
2929
import java.util.TreeSet;
30+
import java.util.concurrent.ConcurrentHashMap;
3031
import java.util.stream.Collectors;
3132

3233
import org.springframework.core.annotation.AnnotatedElementUtils;
@@ -46,8 +47,6 @@
4647
import org.springframework.lang.Nullable;
4748
import org.springframework.util.Assert;
4849
import org.springframework.util.CollectionUtils;
49-
import org.springframework.util.ConcurrentReferenceHashMap;
50-
import org.springframework.util.ConcurrentReferenceHashMap.ReferenceType;
5150
import org.springframework.util.MultiValueMap;
5251
import org.springframework.util.StringUtils;
5352

@@ -117,9 +116,9 @@ public BasicPersistentEntity(TypeInformation<T> information, @Nullable Comparato
117116
this.associations = comparator == null ? new HashSet<>() : new TreeSet<>(new AssociationComparator<>(comparator));
118117

119118
this.propertyCache = new HashMap<>(16, 1f);
120-
this.annotationCache = new ConcurrentReferenceHashMap<>(16, ReferenceType.WEAK);
119+
this.annotationCache = new ConcurrentHashMap<>(16);
121120
this.propertyAnnotationCache = CollectionUtils
122-
.toMultiValueMap(new ConcurrentReferenceHashMap<>(16, ReferenceType.WEAK));
121+
.toMultiValueMap(new ConcurrentHashMap<>(16));
123122
this.propertyAccessorFactory = BeanWrapperPropertyAccessorFactory.INSTANCE;
124123
this.typeAlias = Lazy.of(() -> getAliasFromAnnotation(getType()));
125124
this.isNewStrategy = Lazy.of(() -> Persistable.class.isAssignableFrom(information.getType()) //

src/main/java/org/springframework/data/mapping/model/BeanWrapper.java

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.springframework.data.util.KotlinReflectionUtils;
3333
import org.springframework.lang.Nullable;
3434
import org.springframework.util.Assert;
35-
import org.springframework.util.ConcurrentReferenceHashMap;
3635
import org.springframework.util.ReflectionUtils;
3736

3837
/**

src/main/java/org/springframework/data/mapping/model/ClassGeneratingPropertyAccessorFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,7 @@ private static int getInvokeOp(Method method, boolean interfaceDefinition) {
13951395
private static Map<String, PropertyStackAddress> createPropertyStackMap(
13961396
List<PersistentProperty<?>> persistentProperties) {
13971397

1398-
Map<String, PropertyStackAddress> stackmap = new HashMap<>();
1398+
Map<String, PropertyStackAddress> stackmap = new HashMap<>(persistentProperties.size());
13991399

14001400
for (PersistentProperty<?> property : persistentProperties) {
14011401
stackmap.put(property.getName(), new PropertyStackAddress(new Label(), property.getName().hashCode()));

src/main/java/org/springframework/data/projection/DefaultMethodInvokingMethodInterceptor.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@
2222
import java.lang.reflect.Method;
2323
import java.lang.reflect.Modifier;
2424
import java.util.Map;
25+
import java.util.concurrent.ConcurrentHashMap;
2526
import java.util.concurrent.atomic.AtomicBoolean;
2627

2728
import org.aopalliance.intercept.MethodInterceptor;
2829
import org.aopalliance.intercept.MethodInvocation;
2930
import org.springframework.aop.ProxyMethodInvocation;
3031
import org.springframework.lang.Nullable;
31-
import org.springframework.util.ConcurrentReferenceHashMap;
32-
import org.springframework.util.ConcurrentReferenceHashMap.ReferenceType;
3332
import org.springframework.util.ReflectionUtils;
3433

3534
/**
@@ -43,7 +42,7 @@
4342
public class DefaultMethodInvokingMethodInterceptor implements MethodInterceptor {
4443

4544
private static final Lookup LOOKUP = MethodHandles.lookup();
46-
private final Map<Method, MethodHandle> methodHandleCache = new ConcurrentReferenceHashMap<>(10, ReferenceType.WEAK);
45+
private final Map<Method, MethodHandle> methodHandleCache = new ConcurrentHashMap<>();
4746

4847
/**
4948
* Returns whether the {@code interfaceClass} declares {@link Method#isDefault() default methods}.

src/main/java/org/springframework/data/projection/ProxyProjectionFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.HashMap;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.concurrent.ConcurrentHashMap;
2324

2425
import org.aopalliance.intercept.MethodInterceptor;
2526
import org.aopalliance.intercept.MethodInvocation;
@@ -34,7 +35,6 @@
3435
import org.springframework.lang.Nullable;
3536
import org.springframework.util.Assert;
3637
import org.springframework.util.ClassUtils;
37-
import org.springframework.util.ConcurrentReferenceHashMap;
3838

3939
/**
4040
* A {@link ProjectionFactory} to create JDK proxies to back interfaces and handle method invocations on them. By
@@ -60,7 +60,7 @@ class ProxyProjectionFactory implements ProjectionFactory, BeanClassLoaderAware
6060
}
6161

6262
private final List<MethodInterceptorFactory> factories;
63-
private final Map<Class<?>, ProjectionMetadata> projectionInformationCache = new ConcurrentReferenceHashMap<>();
63+
private final Map<Class<?>, ProjectionMetadata> projectionInformationCache = new ConcurrentHashMap<>();
6464
private @Nullable ClassLoader classLoader;
6565

6666
private final Lazy<DefaultMethodInvokingMethodInterceptor> defaultMethodInvokingMethodInterceptor = Lazy

src/main/java/org/springframework/data/projection/SpelEvaluatingMethodInterceptor.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ public SpelEvaluatingMethodInterceptor(MethodInterceptor delegate, Object target
107107
private static Map<Integer, Expression> potentiallyCreateExpressionsForMethodsOnTargetInterface(
108108
ExpressionParser parser, Class<?> targetInterface) {
109109

110-
Map<Integer, Expression> expressions = new HashMap<>();
110+
Method[] methods = targetInterface.getMethods();
111+
Map<Integer, Expression> expressions = new HashMap<>(methods.length);
111112

112-
for (Method method : targetInterface.getMethods()) {
113+
for (Method method : methods) {
113114

114115
Value value = AnnotationUtils.findAnnotation(method, Value.class);
115116
if (value == null) {

src/main/java/org/springframework/data/querydsl/binding/QuerydslBindingsFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.List;
1919
import java.util.Map;
2020
import java.util.Optional;
21+
import java.util.concurrent.ConcurrentHashMap;
2122
import java.util.stream.Collectors;
2223

2324
import org.springframework.beans.BeanUtils;
@@ -30,7 +31,6 @@
3031
import org.springframework.data.repository.support.Repositories;
3132
import org.springframework.data.util.TypeInformation;
3233
import org.springframework.util.Assert;
33-
import org.springframework.util.ConcurrentReferenceHashMap;
3434

3535
import com.querydsl.core.types.EntityPath;
3636

@@ -63,7 +63,7 @@ public QuerydslBindingsFactory(EntityPathResolver entityPathResolver) {
6363
Assert.notNull(entityPathResolver, "EntityPathResolver must not be null");
6464

6565
this.entityPathResolver = entityPathResolver;
66-
this.entityPaths = new ConcurrentReferenceHashMap<>();
66+
this.entityPaths = new ConcurrentHashMap<>();
6767
this.beanFactory = Optional.empty();
6868
this.repositories = Optional.empty();
6969
this.defaultCustomizer = NoOpCustomizer.INSTANCE;

src/main/java/org/springframework/data/repository/core/support/MethodInvocationValidator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.lang.annotation.ElementType;
1919
import java.lang.reflect.Method;
2020
import java.util.Map;
21+
import java.util.concurrent.ConcurrentHashMap;
2122

2223
import org.aopalliance.intercept.MethodInterceptor;
2324
import org.aopalliance.intercept.MethodInvocation;
@@ -49,7 +50,7 @@
4950
public class MethodInvocationValidator implements MethodInterceptor {
5051

5152
private final ParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();
52-
private final Map<Method, Nullability> nullabilityCache = new ConcurrentReferenceHashMap<>(16, ReferenceType.WEAK);
53+
private final Map<Method, Nullability> nullabilityCache = new ConcurrentHashMap<>(16);
5354

5455
/**
5556
* Returns {@literal true} if the {@code repositoryInterface} is supported by this interceptor.

src/main/java/org/springframework/data/repository/core/support/QueryExecutorMethodInterceptor.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ public QueryExecutorMethodInterceptor(RepositoryInformation repositoryInformatio
9292
private Map<Method, RepositoryQuery> mapMethodsToQuery(RepositoryInformation repositoryInformation,
9393
QueryLookupStrategy lookupStrategy, ProjectionFactory projectionFactory) {
9494

95-
Map<Method, RepositoryQuery> result = new HashMap<>();
95+
List<Method> queryMethods = repositoryInformation.getQueryMethods().toList();
96+
Map<Method, RepositoryQuery> result = new HashMap<>(queryMethods.size());
9697

97-
for (Method method : repositoryInformation.getQueryMethods()) {
98+
for (Method method : queryMethods) {
9899

99100
Pair<Method, RepositoryQuery> pair = lookupQuery(method, repositoryInformation, lookupStrategy,
100101
projectionFactory);

src/main/java/org/springframework/data/repository/core/support/RepositoryComposition.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.springframework.lang.Nullable;
3939
import org.springframework.util.Assert;
4040
import org.springframework.util.ClassUtils;
41-
import org.springframework.util.ConcurrentReferenceHashMap;
4241
import org.springframework.util.ObjectUtils;
4342
import org.springframework.util.ReflectionUtils;
4443

@@ -101,7 +100,7 @@ public class RepositoryComposition {
101100
private static final RepositoryComposition EMPTY = new RepositoryComposition(null, RepositoryFragments.empty(),
102101
MethodLookups.direct(), PASSTHRU_ARG_CONVERTER);
103102

104-
private final Map<Method, Method> methodCache = new ConcurrentReferenceHashMap<>();
103+
private final Map<Method, Method> methodCache = new ConcurrentHashMap<>();
105104
private final RepositoryFragments fragments;
106105
private final MethodLookup methodLookup;
107106
private final BiFunction<Method, Object[], Object[]> argumentConverter;
@@ -365,7 +364,7 @@ public static class RepositoryFragments implements Streamable<RepositoryFragment
365364

366365
static final RepositoryFragments EMPTY = new RepositoryFragments(Collections.emptyList());
367366

368-
private final Map<Method, RepositoryFragment<?>> fragmentCache = new ConcurrentReferenceHashMap<>();
367+
private final Map<Method, RepositoryFragment<?>> fragmentCache = new ConcurrentHashMap<>();
369368
private final Map<Method, RepositoryMethodInvoker> invocationMetadataCache = new ConcurrentHashMap<>();
370369
private final List<RepositoryFragment<?>> fragments;
371370

0 commit comments

Comments
 (0)