Skip to content

Commit bad76ca

Browse files
Initialize maps & sets with size where possible.
1 parent 344d6c4 commit bad76ca

12 files changed

+29
-21
lines changed

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/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/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/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/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/RepositoryFactorySupport.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ public String toString() {
725725
*/
726726
static class RepositoryValidator {
727727

728-
static Map<Class<?>, String> WELL_KNOWN_EXECUTORS = new HashMap<>();
728+
static Map<Class<?>, String> WELL_KNOWN_EXECUTORS = new HashMap<>(4, 1f);
729729

730730
static {
731731

src/main/java/org/springframework/data/repository/query/ExtensionAwareQueryMethodEvaluationContextProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public ExtensionAwareQueryMethodEvaluationContextProvider(List<? extends Evaluat
100100
*/
101101
static Map<String, Object> collectVariables(Parameters<?, ?> parameters, Object[] arguments) {
102102

103-
Map<String, Object> variables = new HashMap<>();
103+
Map<String, Object> variables = new HashMap<>(parameters.getNumberOfParameters());
104104

105105
parameters.stream()//
106106
.filter(Parameter::isSpecialParameter)//

src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ public abstract class QueryExecutionConverters {
7878
private static final boolean VAVR_PRESENT = ClassUtils.isPresent("io.vavr.control.Try",
7979
QueryExecutionConverters.class.getClassLoader());
8080

81-
private static final Set<WrapperType> WRAPPER_TYPES = new HashSet<>();
82-
private static final Set<WrapperType> UNWRAPPER_TYPES = new HashSet<WrapperType>();
81+
private static final Set<WrapperType> WRAPPER_TYPES = new HashSet<>(10, 1f);
82+
private static final Set<WrapperType> UNWRAPPER_TYPES = new HashSet<WrapperType>(10, 1f);
8383
private static final Set<Function<Object, Object>> UNWRAPPERS = new HashSet<>();
8484
private static final Set<Class<?>> ALLOWED_PAGEABLE_TYPES = new HashSet<>();
85-
private static final Map<Class<?>, ExecutionAdapter> EXECUTION_ADAPTER = new HashMap<>();
85+
private static final Map<Class<?>, ExecutionAdapter> EXECUTION_ADAPTER = new HashMap<>(3, 1f);
8686
private static final Map<Class<?>, Boolean> supportsCache = new ConcurrentHashMap<>();
8787
private static final TypeInformation<Void> VOID_INFORMATION = TypeInformation.of(Void.class);
8888

src/main/java/org/springframework/data/util/KotlinBeanInfoFactory.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.beans.SimpleBeanInfo;
3030
import java.lang.reflect.Method;
3131
import java.util.Arrays;
32+
import java.util.Collection;
3233
import java.util.LinkedHashSet;
3334
import java.util.Set;
3435

@@ -59,9 +60,10 @@ public BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException {
5960
}
6061

6162
KClass<?> kotlinClass = JvmClassMappingKt.getKotlinClass(beanClass);
62-
Set<PropertyDescriptor> pds = new LinkedHashSet<>();
63+
Collection<KCallable<?>> members = kotlinClass.getMembers();
64+
Set<PropertyDescriptor> pds = new LinkedHashSet<>(members.size());
6365

64-
for (KCallable<?> member : kotlinClass.getMembers()) {
66+
for (KCallable<?> member : members) {
6567

6668
if (member instanceof KProperty<?> property) {
6769

src/main/java/org/springframework/data/util/TypeCollector.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private List<Class<?>> collect() {
242242

243243
static class InspectionCache {
244244

245-
private final Map<String, ResolvableType> mutableCache = new LinkedHashMap<>();
245+
private final Map<String, ResolvableType> mutableCache = new HashMap<>();
246246

247247
public void add(ResolvableType resolvableType) {
248248
mutableCache.put(resolvableType.toString(), resolvableType);

0 commit comments

Comments
 (0)