From 1bd4b702becb9fe32c773c2b538ff1da4e0de075 Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Fri, 5 Feb 2016 14:33:57 -0800 Subject: [PATCH] HttpObjectDecoder configurable initial buffer size Motivation: The initial buffer size used to decode HTTP objects is currently fixed at 128. This may be too small for some use cases and create a high amount of overhead associated with resizing/copying. The user should be able to configure the initial size as they please. Modifications: - Make HttpObjectDecoder's AppendableCharSequence initial size configurable Result: Users can more finely tune initial buffer size for increased performance or to save memory. Fixes https://github.com/netty/netty/issues/4807 --- .../handler/codec/http/HttpClientCodec.java | 16 ++++++++++++++++ .../handler/codec/http/HttpObjectDecoder.java | 11 ++++++++--- .../handler/codec/http/HttpRequestDecoder.java | 6 ++++++ .../handler/codec/http/HttpResponseDecoder.java | 6 ++++++ .../handler/codec/http/HttpServerCodec.java | 10 ++++++++++ 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java index e7a7610a9d..84f7b5c8e6 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java @@ -94,6 +94,17 @@ public final class HttpClientCodec this.failOnMissingResponse = failOnMissingResponse; } + /** + * Creates a new instance with the specified decoder options. + */ + public HttpClientCodec( + int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean failOnMissingResponse, + boolean validateHeaders, int initialBufferSize) { + init(new Decoder(maxInitialLineLength, maxHeaderSize, maxChunkSize, validateHeaders, initialBufferSize), + new Encoder()); + this.failOnMissingResponse = failOnMissingResponse; + } + private final class Encoder extends HttpRequestEncoder { @Override @@ -120,6 +131,11 @@ public final class HttpClientCodec super(maxInitialLineLength, maxHeaderSize, maxChunkSize, validateHeaders); } + Decoder(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean validateHeaders, + int initialBufferSize) { + super(maxInitialLineLength, maxHeaderSize, maxChunkSize, validateHeaders, initialBufferSize); + } + @Override protected void decode( ChannelHandlerContext ctx, ByteBuf buffer, List out) throws Exception { diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java index 063f220edd..d2a662bbd7 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectDecoder.java @@ -162,7 +162,12 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder { protected HttpObjectDecoder( int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean chunkedSupported, boolean validateHeaders) { + this(maxInitialLineLength, maxHeaderSize, maxChunkSize, chunkedSupported, validateHeaders, 128); + } + protected HttpObjectDecoder( + int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, + boolean chunkedSupported, boolean validateHeaders, int initialBufferSize) { if (maxInitialLineLength <= 0) { throw new IllegalArgumentException( "maxInitialLineLength must be a positive integer: " + @@ -178,12 +183,12 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder { "maxChunkSize must be a positive integer: " + maxChunkSize); } + AppendableCharSequence seq = new AppendableCharSequence(initialBufferSize); + lineParser = new LineParser(seq, maxInitialLineLength); + headerParser = new HeaderParser(seq, maxHeaderSize); this.maxChunkSize = maxChunkSize; this.chunkedSupported = chunkedSupported; this.validateHeaders = validateHeaders; - AppendableCharSequence seq = new AppendableCharSequence(128); - lineParser = new LineParser(seq, maxInitialLineLength); - headerParser = new HeaderParser(seq, maxHeaderSize); } @Override diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpRequestDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpRequestDecoder.java index 91364a9cf3..0fd2d02914 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpRequestDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpRequestDecoder.java @@ -76,6 +76,12 @@ public class HttpRequestDecoder extends HttpObjectDecoder { super(maxInitialLineLength, maxHeaderSize, maxChunkSize, true, validateHeaders); } + public HttpRequestDecoder( + int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean validateHeaders, + int initialBufferSize) { + super(maxInitialLineLength, maxHeaderSize, maxChunkSize, true, validateHeaders, initialBufferSize); + } + @Override protected HttpMessage createMessage(String[] initialLine) throws Exception { return new DefaultHttpRequest( diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseDecoder.java index 485d0a26e3..17043ff3e7 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpResponseDecoder.java @@ -107,6 +107,12 @@ public class HttpResponseDecoder extends HttpObjectDecoder { super(maxInitialLineLength, maxHeaderSize, maxChunkSize, true, validateHeaders); } + public HttpResponseDecoder( + int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean validateHeaders, + int initialBufferSize) { + super(maxInitialLineLength, maxHeaderSize, maxChunkSize, true, validateHeaders, initialBufferSize); + } + @Override protected HttpMessage createMessage(String[] initialLine) { return new DefaultHttpResponse( diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerCodec.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerCodec.java index c10185a51b..98e95bfee7 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerCodec.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerCodec.java @@ -50,4 +50,14 @@ public final class HttpServerCodec super(new HttpRequestDecoder(maxInitialLineLength, maxHeaderSize, maxChunkSize, validateHeaders), new HttpResponseEncoder()); } + + /** + * Creates a new instance with the specified decoder options. + */ + public HttpServerCodec(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean validateHeaders, + int initialBufferSize) { + super( + new HttpRequestDecoder(maxInitialLineLength, maxHeaderSize, maxChunkSize, validateHeaders, initialBufferSize), + new HttpResponseEncoder()); + } }