From e26bbfd4a714597cd09cdd75a0579c54b5f24798 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 9 May 2014 08:31:07 +0200 Subject: [PATCH] Use ByteBuf.readSlice(...).retain() to minimize memory copies. Motivation: At the moment we call ByteBuf.readBytes(...) in these handlers but with optimizations done as part of 25e0d9d we can just use readSlice(...).retain() and eliminate the memory copy. Modifications: Replace ByteBuf.readBytes(...) usage with readSlice(...).retain(). Result: Less memory copies. --- .../io/netty/handler/codec/DelimiterBasedFrameDecoder.java | 6 +++--- .../io/netty/handler/codec/FixedLengthFrameDecoder.java | 2 +- .../java/io/netty/handler/codec/LineBasedFrameDecoder.java | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/codec/src/main/java/io/netty/handler/codec/DelimiterBasedFrameDecoder.java b/codec/src/main/java/io/netty/handler/codec/DelimiterBasedFrameDecoder.java index e1cf93b45d..2dbd897ad6 100644 --- a/codec/src/main/java/io/netty/handler/codec/DelimiterBasedFrameDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/DelimiterBasedFrameDecoder.java @@ -268,13 +268,13 @@ public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder { } if (stripDelimiter) { - frame = buffer.readBytes(minFrameLength); + frame = buffer.readSlice(minFrameLength); buffer.skipBytes(minDelimLength); } else { - frame = buffer.readBytes(minFrameLength + minDelimLength); + frame = buffer.readSlice(minFrameLength + minDelimLength); } - return frame; + return frame.retain(); } else { if (!discardingTooLongFrame) { if (buffer.readableBytes() > maxFrameLength) { diff --git a/codec/src/main/java/io/netty/handler/codec/FixedLengthFrameDecoder.java b/codec/src/main/java/io/netty/handler/codec/FixedLengthFrameDecoder.java index b4435d97d8..bef42a6ab8 100644 --- a/codec/src/main/java/io/netty/handler/codec/FixedLengthFrameDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/FixedLengthFrameDecoder.java @@ -74,7 +74,7 @@ public class FixedLengthFrameDecoder extends ByteToMessageDecoder { if (in.readableBytes() < frameLength) { return null; } else { - return in.readBytes(frameLength); + return in.readSlice(frameLength).retain(); } } } diff --git a/codec/src/main/java/io/netty/handler/codec/LineBasedFrameDecoder.java b/codec/src/main/java/io/netty/handler/codec/LineBasedFrameDecoder.java index f16646de02..ab49530de5 100644 --- a/codec/src/main/java/io/netty/handler/codec/LineBasedFrameDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/LineBasedFrameDecoder.java @@ -100,13 +100,13 @@ public class LineBasedFrameDecoder extends ByteToMessageDecoder { } if (stripDelimiter) { - frame = buffer.readBytes(length); + frame = buffer.readSlice(length); buffer.skipBytes(delimLength); } else { - frame = buffer.readBytes(length + delimLength); + frame = buffer.readSlice(length + delimLength); } - return frame; + return frame.retain(); } else { final int length = buffer.readableBytes(); if (length > maxLength) {