Skip to content

Commit 8dc8d88

Browse files
committed
Pre-calculated RequestMappingInfo hashcode
See gh-25143
1 parent 1e25556 commit 8dc8d88

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ public final class RequestMappingInfo implements RequestCondition<RequestMapping
8787

8888
private final RequestConditionHolder customConditionHolder;
8989

90+
private final int hashCode;
91+
9092

9193
public RequestMappingInfo(@Nullable String name, @Nullable PatternsRequestCondition patterns,
9294
@Nullable RequestMethodsRequestCondition methods, @Nullable ParamsRequestCondition params,
@@ -101,6 +103,10 @@ public RequestMappingInfo(@Nullable String name, @Nullable PatternsRequestCondit
101103
this.consumesCondition = (consumes != null ? consumes : EMPTY_CONSUMES);
102104
this.producesCondition = (produces != null ? produces : EMPTY_PRODUCES);
103105
this.customConditionHolder = (custom != null ? new RequestConditionHolder(custom) : EMPTY_CUSTOM);
106+
107+
this.hashCode = calculateHashCode(
108+
this.patternsCondition, this.methodsCondition, this.paramsCondition, this.headersCondition,
109+
this.consumesCondition, this.producesCondition, this.customConditionHolder);
104110
}
105111

106112
/**
@@ -323,10 +329,17 @@ public boolean equals(@Nullable Object other) {
323329

324330
@Override
325331
public int hashCode() {
326-
return (this.patternsCondition.hashCode() * 31 + // primary differentiation
327-
this.methodsCondition.hashCode() + this.paramsCondition.hashCode() +
328-
this.headersCondition.hashCode() + this.consumesCondition.hashCode() +
329-
this.producesCondition.hashCode() + this.customConditionHolder.hashCode());
332+
return this.hashCode;
333+
}
334+
335+
private static int calculateHashCode(
336+
PatternsRequestCondition patterns, RequestMethodsRequestCondition methods,
337+
ParamsRequestCondition params, HeadersRequestCondition headers,
338+
ConsumesRequestCondition consumes, ProducesRequestCondition produces,
339+
RequestConditionHolder custom) {
340+
341+
return patterns.hashCode() * 31 + methods.hashCode() + params.hashCode() +
342+
headers.hashCode() + consumes.hashCode() + produces.hashCode() + custom.hashCode();
330343
}
331344

332345
@Override

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ public final class RequestMappingInfo implements RequestCondition<RequestMapping
7272
private static final RequestConditionHolder EMPTY_CUSTOM = new RequestConditionHolder(null);
7373

7474

75-
7675
@Nullable
7776
private final String name;
7877

@@ -90,6 +89,8 @@ public final class RequestMappingInfo implements RequestCondition<RequestMapping
9089

9190
private final RequestConditionHolder customConditionHolder;
9291

92+
private final int hashCode;
93+
9394

9495
public RequestMappingInfo(@Nullable String name, @Nullable PatternsRequestCondition patterns,
9596
@Nullable RequestMethodsRequestCondition methods, @Nullable ParamsRequestCondition params,
@@ -104,6 +105,10 @@ public RequestMappingInfo(@Nullable String name, @Nullable PatternsRequestCondit
104105
this.consumesCondition = (consumes != null ? consumes : EMPTY_CONSUMES);
105106
this.producesCondition = (produces != null ? produces : EMPTY_PRODUCES);
106107
this.customConditionHolder = (custom != null ? new RequestConditionHolder(custom) : EMPTY_CUSTOM);
108+
109+
this.hashCode = calculateHashCode(
110+
this.patternsCondition, this.methodsCondition, this.paramsCondition, this.headersCondition,
111+
this.consumesCondition, this.producesCondition, this.customConditionHolder);
107112
}
108113

109114
/**
@@ -125,7 +130,6 @@ public RequestMappingInfo(RequestMappingInfo info, @Nullable RequestCondition<?>
125130
info.consumesCondition, info.producesCondition, customRequestCondition);
126131
}
127132

128-
129133
/**
130134
* Return the name for this mapping, or {@code null}.
131135
*/
@@ -336,10 +340,17 @@ public boolean equals(@Nullable Object other) {
336340

337341
@Override
338342
public int hashCode() {
339-
return (this.patternsCondition.hashCode() * 31 + // primary differentiation
340-
this.methodsCondition.hashCode() + this.paramsCondition.hashCode() +
341-
this.headersCondition.hashCode() + this.consumesCondition.hashCode() +
342-
this.producesCondition.hashCode() + this.customConditionHolder.hashCode());
343+
return this.hashCode;
344+
}
345+
346+
private static int calculateHashCode(
347+
PatternsRequestCondition patterns, RequestMethodsRequestCondition methods,
348+
ParamsRequestCondition params, HeadersRequestCondition headers,
349+
ConsumesRequestCondition consumes, ProducesRequestCondition produces,
350+
RequestConditionHolder custom) {
351+
352+
return patterns.hashCode() * 31 + methods.hashCode() + params.hashCode() +
353+
headers.hashCode() + consumes.hashCode() + produces.hashCode() + custom.hashCode();
343354
}
344355

345356
@Override

0 commit comments

Comments
 (0)