minor gc optimization: better DefaultHttpHeaders.iterator()

This commit is contained in:
Bill Gallagher 2013-10-11 11:12:08 -04:00 committed by Norman Maurer
parent 42cce12498
commit 951a65e57e
4 changed files with 33 additions and 6 deletions

View File

@ -25,6 +25,8 @@ import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.TreeSet;
@ -333,7 +335,7 @@ public class DefaultHttpHeaders extends HttpHeaders {
@Override
public Iterator<Map.Entry<String, String>> iterator() {
return entries().iterator();
return new HeaderIterator();
}
@Override
@ -378,6 +380,32 @@ public class DefaultHttpHeaders extends HttpHeaders {
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 final class HeaderEntry implements Map.Entry<String, String> {
final int hash;
final String key;

View File

@ -176,7 +176,7 @@ public class SpdyHttpEncoder extends MessageToMessageEncoder<HttpObject> {
spdyDataFrame.setLast(chunk instanceof LastHttpContent);
if (chunk instanceof LastHttpContent) {
LastHttpContent trailer = (LastHttpContent) chunk;
List<Map.Entry<String, String>> trailers = trailer.trailingHeaders().entries();
HttpHeaders trailers = trailer.trailingHeaders();
if (trailers.isEmpty()) {
out.add(spdyDataFrame);
} else {

View File

@ -71,9 +71,9 @@ public class HttpSnoopServerHandler extends SimpleChannelInboundHandler<Object>
buf.append("HOSTNAME: ").append(getHost(request, "unknown")).append("\r\n");
buf.append("REQUEST_URI: ").append(request.getUri()).append("\r\n\r\n");
List<Map.Entry<String, String>> headers = request.headers().entries();
HttpHeaders headers = request.headers();
if (!headers.isEmpty()) {
for (Map.Entry<String, String> h: request.headers().entries()) {
for (Map.Entry<String, String> h: headers) {
String key = h.getKey();
String value = h.getValue();
buf.append("HEADER: ").append(key).append(" = ").append(value).append("\r\n");

View File

@ -114,8 +114,7 @@ public class HttpUploadServerHandler extends SimpleChannelInboundHandler<HttpObj
responseContent.append("\r\n\r\n");
// new getMethod
List<Entry<String, String>> headers = request.headers().entries();
for (Entry<String, String> entry : headers) {
for (Entry<String, String> entry : request.headers()) {
responseContent.append("HEADER: " + entry.getKey() + '=' + entry.getValue() + "\r\n");
}
responseContent.append("\r\n\r\n");