Skip to content

Commit

Permalink
Avoid resizing of fixed-size HashMap/LinkedHashMap variants
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Aug 25, 2020
1 parent 241afeb commit ff11467
Show file tree
Hide file tree
Showing 58 changed files with 195 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.lang.reflect.Modifier;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -53,6 +52,7 @@
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;

Expand Down Expand Up @@ -325,7 +325,7 @@ private Callback[] getCallbacks(Class<?> rootClass) throws Exception {
if (isStatic && isFrozen) {
Method[] methods = rootClass.getMethods();
Callback[] fixedCallbacks = new Callback[methods.length];
this.fixedInterceptorMap = new HashMap<>(methods.length);
this.fixedInterceptorMap = CollectionUtils.newHashMap(methods.length);

// TODO: small memory optimization here (can skip creation for methods with no advice)
for (int x = 0; x < methods.length; x++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
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;
Expand Down Expand Up @@ -792,7 +793,7 @@ public static <T> T instantiateClass(Constructor<T> ctor, Object... args)
}

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++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -85,7 +85,7 @@ protected Map<Object, Object> createInstance() {
result = BeanUtils.instantiateClass(this.targetMapClass);
}
else {
result = new LinkedHashMap<>(this.sourceMap.size());
result = CollectionUtils.newLinkedHashMap(this.sourceMap.size());
}
Class<?> keyType = null;
Class<?> valueType = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

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;
Expand All @@ -42,6 +41,7 @@
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;

Expand Down Expand Up @@ -447,7 +447,7 @@ private Set<?> resolveManagedSet(Object argName, Set<?> ms) {
* 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
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;
Expand Down Expand Up @@ -81,6 +80,7 @@
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;
Expand Down Expand Up @@ -476,7 +476,7 @@ public Stream<T> orderedStream() {
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)) {
Expand Down Expand Up @@ -665,7 +665,7 @@ public <T> Map<String, T> getBeansOfType(
@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);
Expand Down Expand Up @@ -715,7 +715,7 @@ public String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotation
@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)) {
Expand Down Expand Up @@ -1235,7 +1235,7 @@ private <T> NamedBeanHolder<T> resolveNamedBean(
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);
Expand Down Expand Up @@ -1532,7 +1532,7 @@ protected Map<String, Object> findAutowireCandidates(

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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public Object generate(Object target, Method method, Object... params) {
}
}

@SuppressWarnings("unchecked")
private static Object doGenerate(KeyGenerator keyGenerator, CacheKeyInvocationContext<?> context) {
List<Object> parameters = new ArrayList<>();
for (CacheInvocationParameter param : context.getKeyParameters()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 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.
Expand Down Expand Up @@ -66,6 +66,7 @@
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;

Expand Down Expand Up @@ -301,13 +302,13 @@ private void retrieveMBeanInfo(MBeanServerConnection server) throws MBeanInfoRet
MBeanInfo info = server.getMBeanInfo(this.objectName);

MBeanAttributeInfo[] attributeInfo = info.getAttributes();
this.allowedAttributes = new HashMap<>(attributeInfo.length);
this.allowedAttributes = CollectionUtils.newHashMap(attributeInfo.length);
for (MBeanAttributeInfo infoEle : attributeInfo) {
this.allowedAttributes.put(infoEle.getName(), infoEle);
}

MBeanOperationInfo[] operationInfo = info.getOperations();
this.allowedOperations = new HashMap<>(operationInfo.length);
this.allowedOperations = CollectionUtils.newHashMap(operationInfo.length);
for (MBeanOperationInfo infoEle : operationInfo) {
Class<?>[] paramTypes = JmxUtils.parameterInfoToTypes(infoEle.getSignature(), this.beanClassLoader);
this.allowedOperations.put(new MethodCacheKey(infoEle.getName(), paramTypes), infoEle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
Expand Down Expand Up @@ -126,7 +126,7 @@ public void afterPropertiesSet() {
* @return the resolved interface mappings (with Class objects as values)
*/
private Map<String, Class<?>[]> resolveInterfaceMappings(Properties mappings) {
Map<String, Class<?>[]> resolvedMappings = new HashMap<>(mappings.size());
Map<String, Class<?>[]> resolvedMappings = CollectionUtils.newHashMap(mappings.size());
for (Enumeration<?> en = mappings.propertyNames(); en.hasMoreElements();) {
String beanKey = (String) en.nextElement();
String[] classNames = StringUtils.commaDelimitedListToStringArray(mappings.getProperty(beanKey));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@
import org.openjdk.jmh.infra.Blackhole;

import org.springframework.core.convert.TypeDescriptor;
import org.springframework.util.CollectionUtils;

/**
* Benchmarks for {@link GenericConversionService}.
*
* @author Brian Clozel
*/
@BenchmarkMode(Mode.Throughput)
Expand Down Expand Up @@ -78,26 +80,28 @@ public void convertMapOfStringToListOfIntegerWithConversionService(MapBenchmarkS

@Benchmark
public void convertMapOfStringToListOfIntegerBaseline(MapBenchmarkState state, Blackhole bh) {
Map<String, Integer> target = new HashMap<>(state.source.size());
Map<String, Integer> target = CollectionUtils.newHashMap(state.source.size());
state.source.forEach((k, v) -> target.put(k, Integer.valueOf(v)));
bh.consume(target);
}


@State(Scope.Benchmark)
public static class MapBenchmarkState extends BenchmarkState {

Map<String, String> source;

@Setup(Level.Trial)
public void setup() throws Exception {
this.source = new HashMap<>(this.collectionSize);
this.source = CollectionUtils.newHashMap(this.collectionSize);
Map<String, Integer> target = new HashMap<>();
this.targetTypeDesc = TypeDescriptor.forObject(target);
this.source = IntStream.rangeClosed(1, collectionSize).mapToObj(String::valueOf)
.collect(Collectors.toMap(String::valueOf, String::valueOf));
}
}


@State(Scope.Benchmark)
public static class BenchmarkState {

Expand All @@ -107,6 +111,6 @@ public static class BenchmarkState {
int collectionSize;

TypeDescriptor targetTypeDesc;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.lang.reflect.Modifier;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
Expand All @@ -35,6 +34,7 @@
import org.springframework.core.annotation.MergedAnnotation.Adapt;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
Expand Down Expand Up @@ -905,7 +905,7 @@ private static Map<String, DefaultValueHolder> computeDefaultValues(
if (!methods.hasDefaultValueMethod()) {
return Collections.emptyMap();
}
Map<String, DefaultValueHolder> result = new LinkedHashMap<>(methods.size());
Map<String, DefaultValueHolder> result = CollectionUtils.newLinkedHashMap(methods.size());
if (!methods.hasNestedAnnotation()) {
// Use simpler method if there are no nested annotations
for (int i = 0; i < methods.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 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.
Expand All @@ -17,12 +17,12 @@
package org.springframework.core.codec;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;

import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;

/**
* Constants and convenience methods for working with hints.
Expand Down Expand Up @@ -120,7 +120,7 @@ else if (hints1.isEmpty()) {
return hints2;
}
else {
Map<String, Object> result = new HashMap<>(hints1.size() + hints2.size());
Map<String, Object> result = CollectionUtils.newHashMap(hints1.size() + hints2.size());
result.putAll(hints1);
result.putAll(hints2);
return result;
Expand All @@ -141,7 +141,7 @@ public static Map<String, Object> merge(Map<String, Object> hints, String hintNa
return Collections.singletonMap(hintName, hintValue);
}
else {
Map<String, Object> result = new HashMap<>(hints.size() + 1);
Map<String, Object> result = CollectionUtils.newHashMap(hints.size() + 1);
result.putAll(hints);
result.put(hintName, hintValue);
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisito
* to ensure that the hierarchical ordering of the entries is preserved.
* @see AnnotationReadingVisitorUtils#getMergedAnnotationAttributes
*/
protected final LinkedMultiValueMap<String, AnnotationAttributes> attributesMap = new LinkedMultiValueMap<>(4);
protected final LinkedMultiValueMap<String, AnnotationAttributes> attributesMap = new LinkedMultiValueMap<>(3);

protected final Set<MethodMetadata> methodMetadataSet = new LinkedHashSet<>(4);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class MethodMetadataReadingVisitor extends MethodVisitor implements Metho

protected final Map<String, Set<String>> metaAnnotationMap = new LinkedHashMap<>(4);

protected final LinkedMultiValueMap<String, AnnotationAttributes> attributesMap = new LinkedMultiValueMap<>(4);
protected final LinkedMultiValueMap<String, AnnotationAttributes> attributesMap = new LinkedMultiValueMap<>(3);


public MethodMetadataReadingVisitor(String methodName, int access, String declaringClassName,
Expand Down
Loading

0 comments on commit ff11467

Please sign in to comment.