From d10174a3e9c54d1ed1e2b8d5725a1d0249375eff Mon Sep 17 00:00:00 2001 From: "wenqi.huang" Date: Fri, 22 Mar 2019 22:09:04 +0800 Subject: [PATCH 1/2] Optimize performance of produces condition checks --- .../http/ReadOnlyHttpHeaders.java | 15 +++++++++++ .../result/method/RequestMappingInfo.java | 25 +++++++++++++------ .../mvc/method/RequestMappingInfo.java | 25 +++++++++++++------ 3 files changed, 49 insertions(+), 16 deletions(-) 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 341938f1ba2..6fe96f08196 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 @@ class ReadOnlyHttpHeaders extends HttpHeaders { } } + @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); 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 795dc53185d..479fcbed15c 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,20 +216,29 @@ public final class RequestMappingInfo implements RequestCondition Date: Fri, 22 Mar 2019 14:39:21 -0400 Subject: [PATCH 2/2] Polish See gh-22598 --- .../springframework/http/ReadOnlyHttpHeaders.java | 6 ++++-- .../result/condition/ConsumesRequestCondition.java | 6 +++--- .../result/condition/ProducesRequestCondition.java | 9 ++------- .../reactive/result/method/RequestMappingInfo.java | 12 ++++++------ .../mvc/condition/ConsumesRequestCondition.java | 6 +++--- .../mvc/condition/ProducesRequestCondition.java | 9 ++------- .../web/servlet/mvc/method/RequestMappingInfo.java | 10 +++++----- 7 files changed, 25 insertions(+), 33 deletions(-) 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 6fe96f08196..c3dedaa241b 100644 --- a/spring-web/src/main/java/org/springframework/http/ReadOnlyHttpHeaders.java +++ b/spring-web/src/main/java/org/springframework/http/ReadOnlyHttpHeaders.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -41,12 +41,14 @@ class ReadOnlyHttpHeaders extends HttpHeaders { private MediaType cachedContentType; @Nullable - private MediaType cachedAccept; + private List cachedAccept; + ReadOnlyHttpHeaders(HttpHeaders headers) { super(headers.headers); } + @Override public MediaType getContentType() { if (this.cachedContentType != null) { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ConsumesRequestCondition.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ConsumesRequestCondition.java index f41bad10245..815b2b844bd 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ConsumesRequestCondition.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/condition/ConsumesRequestCondition.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -43,7 +43,7 @@ import org.springframework.web.server.UnsupportedMediaTypeStatusException; */ public final class ConsumesRequestCondition extends AbstractRequestCondition { - private static final ConsumesRequestCondition PRE_FLIGHT_MATCH = new ConsumesRequestCondition(); + private static final ConsumesRequestCondition EMPTY_CONDITION = new ConsumesRequestCondition(); private final List expressions; @@ -161,7 +161,7 @@ public final class ConsumesRequestCondition extends AbstractRequestCondition { - private static final ProducesRequestCondition PRE_FLIGHT_MATCH = new ProducesRequestCondition(); - private static final ProducesRequestCondition EMPTY_CONDITION = new ProducesRequestCondition(); @@ -187,11 +185,8 @@ public final class ProducesRequestCondition extends AbstractRequestCondition result = new LinkedHashSet<>(this.expressions); result.removeIf(expression -> !expression.match(exchange)); 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 479fcbed15c..ae9dfc3d5d2 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -227,10 +227,7 @@ public final class RequestMappingInfo implements RequestCondition { - private static final ConsumesRequestCondition PRE_FLIGHT_MATCH = new ConsumesRequestCondition(); + private static final ConsumesRequestCondition EMPTY_CONDITION = new ConsumesRequestCondition(); private final List expressions; @@ -163,7 +163,7 @@ public final class ConsumesRequestCondition extends AbstractRequestCondition { - private static final ProducesRequestCondition PRE_FLIGHT_MATCH = new ProducesRequestCondition(); - private static final ProducesRequestCondition EMPTY_CONDITION = new ProducesRequestCondition(); private static final List MEDIA_TYPE_ALL_LIST = @@ -187,11 +185,8 @@ public final class ProducesRequestCondition extends AbstractRequestCondition acceptedMediaTypes; 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 537af70221c..70eb0e57b7c 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 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. @@ -228,10 +228,6 @@ public final class RequestMappingInfo implements RequestCondition