Return singleton from getAll if header doesn't exist. (#9836)

Motivation:

It is more efficient to avoid allocating objects when we don't need to.

Modification:

Don't allocate a `LinkedList` for returning an empty list of header values when the header doesn't exist at all.

Result:

Less allocations
This commit is contained in:
Anuraag Agrawal 2020-03-31 15:35:02 +09:00 committed by GitHub
parent 82e5ca7e7f
commit 9f5970a91a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -155,17 +155,22 @@ public class DefaultHeaders<K, V, T extends Headers<K, V, T>> implements Headers
public List<V> getAll(K name) {
requireNonNull(name, "name");
LinkedList<V> values = new LinkedList<>();
int h = hashingStrategy.hashCode(name);
int i = index(h);
HeaderEntry<K, V> e = entries[i];
while (e != null) {
if (e == null) {
return Collections.emptyList();
}
LinkedList<V> values = new LinkedList<>();
do {
if (e.hash == h && hashingStrategy.equals(name, e.key)) {
values.addFirst(e.getValue());
}
e = e.next;
}
} while (e != null);
return values;
}