From b72761e99c9ba8c61b6c077499e4d6bc0f6eae28 Mon Sep 17 00:00:00 2001 From: Bill Gallagher Date: Fri, 11 Oct 2013 11:59:30 -0400 Subject: [PATCH] minor gc optimization: better DefaultSpdyHeaders.iterator() --- .../codec/spdy/DefaultSpdyHeaders.java | 43 ++++++++++++++----- .../codec/spdy/DefaultSpdyHeadersFrame.java | 2 +- .../netty/handler/codec/spdy/SpdyHeaders.java | 19 +------- .../handler/codec/spdy/SpdyHttpDecoder.java | 6 +-- .../codec/spdy/SpdySessionHandlerTest.java | 2 +- 5 files changed, 38 insertions(+), 34 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/DefaultSpdyHeaders.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/DefaultSpdyHeaders.java index d83b84d5ed..531097d988 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/DefaultSpdyHeaders.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/DefaultSpdyHeaders.java @@ -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> entries() { - List> all = - new LinkedList>(); - - HeaderEntry e = head.after; - while (e != head) { - all.add(e); - e = e.after; - } - return all; + public Iterator> 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> { + + private HeaderEntry current = head; + + @Override + public boolean hasNext() { + return current.after != head; + } + + @Override + public Entry 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 { final int hash; final String key; diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/DefaultSpdyHeadersFrame.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/DefaultSpdyHeadersFrame.java index f7f41f04ed..b315590903 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/DefaultSpdyHeadersFrame.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/DefaultSpdyHeadersFrame.java @@ -98,7 +98,7 @@ public class DefaultSpdyHeadersFrame extends DefaultSpdyStreamFrame } protected void appendHeaders(StringBuilder buf) { - for (Map.Entry e: headers().entries()) { + for (Map.Entry e: headers()) { buf.append(" "); buf.append(e.getKey()); buf.append(": "); diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaders.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaders.java index 3e073370d2..447d7d07f5 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaders.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaders.java @@ -38,11 +38,6 @@ public abstract class SpdyHeaders implements Iterable> return Collections.emptyList(); } - @Override - public List> entries() { - return Collections.emptyList(); - } - @Override public boolean contains(String name) { return false; @@ -90,7 +85,7 @@ public abstract class SpdyHeaders implements Iterable> @Override public Iterator> iterator() { - return entries().iterator(); + return Collections.>emptyList().iterator(); } @Override @@ -420,10 +415,6 @@ public abstract class SpdyHeaders implements Iterable> frame.headers().set(HttpNames.VERSION, httpVersion.text()); } } - @Override - public Iterator> 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> */ public abstract List 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> entries(); - /** * Returns {@code true} if and only if there is a header with the specified * header name. diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpDecoder.java index 2ff061b1d5..6ad8cf2a95 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpDecoder.java @@ -241,7 +241,7 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder { // Ignore trailers in a truncated HEADERS frame. if (!spdyHeadersFrame.isTruncated()) { - for (Map.Entry e: spdyHeadersFrame.headers().entries()) { + for (Map.Entry e: spdyHeadersFrame.headers()) { fullHttpMessage.headers().add(e.getKey(), e.getValue()); } } @@ -310,7 +310,7 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder { HttpHeaders.setHost(req, host); } - for (Map.Entry e: requestFrame.headers().entries()) { + for (Map.Entry e: requestFrame.headers()) { req.headers().add(e.getKey(), e.getValue()); } @@ -332,7 +332,7 @@ public class SpdyHttpDecoder extends MessageToMessageDecoder { SpdyHeaders.removeVersion(spdyVersion, responseFrame); FullHttpResponse res = new DefaultFullHttpResponse(version, status); - for (Map.Entry e: responseFrame.headers().entries()) { + for (Map.Entry e: responseFrame.headers()) { res.headers().add(e.getKey(), e.getValue()); } diff --git a/codec-http/src/test/java/io/netty/handler/codec/spdy/SpdySessionHandlerTest.java b/codec-http/src/test/java/io/netty/handler/codec/spdy/SpdySessionHandlerTest.java index c074ee0a40..4039b1bfa9 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/spdy/SpdySessionHandlerTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/spdy/SpdySessionHandlerTest.java @@ -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) {