[#1982] Limit the usage of ThreadLocal.get() for performance reasons

This commit is contained in:
Norman Maurer 2013-11-14 09:48:33 +01:00
parent 41ccbfd388
commit 6306b83e2d

View File

@ -123,6 +123,7 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
private int contentRead; private int contentRead;
private long contentLength = Long.MIN_VALUE; private long contentLength = Long.MIN_VALUE;
private State state = State.SKIP_CONTROL_CHARS; private State state = State.SKIP_CONTROL_CHARS;
private StringBuilder sb;
/** /**
* The internal state of {@link HttpObjectDecoder}. * The internal state of {@link HttpObjectDecoder}.
@ -201,7 +202,7 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
// FALL THROUGH // FALL THROUGH
} }
case READ_INITIAL: try { case READ_INITIAL: try {
StringBuilder sb = BUILDERS.get(); StringBuilder sb = builder();
HttpMessage msg = splitInitialLine(sb, buffer, maxInitialLineLength); HttpMessage msg = splitInitialLine(sb, buffer, maxInitialLineLength);
if (msg == null) { if (msg == null) {
@ -217,7 +218,7 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
return; return;
} }
case READ_HEADER: try { case READ_HEADER: try {
State nextState = readHeaders(buffer, BUILDERS.get()); State nextState = readHeaders(buffer, builder());
if (nextState == state) { if (nextState == state) {
// was not able to consume whole header // was not able to consume whole header
return; return;
@ -438,7 +439,7 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
return; return;
} }
case READ_CHUNK_FOOTER: try { case READ_CHUNK_FOOTER: try {
LastHttpContent trailer = readTrailingHeaders(buffer, BUILDERS.get()); LastHttpContent trailer = readTrailingHeaders(buffer, builder());
if (trailer == null) { if (trailer == null) {
// not enough data // not enough data
return; return;
@ -868,9 +869,8 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
return Integer.parseInt(hex, 16); return Integer.parseInt(hex, 16);
} }
private static StringBuilder readLine(ByteBuf buffer, int maxLineLength) { private StringBuilder readLine(ByteBuf buffer, int maxLineLength) {
StringBuilder sb = BUILDERS.get(); StringBuilder sb = builder();
sb.setLength(0);
int lineLength = 0; int lineLength = 0;
buffer.markReaderIndex(); buffer.markReaderIndex();
@ -1063,4 +1063,14 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
} }
return result; return result;
} }
private StringBuilder builder() {
if (sb == null) {
// Obtain the StringBuilder from the ThreadLocal and store it for later usage.
// This minimize the ThreadLocal.get() operations a lot and so eliminate some overhead
sb = BUILDERS.get();
}
sb.setLength(0);
return sb;
}
} }