Avoid resizing of fixed-size HashMap/LinkedHashMap variants
Closes gh-25349
This commit is contained in:
@@ -49,6 +49,7 @@ import org.springframework.core.ResolvableType;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.ConcurrentReferenceHashMap;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -792,7 +793,7 @@ public abstract class BeanUtils {
|
||||
}
|
||||
|
||||
List<KParameter> parameters = kotlinConstructor.getParameters();
|
||||
Map<KParameter, Object> argParameters = new HashMap<>(parameters.size());
|
||||
Map<KParameter, Object> argParameters = CollectionUtils.newHashMap(parameters.size());
|
||||
Assert.isTrue(args.length <= parameters.size(),
|
||||
"Number of provided arguments should be less of equals than number of constructor parameters");
|
||||
for (int i = 0 ; i < args.length ; i++) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,13 +16,13 @@
|
||||
|
||||
package org.springframework.beans.factory.config;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.TypeConverter;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* Simple factory for shared Map instances. Allows for central setup
|
||||
@@ -85,7 +85,7 @@ public class MapFactoryBean extends AbstractFactoryBean<Map<Object, Object>> {
|
||||
result = BeanUtils.instantiateClass(this.targetMapClass);
|
||||
}
|
||||
else {
|
||||
result = new LinkedHashMap<>(this.sourceMap.size());
|
||||
result = CollectionUtils.newLinkedHashMap(this.sourceMap.size());
|
||||
}
|
||||
Class<?> keyType = null;
|
||||
Class<?> valueType = null;
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.beans.factory.support;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -42,6 +41,7 @@ import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.config.TypedStringValue;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -447,7 +447,7 @@ class BeanDefinitionValueResolver {
|
||||
* For each element in the managed map, resolve reference if necessary.
|
||||
*/
|
||||
private Map<?, ?> resolveManagedMap(Object argName, Map<?, ?> mm) {
|
||||
Map<Object, Object> resolved = new LinkedHashMap<>(mm.size());
|
||||
Map<Object, Object> resolved = CollectionUtils.newLinkedHashMap(mm.size());
|
||||
mm.forEach((key, value) -> {
|
||||
Object resolvedKey = resolveValueIfNecessary(argName, key);
|
||||
Object resolvedValue = resolveValueIfNecessary(new KeyedArgName(argName, key), value);
|
||||
|
||||
@@ -33,7 +33,6 @@ import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -81,6 +80,7 @@ import org.springframework.core.metrics.StartupStep;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.CompositeIterator;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -476,7 +476,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
if (beanNames.length == 0) {
|
||||
return Stream.empty();
|
||||
}
|
||||
Map<String, T> matchingBeans = new LinkedHashMap<>(beanNames.length);
|
||||
Map<String, T> matchingBeans = CollectionUtils.newLinkedHashMap(beanNames.length);
|
||||
for (String beanName : beanNames) {
|
||||
Object beanInstance = getBean(beanName);
|
||||
if (!(beanInstance instanceof NullBean)) {
|
||||
@@ -665,7 +665,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
@Nullable Class<T> type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException {
|
||||
|
||||
String[] beanNames = getBeanNamesForType(type, includeNonSingletons, allowEagerInit);
|
||||
Map<String, T> result = new LinkedHashMap<>(beanNames.length);
|
||||
Map<String, T> result = CollectionUtils.newLinkedHashMap(beanNames.length);
|
||||
for (String beanName : beanNames) {
|
||||
try {
|
||||
Object beanInstance = getBean(beanName);
|
||||
@@ -715,7 +715,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
@Override
|
||||
public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
|
||||
String[] beanNames = getBeanNamesForAnnotation(annotationType);
|
||||
Map<String, Object> result = new LinkedHashMap<>(beanNames.length);
|
||||
Map<String, Object> result = CollectionUtils.newLinkedHashMap(beanNames.length);
|
||||
for (String beanName : beanNames) {
|
||||
Object beanInstance = getBean(beanName);
|
||||
if (!(beanInstance instanceof NullBean)) {
|
||||
@@ -1235,7 +1235,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
return new NamedBeanHolder<>(beanName, (T) getBean(beanName, requiredType.toClass(), args));
|
||||
}
|
||||
else if (candidateNames.length > 1) {
|
||||
Map<String, Object> candidates = new LinkedHashMap<>(candidateNames.length);
|
||||
Map<String, Object> candidates = CollectionUtils.newLinkedHashMap(candidateNames.length);
|
||||
for (String beanName : candidateNames) {
|
||||
if (containsSingleton(beanName) && args == null) {
|
||||
Object beanInstance = getBean(beanName);
|
||||
@@ -1532,7 +1532,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
|
||||
|
||||
String[] candidateNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
|
||||
this, requiredType, true, descriptor.isEager());
|
||||
Map<String, Object> result = new LinkedHashMap<>(candidateNames.length);
|
||||
Map<String, Object> result = CollectionUtils.newLinkedHashMap(candidateNames.length);
|
||||
for (Map.Entry<Class<?>, Object> classObjectEntry : this.resolvableDependencies.entrySet()) {
|
||||
Class<?> autowiringType = classObjectEntry.getKey();
|
||||
if (autowiringType.isAssignableFrom(requiredType)) {
|
||||
|
||||
Reference in New Issue
Block a user