From 25c31cd137b097b7547a881ed23a1182c40fb5c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Br=C3=A9gier?= Date: Sat, 2 Jun 2012 21:25:55 +0300 Subject: [PATCH] Improve Performance by calling very much less sao.setReadPosition(x) --- .../multipart/HttpPostRequestDecoder.java | 74 +++++++++++-------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/jboss/netty/handler/codec/http/multipart/HttpPostRequestDecoder.java b/src/main/java/org/jboss/netty/handler/codec/http/multipart/HttpPostRequestDecoder.java index 8bc4743f17..cc68cf00d1 100644 --- a/src/main/java/org/jboss/netty/handler/codec/http/multipart/HttpPostRequestDecoder.java +++ b/src/main/java/org/jboss/netty/handler/codec/http/multipart/HttpPostRequestDecoder.java @@ -1395,6 +1395,7 @@ public class HttpPostRequestDecoder { boolean newLine = true; int index = 0; int lastPosition = undecodedChunk.readerIndex(); + int setReadPosition = -1; boolean found = false; while (sao.pos < sao.limit) { @@ -1419,23 +1420,23 @@ public class HttpPostRequestDecoder { if (nextByte == HttpConstants.LF) { newLine = true; index = 0; - sao.setReadPosition(0); - lastPosition = undecodedChunk.readerIndex() - 2; + setReadPosition = sao.pos; + lastPosition = sao.pos - 2; } } else { // save last valid position - sao.setReadPosition(0); - lastPosition = undecodedChunk.readerIndex(); + setReadPosition = sao.pos; + lastPosition = sao.pos; } } else if (nextByte == HttpConstants.LF) { newLine = true; index = 0; - sao.setReadPosition(0); - lastPosition = undecodedChunk.readerIndex() - 1; + setReadPosition = sao.pos; + lastPosition = sao.pos - 1; } else { // save last valid position - sao.setReadPosition(0); - lastPosition = undecodedChunk.readerIndex(); + setReadPosition = sao.pos; + lastPosition = sao.pos; } } } else { @@ -1446,26 +1447,30 @@ public class HttpPostRequestDecoder { if (nextByte == HttpConstants.LF) { newLine = true; index = 0; - sao.setReadPosition(0); - lastPosition = undecodedChunk.readerIndex() - 2; + setReadPosition = sao.pos; + lastPosition = sao.pos - 2; } } else { // save last valid position - sao.setReadPosition(0); - lastPosition = undecodedChunk.readerIndex(); + setReadPosition = sao.pos; + lastPosition = sao.pos; } } else if (nextByte == HttpConstants.LF) { newLine = true; index = 0; - sao.setReadPosition(0); - lastPosition = undecodedChunk.readerIndex() - 1; + setReadPosition = sao.pos; + lastPosition = sao.pos - 1; } else { // save last valid position - sao.setReadPosition(0); - lastPosition = undecodedChunk.readerIndex(); + setReadPosition = sao.pos; + lastPosition = sao.pos; } } } + if (setReadPosition > 0) { + sao.pos = setReadPosition; + sao.setReadPosition(0); + } ChannelBuffer buffer = undecodedChunk.slice(readerIndex, lastPosition - readerIndex); if (found) { // found so lastPosition is correct and final @@ -1604,6 +1609,7 @@ public class HttpPostRequestDecoder { boolean newLine = true; int index = 0; int lastPosition = undecodedChunk.readerIndex(); + int setReadPosition = -1; boolean found = false; while (sao.pos < sao.limit) { @@ -1628,21 +1634,21 @@ public class HttpPostRequestDecoder { if (nextByte == HttpConstants.LF) { newLine = true; index = 0; - sao.setReadPosition(0); - lastPosition = undecodedChunk.readerIndex() - 2; + lastPosition = sao.pos - 2; + setReadPosition = sao.pos; } } else { - sao.setReadPosition(0); - lastPosition = undecodedChunk.readerIndex(); + lastPosition = sao.pos; + setReadPosition = sao.pos; } } else if (nextByte == HttpConstants.LF) { newLine = true; index = 0; - sao.setReadPosition(0); - lastPosition = undecodedChunk.readerIndex() - 1; + lastPosition = sao.pos - 1; + setReadPosition = sao.pos; } else { - sao.setReadPosition(0); - lastPosition = undecodedChunk.readerIndex(); + lastPosition = sao.pos; + setReadPosition = sao.pos; } } } else { @@ -1653,24 +1659,28 @@ public class HttpPostRequestDecoder { if (nextByte == HttpConstants.LF) { newLine = true; index = 0; - sao.setReadPosition(0); - lastPosition = undecodedChunk.readerIndex() - 2; + lastPosition = sao.pos - 2; + setReadPosition = sao.pos; } } else { - sao.setReadPosition(0); - lastPosition = undecodedChunk.readerIndex(); + lastPosition = sao.pos; + setReadPosition = sao.pos; } } else if (nextByte == HttpConstants.LF) { newLine = true; index = 0; - sao.setReadPosition(0); - lastPosition = undecodedChunk.readerIndex() - 1; + lastPosition = sao.pos - 1; + setReadPosition = sao.pos; } else { - sao.setReadPosition(0); - lastPosition = undecodedChunk.readerIndex(); + lastPosition = sao.pos; + setReadPosition = sao.pos; } } } + if (setReadPosition > 0) { + sao.pos = setReadPosition; + sao.setReadPosition(0); + } if (found) { // found so lastPosition is correct // but position is just after the delimiter (either close delimiter or simple one)