Instead of complicated capacity estimation, just use thread local buffers.
Fixes #1951
This commit is contained in:
parent
40c9de1b74
commit
285f51d5ba
@ -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;
|
||||
|
@ -18,6 +18,20 @@ package io.netty.handler.codec.http;
|
||||
|
||||
final class CookieEncoderUtil {
|
||||
|
||||
static final ThreadLocal<StringBuilder> buffer = new ThreadLocal<StringBuilder>() {
|
||||
@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, "");
|
||||
|
@ -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());
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user