minor gc optimization: better DefaultSpdyHeaders.iterator()
This commit is contained in:
parent
951a65e57e
commit
b72761e99c
@ -15,11 +15,14 @@
|
||||
*/
|
||||
package io.netty.handler.codec.spdy;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
|
||||
public class DefaultSpdyHeaders extends SpdyHeaders {
|
||||
@ -237,16 +240,8 @@ public class DefaultSpdyHeaders extends SpdyHeaders {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Map.Entry<String, String>> entries() {
|
||||
List<Map.Entry<String, String>> all =
|
||||
new LinkedList<Map.Entry<String, String>>();
|
||||
|
||||
HeaderEntry e = head.after;
|
||||
while (e != head) {
|
||||
all.add(e);
|
||||
e = e.after;
|
||||
}
|
||||
return all;
|
||||
public Iterator<Map.Entry<String, String>> iterator() {
|
||||
return new HeaderIterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -281,7 +276,7 @@ public class DefaultSpdyHeaders extends SpdyHeaders {
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return entries().isEmpty();
|
||||
return head == head.after;
|
||||
}
|
||||
|
||||
private static String toString(Object value) {
|
||||
@ -291,6 +286,32 @@ public class DefaultSpdyHeaders extends SpdyHeaders {
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
private final class HeaderIterator implements Iterator<Map.Entry<String, String>> {
|
||||
|
||||
private HeaderEntry current = head;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return current.after != head;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Entry<String, String> next() {
|
||||
current = current.after;
|
||||
|
||||
if (current == head) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
private static final class HeaderEntry implements Map.Entry<String, String> {
|
||||
final int hash;
|
||||
final String key;
|
||||
|
@ -98,7 +98,7 @@ public class DefaultSpdyHeadersFrame extends DefaultSpdyStreamFrame
|
||||
}
|
||||
|
||||
protected void appendHeaders(StringBuilder buf) {
|
||||
for (Map.Entry<String, String> e: headers().entries()) {
|
||||
for (Map.Entry<String, String> e: headers()) {
|
||||
buf.append(" ");
|
||||
buf.append(e.getKey());
|
||||
buf.append(": ");
|
||||
|
@ -38,11 +38,6 @@ public abstract class SpdyHeaders implements Iterable<Map.Entry<String, String>>
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Map.Entry<String, String>> entries() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(String name) {
|
||||
return false;
|
||||
@ -90,7 +85,7 @@ public abstract class SpdyHeaders implements Iterable<Map.Entry<String, String>>
|
||||
|
||||
@Override
|
||||
public Iterator<Map.Entry<String, String>> iterator() {
|
||||
return entries().iterator();
|
||||
return Collections.<Map.Entry<String, String>>emptyList().iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -420,10 +415,6 @@ public abstract class SpdyHeaders implements Iterable<Map.Entry<String, String>>
|
||||
frame.headers().set(HttpNames.VERSION, httpVersion.text());
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public Iterator<Map.Entry<String, String>> iterator() {
|
||||
return entries().iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the header value with the specified header name. If there is
|
||||
@ -442,14 +433,6 @@ public abstract class SpdyHeaders implements Iterable<Map.Entry<String, String>>
|
||||
*/
|
||||
public abstract List<String> getAll(String name);
|
||||
|
||||
/**
|
||||
* Returns all header names and values that this frame contains.
|
||||
*
|
||||
* @return the {@link List} of the header name-value pairs. An empty list
|
||||
* if there is no header in this message.
|
||||
*/
|
||||
public abstract List<Map.Entry<String, String>> entries();
|
||||
|
||||
/**
|
||||
* Returns {@code true} if and only if there is a header with the specified
|
||||
* header name.
|
||||
|
@ -241,7 +241,7 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder<SpdyFrame> {
|
||||
|
||||
// Ignore trailers in a truncated HEADERS frame.
|
||||
if (!spdyHeadersFrame.isTruncated()) {
|
||||
for (Map.Entry<String, String> e: spdyHeadersFrame.headers().entries()) {
|
||||
for (Map.Entry<String, String> e: spdyHeadersFrame.headers()) {
|
||||
fullHttpMessage.headers().add(e.getKey(), e.getValue());
|
||||
}
|
||||
}
|
||||
@ -310,7 +310,7 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder<SpdyFrame> {
|
||||
HttpHeaders.setHost(req, host);
|
||||
}
|
||||
|
||||
for (Map.Entry<String, String> e: requestFrame.headers().entries()) {
|
||||
for (Map.Entry<String, String> e: requestFrame.headers()) {
|
||||
req.headers().add(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
@ -332,7 +332,7 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder<SpdyFrame> {
|
||||
SpdyHeaders.removeVersion(spdyVersion, responseFrame);
|
||||
|
||||
FullHttpResponse res = new DefaultFullHttpResponse(version, status);
|
||||
for (Map.Entry<String, String> e: responseFrame.headers().entries()) {
|
||||
for (Map.Entry<String, String> e: responseFrame.headers()) {
|
||||
res.headers().add(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
|
@ -89,7 +89,7 @@ public class SpdySessionHandlerTest {
|
||||
assertTrue(receivedValues.isEmpty());
|
||||
spdyHeadersFrame.headers().remove(name);
|
||||
}
|
||||
assertTrue(spdyHeadersFrame.headers().entries().isEmpty());
|
||||
assertTrue(spdyHeadersFrame.headers().isEmpty());
|
||||
}
|
||||
|
||||
private static void testSpdySessionHandler(SpdyVersion version, boolean server) {
|
||||
|
Loading…
Reference in New Issue
Block a user