Avoid resizing of fixed-size HashMap/LinkedHashMap variants

Closes gh-25349
This commit is contained in:
Juergen Hoeller
2020-08-25 19:26:18 +02:00
parent 241afeb1b7
commit ff11467a0c
58 changed files with 195 additions and 149 deletions

View File

@@ -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.
@@ -33,6 +33,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.lang.Nullable;
import org.springframework.util.AlternativeJdkIdGenerator;
import org.springframework.util.CollectionUtils;
import org.springframework.util.IdGenerator;
/**
@@ -164,7 +165,7 @@ public class MessageHeaders implements Map<String, Object>, Serializable {
* @param keysToIgnore the keys of the entries to ignore
*/
private MessageHeaders(MessageHeaders original, Set<String> keysToIgnore) {
this.headers = new HashMap<>(original.headers.size());
this.headers = CollectionUtils.newHashMap(original.headers.size());
original.headers.forEach((key, value) -> {
if (!keysToIgnore.contains(key)) {
this.headers.put(key, value);

View File

@@ -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.
@@ -109,7 +109,7 @@ public abstract class AbstractMethodMessageHandler<T>
private final Map<T, HandlerMethod> handlerMethods = new LinkedHashMap<>(64);
private final MultiValueMap<String, T> destinationLookup = new LinkedMultiValueMap<>(64);
private final MultiValueMap<String, T> destinationLookup = new LinkedMultiValueMap<>(48);
private final Map<Class<?>, AbstractExceptionHandlerMethodResolver> exceptionHandlerCache =
new ConcurrentHashMap<>(64);

View File

@@ -106,7 +106,7 @@ public abstract class AbstractMethodMessageHandler<T>
private final Map<T, HandlerMethod> handlerMethods = new LinkedHashMap<>(64);
private final MultiValueMap<String, T> destinationLookup = new LinkedMultiValueMap<>(64);
private final MultiValueMap<String, T> destinationLookup = new LinkedMultiValueMap<>(48);
/**

View File

@@ -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.
@@ -18,7 +18,6 @@ package org.springframework.messaging.rsocket.annotation.support;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
@@ -29,6 +28,7 @@ import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.AbstractMessageCondition;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
/**
* A condition to assist with mapping onto handler methods based on the RSocket
@@ -88,7 +88,7 @@ public class RSocketFrameTypeMessageCondition extends AbstractMessageCondition<R
private static final Map<String, RSocketFrameTypeMessageCondition> frameTypeConditionCache;
static {
frameTypeConditionCache = new HashMap<>(FrameType.values().length);
frameTypeConditionCache = CollectionUtils.newHashMap(FrameType.values().length);
for (FrameType type : FrameType.values()) {
frameTypeConditionCache.put(type.name(), new RSocketFrameTypeMessageCondition(type));
}

View File

@@ -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.
@@ -28,6 +28,7 @@ import java.util.Set;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
@@ -110,7 +111,7 @@ public class StompHeaders implements MultiValueMap<String, String>, Serializable
* Create a new instance to be populated with new header values.
*/
public StompHeaders() {
this(new LinkedMultiValueMap<>(4), false);
this(new LinkedMultiValueMap<>(3), false);
}
private StompHeaders(Map<String, List<String>> headers, boolean readOnly) {
@@ -482,7 +483,7 @@ public class StompHeaders implements MultiValueMap<String, String>, Serializable
@Override
public Map<String, String> toSingleValueMap() {
LinkedHashMap<String, String> singleValueMap = new LinkedHashMap<>(this.headers.size());
LinkedHashMap<String, String> singleValueMap = CollectionUtils.newLinkedHashMap(this.headers.size());
this.headers.forEach((key, value) -> singleValueMap.put(key, value.get(0)));
return singleValueMap;
}

View File

@@ -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.
@@ -33,6 +33,7 @@ import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
/**
@@ -209,7 +210,7 @@ public class MultiServerUserRegistry implements SimpUserRegistry, SmartApplicati
public UserRegistrySnapshot(String id, SimpUserRegistry registry) {
this.id = id;
Set<SimpUser> users = registry.getUsers();
this.users = new HashMap<>(users.size());
this.users = CollectionUtils.newHashMap(users.size());
for (SimpUser user : users) {
this.users.put(user.getName(), new TransferSimpUser(user));
}

View File

@@ -163,7 +163,7 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor {
return;
}
if (map == null) {
map = new LinkedMultiValueMap<>(4);
map = new LinkedMultiValueMap<>(3);
setHeader(NATIVE_HEADERS, map);
}
List<String> values = new LinkedList<>();
@@ -184,7 +184,7 @@ public class NativeMessageHeaderAccessor extends MessageHeaderAccessor {
}
Map<String, List<String>> nativeHeaders = getNativeHeaders();
if (nativeHeaders == null) {
nativeHeaders = new LinkedMultiValueMap<>(4);
nativeHeaders = new LinkedMultiValueMap<>(3);
setHeader(NATIVE_HEADERS, nativeHeaders);
}
List<String> values = nativeHeaders.computeIfAbsent(name, k -> new LinkedList<>());