Initialize Map with correct size in RequestPredicates

Prior to this commit, the `RequestPredicates` would add new attributes
to the existing request attributes by creating a new `LinkedHashMap`
with the total number of elements as its new initial capacity.

This would not achieve optimal performance as initial resize or rehash
operations could be expected. Consistently using
`CollectionUtils#newLinkedHashMap` avoids this problem.

Closes gh-32201
This commit is contained in:
Patrick Strawderman 2024-02-05 20:08:24 -08:00 committed by Brian Clozel
parent 9698dbc232
commit d5cb1d9adb
2 changed files with 6 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@ -49,6 +49,7 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.WebDataBinder;
@ -325,7 +326,7 @@ public abstract class RequestPredicates {
return left;
}
else {
Map<K, V> result = new LinkedHashMap<>(left.size() + right.size());
Map<K, V> result = CollectionUtils.newLinkedHashMap(left.size() + right.size());
result.putAll(left);
result.putAll(right);
return result;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
@ -51,6 +51,7 @@ import org.springframework.http.server.RequestPath;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.validation.BindException;
@ -324,7 +325,7 @@ public abstract class RequestPredicates {
return left;
}
else {
Map<K, V> result = new LinkedHashMap<>(left.size() + right.size());
Map<K, V> result = CollectionUtils.newLinkedHashMap(left.size() + right.size());
result.putAll(left);
result.putAll(right);
return result;