Hi Tee,<br><br>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:)<br>
<br>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.<br>
<br><br><br>HttpMessageDecoder.java<br><br> private String readLine(ChannelBuffer buffer, int maxLineLength) throws TooLongFrameException {<br> int lineLength = 0;<br> <br> <br> // Jiming's code<br>
int fromIndex = buffer.readerIndex();<br> int readableBytes = buffer.readableBytes();<br> int indexOf = buffer.indexOf(fromIndex, fromIndex + readableBytes, HttpCodecUtil.LF);<br> lineLength = indexOf - fromIndex + 1;<br>
if(lineLength > maxLineLength) {<br> throw new TooLongFrameException(<br> "An HTTP line is larger than " + maxLineLength +<br> " bytes.");<br> }<br> byte[] arr = new byte[lineLength];<br>
buffer.readBytes(arr);<br> int trimLen = (arr[arr.length - 2] == HttpCodecUtil.CR)?arr.length - 2:arr.length-1;<br> return new String(arr, 0, trimLen);<br> }<br><br> private String readHeader(ChannelBuffer buffer) throws TooLongFrameException {<br>
// Jiming's code<br> String result = readLine(buffer, maxHeaderSize);<br> this.headerSize += result.length() + 1;<br> if (headerSize >= maxHeaderSize) {<br> throw new TooLongFrameException("HTTP header is larger than "<br>
+ maxHeaderSize + " bytes.");<br> }<br> // System.out.println(result + ":" + this.headerSize);<br> return result;<br> }<br><br><br>Thanks,<br>
Jiming<br>