Skip to content

Commit

Permalink
Instead of complicated capacity estimation, just use thread local buf…
Browse files Browse the repository at this point in the history
…fers.

Fixes netty#1951
  • Loading branch information
trustin committed Nov 2, 2013
1 parent e987ff6 commit 81243c1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static String encode(Cookie cookie) {
throw new NullPointerException("cookie");
}

StringBuilder buf = new StringBuilder(estimateClientLength(cookie));
StringBuilder buf = buffer.get();
encode(buf, cookie);
return stripTrailingSeparator(buf);
}
Expand All @@ -52,16 +52,7 @@ public static String encode(Cookie... cookies) {
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;
Expand All @@ -77,16 +68,7 @@ public static String encode(Iterable<Cookie> cookies) {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,82 +18,25 @@

final class CookieEncoderUtil {

static String stripTrailingSeparator(StringBuilder buf) {
if (buf.length() > 0) {
buf.setLength(buf.length() - 2);
}
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; '
static final ThreadLocal<StringBuilder> buffer = new ThreadLocal<StringBuilder>() {
@Override
public StringBuilder get() {
StringBuilder buf = super.get();
buf.setLength(0);
return buf;
}

if (c.getPath() != null) {
estimate += estimateLength(CookieHeaderNames.PATH, c.getPath());
@Override
protected StringBuilder initialValue() {
return new StringBuilder(512);
}
};

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; '
}
static String stripTrailingSeparator(StringBuilder buf) {
if (buf.length() > 0) {
buf.setLength(buf.length() - 2);
}

return estimate;
}

private static int estimateLength(String name, String value) {
return name.length() + value.length() + 6;
return buf.toString();
}

static void add(StringBuilder sb, String name, String val) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static String encode(Cookie cookie) {
throw new NullPointerException("cookie");
}

StringBuilder buf = new StringBuilder(estimateServerLength(cookie));
StringBuilder buf = buffer.get();

add(buf, cookie.getName(), cookie.getValue());

Expand Down

0 comments on commit 81243c1

Please sign in to comment.