From 285f51d5ba005e3aedfb1590ca9adba943215252 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Sat, 2 Nov 2013 19:22:08 +0900 Subject: [PATCH] Instead of complicated capacity estimation, just use thread local buffers. Fixes #1951 --- .../codec/http/ClientCookieEncoder.java | 24 +----- .../handler/codec/http/CookieEncoderUtil.java | 85 +++---------------- .../codec/http/ServerCookieEncoder.java | 2 +- 3 files changed, 18 insertions(+), 93 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/ClientCookieEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/ClientCookieEncoder.java index 1f840e00af..03dc1ad48d 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/ClientCookieEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/ClientCookieEncoder.java @@ -42,7 +42,7 @@ public final class ClientCookieEncoder { throw new NullPointerException("cookie"); } - StringBuilder buf = new StringBuilder(estimateClientLength(cookie)); + StringBuilder buf = buffer.get(); encode(buf, cookie); return stripTrailingSeparator(buf); } @@ -52,16 +52,7 @@ public final class ClientCookieEncoder { throw new NullPointerException("cookies"); } - int initialCapacity = 0; - for (Cookie c: cookies) { - if (c == null) { - break; - } - - initialCapacity += estimateClientLength(c); - } - - StringBuilder buf = new StringBuilder(initialCapacity); + StringBuilder buf = buffer.get(); for (Cookie c: cookies) { if (c == null) { break; @@ -77,16 +68,7 @@ public final class ClientCookieEncoder { throw new NullPointerException("cookies"); } - int initialCapacity = 0; - for (Cookie c: cookies) { - if (c == null) { - break; - } - - initialCapacity += estimateClientLength(c); - } - - StringBuilder buf = new StringBuilder(initialCapacity); + StringBuilder buf = buffer.get(); for (Cookie c: cookies) { if (c == null) { break; diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/CookieEncoderUtil.java b/codec-http/src/main/java/io/netty/handler/codec/http/CookieEncoderUtil.java index c57e00caa7..1fba709e38 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/CookieEncoderUtil.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/CookieEncoderUtil.java @@ -18,6 +18,20 @@ package io.netty.handler.codec.http; final class CookieEncoderUtil { + static final ThreadLocal buffer = new ThreadLocal() { + @Override + public StringBuilder get() { + StringBuilder buf = super.get(); + buf.setLength(0); + return buf; + } + + @Override + protected StringBuilder initialValue() { + return new StringBuilder(512); + } + }; + static String stripTrailingSeparator(StringBuilder buf) { if (buf.length() > 0) { buf.setLength(buf.length() - 2); @@ -25,77 +39,6 @@ final class CookieEncoderUtil { return buf.toString(); } - static int estimateClientLength(Cookie c) { - int estimate = estimateLength(c.getName(), c.getValue()); - - if (c.getPath() != null) { - estimate += estimateLength(CookieHeaderNames.PATH, c.getPath()); - } - - if (c.getDomain() != null) { - estimate += estimateLength(CookieHeaderNames.DOMAIN, c.getDomain()); - } - - if (c.getVersion() > 0) { - estimate += 12; // '$Version=N; ' - int numPorts = c.getPorts().size(); - if (numPorts > 0) { - estimate += 10; // '$Port=""; ' - estimate += numPorts * 4; - } - } - - return estimate; - } - - static int estimateServerLength(Cookie c) { - int estimate = estimateLength(c.getName(), c.getValue()); - - if (c.getMaxAge() != Long.MIN_VALUE) { - estimate += 41; // 'Expires=Sun, 06 Nov 1994 08:49:37 +0900; ' - } - - if (c.getPath() != null) { - estimate += estimateLength(CookieHeaderNames.PATH, c.getPath()); - } - - if (c.getDomain() != null) { - estimate += estimateLength(CookieHeaderNames.DOMAIN, c.getDomain()); - } - if (c.isSecure()) { - estimate += 8; // 'Secure; ' - } - if (c.isHttpOnly()) { - estimate += 10; // 'HTTPOnly; ' - } - if (c.getVersion() > 0) { - estimate += 11; // 'Version=N; ' - if (c.getComment() != null) { - estimate += estimateLength(CookieHeaderNames.COMMENT, c.getComment()); - } - - if (c.getCommentUrl() != null) { - estimate += estimateLength(CookieHeaderNames.COMMENTURL, c.getCommentUrl()); - } - - int numPorts = c.getPorts().size(); - if (numPorts > 0) { - estimate += 9; // 'Port=""; ' - estimate += numPorts * 4; - } - - if (c.isDiscard()) { - estimate += 9; // 'Discard; ' - } - } - - return estimate; - } - - private static int estimateLength(String name, String value) { - return name.length() + value.length() + 6; - } - static void add(StringBuilder sb, String name, String val) { if (val == null) { addQuoted(sb, name, ""); diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/ServerCookieEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/ServerCookieEncoder.java index c7f724c428..21c906e7be 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/ServerCookieEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/ServerCookieEncoder.java @@ -47,7 +47,7 @@ public final class ServerCookieEncoder { throw new NullPointerException("cookie"); } - StringBuilder buf = new StringBuilder(estimateServerLength(cookie)); + StringBuilder buf = buffer.get(); add(buf, cookie.getName(), cookie.getValue());