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

@@ -22,6 +22,7 @@ import java.util.LinkedHashSet;
import java.util.Set;
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.reactive.CorsUtils;
@@ -65,7 +66,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

@@ -22,6 +22,7 @@ import java.util.LinkedHashSet;
import java.util.Set;
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.server.ServerWebExchange;
@@ -51,7 +52,7 @@ public final class ParamsRequestCondition extends AbstractRequestCondition<Param
if (ObjectUtils.isEmpty(params)) {
return Collections.emptySet();
}
Set<ParamExpression> result = new LinkedHashSet<>(params.length);
Set<ParamExpression> result = CollectionUtils.newLinkedHashSet(params.length);
for (String param : params) {
result.add(new ParamExpression(param));
}