Avoid resizing of fixed-size HashSet/LinkedHashSet variants

Add helpers to CollectionUtils for building HashSets and LinkedHashSets
that can hold an expected number of elements without needing to
resize/rehash.

Closes gh-32291
This commit is contained in:
Patrick Strawderman
2024-02-18 14:05:31 -08:00
committed by Sam Brannen
parent 6383a0d7ca
commit e1a32d4ba9
47 changed files with 114 additions and 75 deletions

View File

@@ -24,6 +24,7 @@ import java.util.Set;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.cors.CorsUtils;
@@ -67,7 +68,7 @@ public final class HeadersRequestCondition extends AbstractRequestCondition<Head
if ("Accept".equalsIgnoreCase(expr.name) || "Content-Type".equalsIgnoreCase(expr.name)) {
continue;
}
result = (result != null ? result : new LinkedHashSet<>(headers.length));
result = (result != null ? result : CollectionUtils.newLinkedHashSet(headers.length));
result.add(expr);
}
}

View File

@@ -25,6 +25,7 @@ import java.util.Set;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.util.WebUtils;
@@ -55,7 +56,7 @@ public final class ParamsRequestCondition extends AbstractRequestCondition<Param
if (ObjectUtils.isEmpty(params)) {
return Collections.emptySet();
}
Set<ParamExpression> expressions = new LinkedHashSet<>(params.length);
Set<ParamExpression> expressions = CollectionUtils.newLinkedHashSet(params.length);
for (String param : params) {
expressions.add(new ParamExpression(param));
}

View File

@@ -30,6 +30,7 @@ import jakarta.servlet.http.HttpServletRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.PathMatcher;
import org.springframework.util.StringUtils;
@@ -157,7 +158,7 @@ public class PatternsRequestCondition extends AbstractRequestCondition<PatternsR
if (!hasPattern(patterns)) {
return EMPTY_PATH_PATTERN;
}
Set<String> result = new LinkedHashSet<>(patterns.length);
Set<String> result = CollectionUtils.newLinkedHashSet(patterns.length);
for (String pattern : patterns) {
pattern = PathPatternParser.defaultInstance.initFullPathPattern(pattern);
result.add(pattern);

View File

@@ -512,7 +512,7 @@ public abstract class RequestMappingInfoHandlerMapping extends AbstractHandlerMe
}
private static Set<HttpMethod> initAllowedHttpMethods(Set<String> declaredMethods) {
Set<HttpMethod> result = new LinkedHashSet<>(declaredMethods.size());
Set<HttpMethod> result = CollectionUtils.newLinkedHashSet(declaredMethods.size());
if (declaredMethods.isEmpty()) {
for (HttpMethod method : HttpMethod.values()) {
if (method != HttpMethod.TRACE) {

View File

@@ -32,6 +32,7 @@ import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.HttpRequestMethodNotSupportedException;
@@ -130,7 +131,7 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport {
*/
public WebContentGenerator(boolean restrictDefaultSupportedMethods) {
if (restrictDefaultSupportedMethods) {
this.supportedMethods = new LinkedHashSet<>(4);
this.supportedMethods = CollectionUtils.newLinkedHashSet(3);
this.supportedMethods.add(METHOD_GET);
this.supportedMethods.add(METHOD_HEAD);
this.supportedMethods.add(METHOD_POST);