Skip to content

Commit 5c537db

Browse files
committed
Optimize single PathPattern match
Closes gh-36275
1 parent 849553d commit 5c537db

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/PatternsRequestCondition.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,16 @@ else if (isEmptyPathMapping()) {
157157
@Override
158158
@Nullable
159159
public PatternsRequestCondition getMatchingCondition(ServerWebExchange exchange) {
160-
SortedSet<PathPattern> matches = getMatchingPatterns(exchange);
160+
PathContainer lookupPath = exchange.getRequest().getPath().pathWithinApplication();
161+
if (this.patterns.size() == 1) {
162+
return (this.patterns.first().matches(lookupPath) ? this : null);
163+
}
164+
SortedSet<PathPattern> matches = getMatchingPatterns(lookupPath);
161165
return (matches != null ? new PatternsRequestCondition(matches) : null);
162166
}
163167

164168
@Nullable
165-
private SortedSet<PathPattern> getMatchingPatterns(ServerWebExchange exchange) {
166-
PathContainer lookupPath = exchange.getRequest().getPath().pathWithinApplication();
169+
private SortedSet<PathPattern> getMatchingPatterns(PathContainer lookupPath) {
167170
TreeSet<PathPattern> result = null;
168171
for (PathPattern pattern : this.patterns) {
169172
if (pattern.matches(lookupPath)) {

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/PathPatternsRequestCondition.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ else if (isEmptyPathMapping()) {
190190
@Nullable
191191
public PathPatternsRequestCondition getMatchingCondition(HttpServletRequest request) {
192192
PathContainer path = ServletRequestPathUtils.getParsedRequestPath(request).pathWithinApplication();
193+
if (this.patterns.size() == 1) {
194+
return (this.patterns.first().matches(path) ? this : null);
195+
}
193196
SortedSet<PathPattern> matches = getMatchingPatterns(path);
194197
return (matches != null ? new PathPatternsRequestCondition(matches) : null);
195198
}

0 commit comments

Comments
 (0)