Skip to content

Commit

Permalink
Initialize Map with correct size in RequestPredicates
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kilink authored and bclozel committed Feb 6, 2024
1 parent 9698dbc commit d5cb1d9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -49,6 +49,7 @@
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;
Expand Down Expand Up @@ -325,7 +326,7 @@ private static <K, V> Map<K, V> mergeMaps(Map<K, V> left, Map<K, V> right) {
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -51,6 +51,7 @@
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;
Expand Down Expand Up @@ -324,7 +325,7 @@ private static <K, V> Map<K, V> mergeMaps(Map<K, V> left, Map<K, V> right) {
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;
Expand Down

0 comments on commit d5cb1d9

Please sign in to comment.