GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix Zstd compression for large data (#15861) (#15900) · netty/netty@4647500

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec/src
    • main/java/io/netty/handler/codec/compression
      • StandardCompressionOptions.java
      • ZstdConstants.java
      • ZstdEncoder.java
      • ZstdOptions.java
    • test/java/io/netty/handler/codec/compression
      • ZstdEncoderTest.java
      • ZstdIntegrationTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec/src/main/java/io/netty/handler/codec/compression/StandardCompressionOptions.java‎

Copy file name to clipboardExpand all lines: codec/src/main/java/io/netty/handler/codec/compression/StandardCompressionOptions.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static BrotliOptions brotli(int quality, int window, BrotliMode mode) {
7171
/**
7272
* Default implementation of {@link ZstdOptions} with{compressionLevel(int)} set to
7373
* {@link ZstdConstants#DEFAULT_COMPRESSION_LEVEL},{@link ZstdConstants#DEFAULT_BLOCK_SIZE},
74-
* {@link ZstdConstants#MAX_BLOCK_SIZE}
74+
* {@link ZstdConstants#DEFAULT_MAX_ENCODE_SIZE}
7575
*/
7676
public static ZstdOptions zstd() {
7777
return ZstdOptions.DEFAULT;
Collapse file

‎codec/src/main/java/io/netty/handler/codec/compression/ZstdConstants.java‎

Copy file name to clipboardExpand all lines: codec/src/main/java/io/netty/handler/codec/compression/ZstdConstants.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ final class ZstdConstants {
3535
static final int MAX_COMPRESSION_LEVEL = Zstd.maxCompressionLevel();
3636

3737
/**
38-
* Max block size
38+
* Max encode size
3939
*/
40-
static final int MAX_BLOCK_SIZE = 1 << (DEFAULT_COMPRESSION_LEVEL + 7) + 0x0F; // 32 M
40+
static final int DEFAULT_MAX_ENCODE_SIZE = Integer.MAX_VALUE;
4141
/**
4242
* Default block size
4343
*/
Collapse file

‎codec/src/main/java/io/netty/handler/codec/compression/ZstdEncoder.java‎

Copy file name to clipboardExpand all lines: codec/src/main/java/io/netty/handler/codec/compression/ZstdEncoder.java
+6-4Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import static io.netty.handler.codec.compression.ZstdConstants.MIN_COMPRESSION_LEVEL;
2929
import static io.netty.handler.codec.compression.ZstdConstants.MAX_COMPRESSION_LEVEL;
3030
import static io.netty.handler.codec.compression.ZstdConstants.DEFAULT_BLOCK_SIZE;
31-
import static io.netty.handler.codec.compression.ZstdConstants.MAX_BLOCK_SIZE;
31+
import static io.netty.handler.codec.compression.ZstdConstants.DEFAULT_MAX_ENCODE_SIZE;
3232

3333
/**
3434
* Compresses a {@link ByteBuf} using the Zstandard algorithm.
@@ -56,7 +56,7 @@ public final class ZstdEncoder extends MessageToByteEncoder<ByteBuf> {
5656
* please use {@link ZstdEncoder(int,int)} constructor
5757
*/
5858
public ZstdEncoder() {
59-
this(DEFAULT_COMPRESSION_LEVEL, DEFAULT_BLOCK_SIZE, MAX_BLOCK_SIZE);
59+
this(DEFAULT_COMPRESSION_LEVEL, DEFAULT_BLOCK_SIZE, DEFAULT_MAX_ENCODE_SIZE);
6060
}
6161

6262
/**
@@ -65,7 +65,7 @@ public ZstdEncoder() {
6565
* specifies the level of the compression
6666
*/
6767
public ZstdEncoder(int compressionLevel) {
68-
this(compressionLevel, DEFAULT_BLOCK_SIZE, MAX_BLOCK_SIZE);
68+
this(compressionLevel, DEFAULT_BLOCK_SIZE, DEFAULT_MAX_ENCODE_SIZE);
6969
}
7070

7171
/**
@@ -113,7 +113,9 @@ protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean
113113
while (remaining > 0) {
114114
int curSize = Math.min(blockSize, remaining);
115115
remaining -= curSize;
116-
bufferSize += Zstd.compressBound(curSize);
116+
// calculate the max compressed size with Zstd.compressBound since
117+
// it returns the maximum size of the compressed data
118+
bufferSize = Math.max(bufferSize, Zstd.compressBound(curSize));
117119
}
118120

119121
if (bufferSize > maxEncodeSize || 0 > bufferSize) {
Collapse file

‎codec/src/main/java/io/netty/handler/codec/compression/ZstdOptions.java‎

Copy file name to clipboardExpand all lines: codec/src/main/java/io/netty/handler/codec/compression/ZstdOptions.java
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import static io.netty.handler.codec.compression.ZstdConstants.MIN_COMPRESSION_LEVEL;
2222
import static io.netty.handler.codec.compression.ZstdConstants.MAX_COMPRESSION_LEVEL;
2323
import static io.netty.handler.codec.compression.ZstdConstants.DEFAULT_BLOCK_SIZE;
24-
import static io.netty.handler.codec.compression.ZstdConstants.MAX_BLOCK_SIZE;
24+
import static io.netty.handler.codec.compression.ZstdConstants.DEFAULT_MAX_ENCODE_SIZE;
2525

2626
/**
2727
* {@link ZstdOptions} holds compressionLevel for
@@ -36,9 +36,10 @@ public class ZstdOptions implements CompressionOptions {
3636
/**
3737
* Default implementation of {@link ZstdOptions} with{compressionLevel(int)} set to
3838
* {@link ZstdConstants#DEFAULT_COMPRESSION_LEVEL},{@link ZstdConstants#DEFAULT_BLOCK_SIZE},
39-
* {@link ZstdConstants#MAX_BLOCK_SIZE}
39+
* {@link ZstdConstants#DEFAULT_MAX_ENCODE_SIZE}
4040
*/
41-
static final ZstdOptions DEFAULT = new ZstdOptions(DEFAULT_COMPRESSION_LEVEL, DEFAULT_BLOCK_SIZE, MAX_BLOCK_SIZE);
41+
static final ZstdOptions DEFAULT = new ZstdOptions(DEFAULT_COMPRESSION_LEVEL, DEFAULT_BLOCK_SIZE,
42+
DEFAULT_MAX_ENCODE_SIZE);
4243

4344
/**
4445
* Create a new {@link ZstdOptions}
Collapse file

‎codec/src/test/java/io/netty/handler/codec/compression/ZstdEncoderTest.java‎

Copy file name to clipboardExpand all lines: codec/src/test/java/io/netty/handler/codec/compression/ZstdEncoderTest.java
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ public void setup() {
4949
when(ctx.alloc()).thenReturn(ByteBufAllocator.DEFAULT);
5050
}
5151

52+
public static ByteBuf[] hugeData() {
53+
final byte[] bytesHuge = new byte[36 * 1024 * 1024];
54+
ByteBuf heap = Unpooled.wrappedBuffer(bytesHuge);
55+
ByteBuf direct = Unpooled.directBuffer(bytesHuge.length);
56+
direct.writeBytes(bytesHuge);
57+
return new ByteBuf[] {heap, direct};
58+
}
59+
5260
@Override
5361
public EmbeddedChannel createChannel() {
5462
return new EmbeddedChannel(new ZstdEncoder());
@@ -57,6 +65,16 @@ public EmbeddedChannel createChannel() {
5765
@ParameterizedTest
5866
@MethodSource("largeData")
5967
public void testCompressionOfLargeBatchedFlow(final ByteBuf data) throws Exception {
68+
testCompressionOfLargeDataBatchedFlow(data);
69+
}
70+
71+
@ParameterizedTest
72+
@MethodSource("hugeData")
73+
public void testCompressionOfHugeBatchedFlow(final ByteBuf data) throws Exception {
74+
testCompressionOfLargeDataBatchedFlow(data);
75+
}
76+
77+
private void testCompressionOfLargeDataBatchedFlow(final ByteBuf data) throws Exception {
6078
final int dataLength = data.readableBytes();
6179
int written = 0;
6280

Collapse file

‎codec/src/test/java/io/netty/handler/codec/compression/ZstdIntegrationTest.java‎

Copy file name to clipboardExpand all lines: codec/src/test/java/io/netty/handler/codec/compression/ZstdIntegrationTest.java
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717

1818
import io.netty.channel.embedded.EmbeddedChannel;
1919

20-
import static io.netty.handler.codec.compression.ZstdConstants.MAX_BLOCK_SIZE;
20+
import static io.netty.handler.codec.compression.ZstdConstants.DEFAULT_MAX_ENCODE_SIZE;
2121

2222
public class ZstdIntegrationTest extends AbstractIntegrationTest {
2323

2424
private static final int BLOCK_SIZE = 1 << 20;
2525

2626
@Override
2727
protected EmbeddedChannel createEncoder() {
28-
return new EmbeddedChannel(new ZstdEncoder(BLOCK_SIZE, MAX_BLOCK_SIZE));
28+
return new EmbeddedChannel(new ZstdEncoder(BLOCK_SIZE, DEFAULT_MAX_ENCODE_SIZE));
2929
}
3030

3131
@Override

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.