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 ff6a06a9be
commit b5f5175338
4 changed files with 40 additions and 6 deletions

View File

@ -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 {
@ -249,6 +252,11 @@ public class DefaultSpdyHeaders extends SpdyHeaders {
return all;
}
@Override
public Iterator<Map.Entry<String, String>> iterator() {
return new HeaderIterator();
}
@Override
public boolean contains(String name) {
return get(name) != null;
@ -281,7 +289,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 +299,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;

View File

@ -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(": ");

View File

@ -242,7 +242,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());
}
}
@ -311,7 +311,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());
}
@ -333,7 +333,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());
}

View File

@ -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(int version, boolean server) {