minor gc optimization: better DefaultSpdyHeaders.iterator()

This commit is contained in:
Bill Gallagher 2013-10-11 11:59:30 -04:00 committed by Norman Maurer
parent 951a65e57e
commit b72761e99c
5 changed files with 38 additions and 34 deletions

View File

@ -15,11 +15,14 @@
*/ */
package io.netty.handler.codec.spdy; package io.netty.handler.codec.spdy;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.Map.Entry;
public class DefaultSpdyHeaders extends SpdyHeaders { public class DefaultSpdyHeaders extends SpdyHeaders {
@ -237,16 +240,8 @@ public class DefaultSpdyHeaders extends SpdyHeaders {
} }
@Override @Override
public List<Map.Entry<String, String>> entries() { public Iterator<Map.Entry<String, String>> iterator() {
List<Map.Entry<String, String>> all = return new HeaderIterator();
new LinkedList<Map.Entry<String, String>>();
HeaderEntry e = head.after;
while (e != head) {
all.add(e);
e = e.after;
}
return all;
} }
@Override @Override
@ -281,7 +276,7 @@ public class DefaultSpdyHeaders extends SpdyHeaders {
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
return entries().isEmpty(); return head == head.after;
} }
private static String toString(Object value) { private static String toString(Object value) {
@ -291,6 +286,32 @@ public class DefaultSpdyHeaders extends SpdyHeaders {
return value.toString(); 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> { private static final class HeaderEntry implements Map.Entry<String, String> {
final int hash; final int hash;
final String key; final String key;

View File

@ -98,7 +98,7 @@ public class DefaultSpdyHeadersFrame extends DefaultSpdyStreamFrame
} }
protected void appendHeaders(StringBuilder buf) { protected void appendHeaders(StringBuilder buf) {
for (Map.Entry<String, String> e: headers().entries()) { for (Map.Entry<String, String> e: headers()) {
buf.append(" "); buf.append(" ");
buf.append(e.getKey()); buf.append(e.getKey());
buf.append(": "); buf.append(": ");

View File

@ -38,11 +38,6 @@ public abstract class SpdyHeaders implements Iterable<Map.Entry<String, String>>
return Collections.emptyList(); return Collections.emptyList();
} }
@Override
public List<Map.Entry<String, String>> entries() {
return Collections.emptyList();
}
@Override @Override
public boolean contains(String name) { public boolean contains(String name) {
return false; return false;
@ -90,7 +85,7 @@ public abstract class SpdyHeaders implements Iterable<Map.Entry<String, String>>
@Override @Override
public Iterator<Map.Entry<String, String>> iterator() { public Iterator<Map.Entry<String, String>> iterator() {
return entries().iterator(); return Collections.<Map.Entry<String, String>>emptyList().iterator();
} }
@Override @Override
@ -420,10 +415,6 @@ public abstract class SpdyHeaders implements Iterable<Map.Entry<String, String>>
frame.headers().set(HttpNames.VERSION, httpVersion.text()); 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 * 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); 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 * Returns {@code true} if and only if there is a header with the specified
* header name. * header name.

View File

@ -241,7 +241,7 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder<SpdyFrame> {
// Ignore trailers in a truncated HEADERS frame. // Ignore trailers in a truncated HEADERS frame.
if (!spdyHeadersFrame.isTruncated()) { 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()); fullHttpMessage.headers().add(e.getKey(), e.getValue());
} }
} }
@ -310,7 +310,7 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder<SpdyFrame> {
HttpHeaders.setHost(req, host); 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()); req.headers().add(e.getKey(), e.getValue());
} }
@ -332,7 +332,7 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder<SpdyFrame> {
SpdyHeaders.removeVersion(spdyVersion, responseFrame); SpdyHeaders.removeVersion(spdyVersion, responseFrame);
FullHttpResponse res = new DefaultFullHttpResponse(version, status); 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()); res.headers().add(e.getKey(), e.getValue());
} }

View File

@ -89,7 +89,7 @@ public class SpdySessionHandlerTest {
assertTrue(receivedValues.isEmpty()); assertTrue(receivedValues.isEmpty());
spdyHeadersFrame.headers().remove(name); spdyHeadersFrame.headers().remove(name);
} }
assertTrue(spdyHeadersFrame.headers().entries().isEmpty()); assertTrue(spdyHeadersFrame.headers().isEmpty());
} }
private static void testSpdySessionHandler(SpdyVersion version, boolean server) { private static void testSpdySessionHandler(SpdyVersion version, boolean server) {