From 8d9ddd071b5f740e057e3786250fcafb0c6bdc0f Mon Sep 17 00:00:00 2001 From: "wenqi.huang" Date: Fri, 15 Mar 2019 16:55:47 +0800 Subject: [PATCH 1/3] Improve performance of class `RequestMappingInfo` --- .../reactive/result/method/RequestMappingInfo.java | 11 ++++++++--- .../web/servlet/mvc/method/RequestMappingInfo.java | 11 ++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java index 795dc53185de..c096651f3d84 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java @@ -218,10 +218,8 @@ public RequestMappingInfo getMatchingCondition(ServerWebExchange exchange) { RequestMethodsRequestCondition methods = this.methodsCondition.getMatchingCondition(exchange); ParamsRequestCondition params = this.paramsCondition.getMatchingCondition(exchange); HeadersRequestCondition headers = this.headersCondition.getMatchingCondition(exchange); - ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(exchange); - ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(exchange); - if (methods == null || params == null || headers == null || consumes == null || produces == null) { + if (methods == null || params == null || headers == null) { return null; } @@ -230,6 +228,13 @@ public RequestMappingInfo getMatchingCondition(ServerWebExchange exchange) { return null; } + ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(exchange); + ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(exchange); + + if (consumes == null || produces == null) { + return null; + } + RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(exchange); if (custom == null) { return null; diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java index 29a1d6382553..6d795f8e1700 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java @@ -219,10 +219,8 @@ public RequestMappingInfo getMatchingCondition(HttpServletRequest request) { RequestMethodsRequestCondition methods = this.methodsCondition.getMatchingCondition(request); ParamsRequestCondition params = this.paramsCondition.getMatchingCondition(request); HeadersRequestCondition headers = this.headersCondition.getMatchingCondition(request); - ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(request); - ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(request); - if (methods == null || params == null || headers == null || consumes == null || produces == null) { + if (methods == null || params == null || headers == null) { return null; } @@ -231,6 +229,13 @@ public RequestMappingInfo getMatchingCondition(HttpServletRequest request) { return null; } + ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(request); + ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(request); + + if (consumes == null || produces == null) { + return null; + } + RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(request); if (custom == null) { return null; From c7564f7584b4aca5e67f012bee1fedd55b8dec86 Mon Sep 17 00:00:00 2001 From: "wenqi.huang" Date: Fri, 15 Mar 2019 19:37:08 +0800 Subject: [PATCH 2/3] Immediately return `null` after each individual condition check --- .../result/method/RequestMappingInfo.java | 18 +++++++++++------- .../servlet/mvc/method/RequestMappingInfo.java | 18 +++++++++++------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java index c096651f3d84..479fcbed15c5 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java @@ -216,25 +216,29 @@ else if (this.name != null) { @Nullable public RequestMappingInfo getMatchingCondition(ServerWebExchange exchange) { RequestMethodsRequestCondition methods = this.methodsCondition.getMatchingCondition(exchange); + if (methods == null) { + return null; + } ParamsRequestCondition params = this.paramsCondition.getMatchingCondition(exchange); + if (params == null) { + return null; + } HeadersRequestCondition headers = this.headersCondition.getMatchingCondition(exchange); - - if (methods == null || params == null || headers == null) { + if (headers == null) { return null; } - PatternsRequestCondition patterns = this.patternsCondition.getMatchingCondition(exchange); if (patterns == null) { return null; } - ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(exchange); + if (consumes == null) { + return null; + } ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(exchange); - - if (consumes == null || produces == null) { + if (produces == null) { return null; } - RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(exchange); if (custom == null) { return null; diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java index 6d795f8e1700..537af70221c8 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java @@ -217,25 +217,29 @@ else if (this.name != null) { @Nullable public RequestMappingInfo getMatchingCondition(HttpServletRequest request) { RequestMethodsRequestCondition methods = this.methodsCondition.getMatchingCondition(request); + if (methods == null) { + return null; + } ParamsRequestCondition params = this.paramsCondition.getMatchingCondition(request); + if (params == null) { + return null; + } HeadersRequestCondition headers = this.headersCondition.getMatchingCondition(request); - - if (methods == null || params == null || headers == null) { + if (headers == null) { return null; } - PatternsRequestCondition patterns = this.patternsCondition.getMatchingCondition(request); if (patterns == null) { return null; } - ConsumesRequestCondition consumes = this.consumesCondition.getMatchingCondition(request); + if (consumes == null) { + return null; + } ProducesRequestCondition produces = this.producesCondition.getMatchingCondition(request); - - if (consumes == null || produces == null) { + if (produces == null) { return null; } - RequestConditionHolder custom = this.customConditionHolder.getMatchingCondition(request); if (custom == null) { return null; From 0e5ce5114cd35e1e7710161c9af614e399bb894c Mon Sep 17 00:00:00 2001 From: "wenqi.huang" Date: Fri, 22 Mar 2019 22:09:04 +0800 Subject: [PATCH 3/3] Cache the result of getAccept method in ReadOnlyHttpHeaders --- .../springframework/http/ReadOnlyHttpHeaders.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spring-web/src/main/java/org/springframework/http/ReadOnlyHttpHeaders.java b/spring-web/src/main/java/org/springframework/http/ReadOnlyHttpHeaders.java index 341938f1ba2a..6fe96f081960 100644 --- a/spring-web/src/main/java/org/springframework/http/ReadOnlyHttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/ReadOnlyHttpHeaders.java @@ -40,6 +40,9 @@ class ReadOnlyHttpHeaders extends HttpHeaders { @Nullable private MediaType cachedContentType; + @Nullable + private MediaType cachedAccept; + ReadOnlyHttpHeaders(HttpHeaders headers) { super(headers.headers); } @@ -56,6 +59,18 @@ public MediaType getContentType() { } } + @Override + public List getAccept() { + if (this.cachedAccept != null) { + return this.cachedAccept; + } + else { + List accept = super.getAccept(); + this.cachedAccept = accept; + return accept; + } + } + @Override public List get(Object key) { List values = this.headers.get(key);