JdkZlibEncoder can use pooled heap buffers for deflater input#11891
JdkZlibEncoder can use pooled heap buffers for deflater input#11891normanmaurer merged 4 commits intonetty:4.1from
Conversation
Motivation: Previously, when the input ByteBuf was not heap allocated we copied the input into a new byte array allocated on the heap for every encode() call. This puts high-throughput clients that use the JdkZlibEncoder under memory pressure. Modifications: Now, when the input ByteBuf is not heap allocated we copy the input into a heap ByteBuf allocated from the PooledByteBufAllocator, releasing it after the encode() has completed. Result: The result is less heap allocation and GC activity. Fixes netty#11890 Signed-off-by: Daniel Ferstay <[email protected]>
| // skip all bytes as we will consume all of them | ||
| uncompressed.skipBytes(len); | ||
| } else { | ||
| heapBuf = PooledByteBufAllocator.DEFAULT.heapBuffer(len, len); |
There was a problem hiding this comment.
| heapBuf = PooledByteBufAllocator.DEFAULT.heapBuffer(len, len); | |
| heapBuf = ctx.alloc().heapBuffer(len, len); |
| // We did not consume everything but the buffer is not writable anymore. Increase the capacity to | ||
| // make more room. | ||
| out.ensureWritable(out.writerIndex()); | ||
| } |
There was a problem hiding this comment.
Please revert the formatting changes.
There was a problem hiding this comment.
@normanmaurer ,
Are you referring to the additional indentation? If so, it is due to the addition of the try / finally block to ensure that the allocated heapBuf is released on method return. Do do you want me to remove the try / finally?
There was a problem hiding this comment.
Check the checkstyle errors
There was a problem hiding this comment.
Got it; the checkstyle errors should now be addressed.
| writeHeader = false; | ||
| if (wrapper == ZlibWrapper.GZIP) { | ||
| out.writeBytes(gzipHeader); | ||
| } |
There was a problem hiding this comment.
please revert formatting changes
| } | ||
| if (wrapper == ZlibWrapper.GZIP) { | ||
| crc.update(inAry, offset, len); | ||
| } |
There was a problem hiding this comment.
please revert formatting changes
| uncompressed.readBytes(inAry); | ||
| offset = 0; | ||
| } | ||
| ByteBuf heapBuf = null; |
There was a problem hiding this comment.
make this final and assign in the if / else blocks
There was a problem hiding this comment.
|
Also please sign or ICLA: https://netty.io/s/icla |
Signed :) |
|
@dferstay can you please revert the formatting changes ? |
codec/src/main/java/io/netty/handler/codec/compression/JdkZlibEncoder.java
Outdated
Show resolved
Hide resolved
codec/src/main/java/io/netty/handler/codec/compression/JdkZlibEncoder.java
Outdated
Show resolved
Hide resolved
|
@dferstay thanks a lot ! Would be cool if you can add details to the adopters page: https://netty.io/wiki/adopters.html |
@normanmaurer, |
…11891) Motivation: Previously, when the input ByteBuf was not heap allocated we copied the input into a new byte array allocated on the heap for every encode() call. This puts high-throughput clients that use the JdkZlibEncoder under memory pressure. Modifications: Now, when the input ByteBuf is not heap allocated we copy the input into a heap ByteBuf allocated from the configured ByteBufAllocator (which might be pooled), releasing it after the encode() has completed. Result: The result is less heap allocation and GC activity when the PooledByteBufAllocator is used (which is the default) Fixes netty#11890 Signed-off-by: Daniel Ferstay <[email protected]> Co-authored-by: Daniel Ferstay <[email protected]> Co-authored-by: Norman Maurer <[email protected]>
…11891) Motivation: Previously, when the input ByteBuf was not heap allocated we copied the input into a new byte array allocated on the heap for every encode() call. This puts high-throughput clients that use the JdkZlibEncoder under memory pressure. Modifications: Now, when the input ByteBuf is not heap allocated we copy the input into a heap ByteBuf allocated from the configured ByteBufAllocator (which might be pooled), releasing it after the encode() has completed. Result: The result is less heap allocation and GC activity when the PooledByteBufAllocator is used (which is the default) Fixes netty#11890 Signed-off-by: Daniel Ferstay <[email protected]> Co-authored-by: Daniel Ferstay <[email protected]> Co-authored-by: Norman Maurer <[email protected]>
…11891) Motivation: Previously, when the input ByteBuf was not heap allocated we copied the input into a new byte array allocated on the heap for every encode() call. This puts high-throughput clients that use the JdkZlibEncoder under memory pressure. Modifications: Now, when the input ByteBuf is not heap allocated we copy the input into a heap ByteBuf allocated from the configured ByteBufAllocator (which might be pooled), releasing it after the encode() has completed. Result: The result is less heap allocation and GC activity when the PooledByteBufAllocator is used (which is the default) Fixes netty#11890 Signed-off-by: Daniel Ferstay <[email protected]> Co-authored-by: Daniel Ferstay <[email protected]> Co-authored-by: Norman Maurer <[email protected]>
…11891) Motivation: Previously, when the input ByteBuf was not heap allocated we copied the input into a new byte array allocated on the heap for every encode() call. This puts high-throughput clients that use the JdkZlibEncoder under memory pressure. Modifications: Now, when the input ByteBuf is not heap allocated we copy the input into a heap ByteBuf allocated from the configured ByteBufAllocator (which might be pooled), releasing it after the encode() has completed. Result: The result is less heap allocation and GC activity when the PooledByteBufAllocator is used (which is the default) Fixes netty#11890 Signed-off-by: Daniel Ferstay <[email protected]> Co-authored-by: Daniel Ferstay <[email protected]> Co-authored-by: Norman Maurer <[email protected]>



Motivation:
Previously, when the input ByteBuf was not heap allocated we copied the
input into a new byte array allocated on the heap for every encode() call.
This puts high-throughput clients that use the JdkZlibEncoder under memory
pressure.
Modifications:
Now, when the input ByteBuf is not heap allocated we copy the input into a
heap ByteBuf allocated from the configured ByteBufAllocator (which might be pooled), releasing it after
the encode() has completed.
Result:
The result is less heap allocation and GC activity when the PooledByteBufAllocator is used (which is the default)
Fixes #11890
Signed-off-by: Daniel Ferstay [email protected]