-
Notifications
You must be signed in to change notification settings - Fork 38k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Several benchmarks underlined a few hotspots for CPU and GC pressure in the Spring Framework codebase: 1. `org.springframework.util.MimeType.<init>(String, String, Map)` 2. `org.springframework.util.LinkedCaseInsensitiveMap.convertKey(String)` Both are linked with HTTP request headers parsing and response headers writin during the exchange processing phase. 1) is linked to repeated calls to `HttpHeaders.getContentType` within a single request handling. The media type parsing operation is expensive and the result doesn't change between calls, since the request headers are immutable at that point. This commit improves this by caching the parsed `MediaType` for the `"Content-Type"` request header in the `ReadOnlyHttpHeaders` class. This change is available for both Spring MVC and Spring WebFlux. 2) is linked to insertions/lookups in the `LinkedCaseInsensitiveMap`, which is the data structure behind `HttpHeaders`. Those operations are creating a lot of garbage (including a lot of `String` created by `toLowerCase`). We could choose a more efficient data structure for storing HTTP headers data. As a first step, this commit is focusing on Spring WebFlux and introduces `MultiValueMap` implementations mapped by native HTTP headers for the following servers: Tomcat, Jetty, Netty and Undertow. Such implementations avoid unnecessary copying of the headers and leverages as much as possible optimized operations provided by the native implementations. This change has a few consequences: * `HttpHeaders` can now wrap a `MultiValueMap` directly * The default constructor of `HttpHeaders` is still backed by a `LinkedCaseInsensitiveMap` * The HTTP request headers for the websocket HTTP handshake now need to be cloned, because native headers are likely to be pooled/recycled by the server implementation, hence gone when the initial HTTP exchange is done Issue: SPR-17250
- Loading branch information
Showing
19 changed files
with
1,224 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
spring-web/src/main/java/org/springframework/http/ReadOnlyHttpHeaders.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* Copyright 2002-2018 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.http; | ||
|
||
import java.util.AbstractMap; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.lang.Nullable; | ||
import org.springframework.util.MultiValueMap; | ||
|
||
/** | ||
* {@code HttpHeaders} object that can only be read, not written to. | ||
* | ||
* @author Brian Clozel | ||
* @since 5.1 | ||
*/ | ||
class ReadOnlyHttpHeaders extends HttpHeaders { | ||
|
||
private static final long serialVersionUID = -8578554704772377436L; | ||
|
||
@Nullable | ||
private MediaType cachedContentType; | ||
|
||
ReadOnlyHttpHeaders(HttpHeaders headers) { | ||
super(headers.headers); | ||
} | ||
|
||
@Override | ||
public MediaType getContentType() { | ||
if (this.cachedContentType != null) { | ||
return this.cachedContentType; | ||
} | ||
else { | ||
MediaType contentType = super.getContentType(); | ||
this.cachedContentType = contentType; | ||
return contentType; | ||
} | ||
} | ||
|
||
@Override | ||
public List<String> get(Object key) { | ||
List<String> values = this.headers.get(key); | ||
if (values != null) { | ||
return Collections.unmodifiableList(values); | ||
} | ||
return values; | ||
} | ||
|
||
@Override | ||
public void add(String headerName, @Nullable String headerValue) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void addAll(String key, List<? extends String> values) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void addAll(MultiValueMap<String, String> values) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void set(String headerName, @Nullable String headerValue) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void setAll(Map<String, String> values) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Map<String, String> toSingleValueMap() { | ||
return Collections.unmodifiableMap(this.headers.toSingleValueMap()); | ||
} | ||
|
||
@Override | ||
public Set<String> keySet() { | ||
return Collections.unmodifiableSet(this.headers.keySet()); | ||
} | ||
|
||
@Override | ||
public List<String> put(String key, List<String> value) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public List<String> remove(Object key) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void putAll(Map<? extends String, ? extends List<String>> map) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Collection<List<String>> values() { | ||
return Collections.unmodifiableCollection(this.headers.values()); | ||
} | ||
|
||
@Override | ||
public Set<Entry<String, List<String>>> entrySet() { | ||
return Collections.unmodifiableSet(this.headers.entrySet().stream() | ||
.map(AbstractMap.SimpleImmutableEntry::new) | ||
.collect(Collectors.toSet())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.