Improve http performance?

Jiming Liu jimingliu at gmail.com
Tue Mar 30 01:46:49 EDT 2010


Hi Tee,

I've refactored some code to trying to improve Http performance. The
following code is around 50% percent faster than the original ones. But to
the http service, it only improved around 4%. Yes, Netty is very well coded,
it is not easy to find a way to improve it:)

After refactor, I noticed that Buffer.indexOf are a bottleneck now. And
after reading netty code deeper, I found maybe the buffers should have more
batch operations, and make them real batch operation instead of still
operate byte after byte.



HttpMessageDecoder.java

    private String readLine(ChannelBuffer buffer, int maxLineLength) throws
TooLongFrameException {
        int lineLength = 0;


        // Jiming's code
        int fromIndex = buffer.readerIndex();
        int readableBytes = buffer.readableBytes();
        int indexOf = buffer.indexOf(fromIndex, fromIndex + readableBytes,
HttpCodecUtil.LF);
        lineLength = indexOf - fromIndex + 1;
        if(lineLength > maxLineLength) {
            throw new TooLongFrameException(
            "An HTTP line is larger than " + maxLineLength +
            " bytes.");
        }
        byte[] arr = new byte[lineLength];
        buffer.readBytes(arr);
        int trimLen = (arr[arr.length - 2] == HttpCodecUtil.CR)?arr.length -
2:arr.length-1;
        return new String(arr, 0, trimLen);
    }

    private String readHeader(ChannelBuffer buffer) throws
TooLongFrameException {
           // Jiming's code
            String result = readLine(buffer, maxHeaderSize);
            this.headerSize += result.length() + 1;
            if (headerSize >= maxHeaderSize) {
                throw new TooLongFrameException("HTTP header is larger than
"
                        + maxHeaderSize + " bytes.");
            }
            // System.out.println(result + ":" + this.headerSize);
            return result;
    }


Thanks,
Jiming
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/netty-users/attachments/20100330/84e19065/attachment-0001.html 


More information about the netty-users mailing list