Browse Source

Optimize Entry hashCode in HttpHeaders (plus related polishing)

pull/36340/head
Juergen Hoeller 4 weeks ago
parent
commit
508b31da4f
  1. 27
      spring-web/src/main/java/org/springframework/http/HttpHeaders.java
  2. 11
      spring-web/src/main/java/org/springframework/http/support/JettyHeadersAdapter.java

27
spring-web/src/main/java/org/springframework/http/HttpHeaders.java

@ -2188,6 +2188,7 @@ public class HttpHeaders implements Serializable { @@ -2188,6 +2188,7 @@ public class HttpHeaders implements Serializable {
private static final Object VALUE = new Object();
private final MultiValueMap<String, String> headers;
private final Map<String, Object> deduplicatedNames;
public CaseInsensitiveHeaderNameSet(MultiValueMap<String, String> headers) {
@ -2229,13 +2230,15 @@ public class HttpHeaders implements Serializable { @@ -2229,13 +2230,15 @@ public class HttpHeaders implements Serializable {
}
}
private static class HeaderNamesIterator implements Iterator<String> {
private @Nullable String currentName;
private static class HeaderNamesIterator implements Iterator<String> {
private final MultiValueMap<String, String> headers;
private final Iterator<String> namesIterator;
private @Nullable String currentName;
public HeaderNamesIterator(MultiValueMap<String, String> headers, Map<String, Object> caseInsensitiveNames) {
this.headers = headers;
this.namesIterator = caseInsensitiveNames.keySet().iterator();
@ -2269,6 +2272,7 @@ public class HttpHeaders implements Serializable { @@ -2269,6 +2272,7 @@ public class HttpHeaders implements Serializable {
private static final class CaseInsensitiveEntrySet extends AbstractSet<Entry<String, List<String>>> {
private final MultiValueMap<String, String> headers;
private final CaseInsensitiveHeaderNameSet nameSet;
public CaseInsensitiveEntrySet(MultiValueMap<String, String> headers) {
@ -2286,6 +2290,7 @@ public class HttpHeaders implements Serializable { @@ -2286,6 +2290,7 @@ public class HttpHeaders implements Serializable {
return this.nameSet.size();
}
private final class CaseInsensitiveIterator implements Iterator<Entry<String, List<String>>> {
private final Iterator<String> namesIterator;
@ -2310,6 +2315,7 @@ public class HttpHeaders implements Serializable { @@ -2310,6 +2315,7 @@ public class HttpHeaders implements Serializable {
}
}
private final class CaseInsensitiveEntry implements Entry<String, List<String>> {
private final String key;
@ -2330,26 +2336,21 @@ public class HttpHeaders implements Serializable { @@ -2330,26 +2336,21 @@ public class HttpHeaders implements Serializable {
@Override
public List<String> setValue(List<String> value) {
List<String> previousValues = Objects.requireNonNull(
CaseInsensitiveEntrySet.this.headers.get(this.key));
List<String> previousValues = Objects.requireNonNull(CaseInsensitiveEntrySet.this.headers.get(this.key));
CaseInsensitiveEntrySet.this.headers.put(this.key, value);
return previousValues;
}
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Map.Entry<?,?> that)) {
return false;
}
return ObjectUtils.nullSafeEquals(getKey(), that.getKey()) && ObjectUtils.nullSafeEquals(getValue(), that.getValue());
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof Map.Entry<?, ?> that &&
ObjectUtils.nullSafeEquals(getKey(), that.getKey()) &&
ObjectUtils.nullSafeEquals(getValue(), that.getValue())));
}
@Override
public int hashCode() {
return ObjectUtils.nullSafeHash(getKey(), getValue());
return this.key.hashCode(); // avoid value lookup for hashCode
}
}
}

11
spring-web/src/main/java/org/springframework/http/support/JettyHeadersAdapter.java

@ -52,14 +52,14 @@ public final class JettyHeadersAdapter implements MultiValueMap<String, String> @@ -52,14 +52,14 @@ public final class JettyHeadersAdapter implements MultiValueMap<String, String>
/**
* Creates a new {@code JettyHeadersAdapter} based on the given
* Construct a new {@code JettyHeadersAdapter} based on the given
* {@code HttpFields} instance.
* @param headers the {@code HttpFields} to base this adapter on
*/
public JettyHeadersAdapter(HttpFields headers) {
Assert.notNull(headers, "Headers must not be null");
this.headers = headers;
this.mutable = headers instanceof HttpFields.Mutable m ? m : null;
this.mutable = (headers instanceof HttpFields.Mutable m ? m : null);
}
@ -146,12 +146,12 @@ public final class JettyHeadersAdapter implements MultiValueMap<String, String> @@ -146,12 +146,12 @@ public final class JettyHeadersAdapter implements MultiValueMap<String, String>
public @Nullable List<String> get(Object key) {
List<String> list = null;
if (key instanceof String name) {
for (HttpField f : this.headers) {
if (f.is(name)) {
for (HttpField field : this.headers) {
if (field.is(name)) {
if (list == null) {
list = new ArrayList<>();
}
list.add(f.getValue());
list.add(field.getValue());
}
}
}
@ -162,7 +162,6 @@ public final class JettyHeadersAdapter implements MultiValueMap<String, String> @@ -162,7 +162,6 @@ public final class JettyHeadersAdapter implements MultiValueMap<String, String>
public @Nullable List<String> put(String key, List<String> value) {
HttpFields.Mutable mutableHttpFields = mutableFields();
List<String> oldValues = get(key);
if (oldValues == null) {
switch (value.size()) {
case 0 -> {}

Loading…
Cancel
Save