Skip to content

Commit e93c62f

Browse files
committed
Improve testing
1 parent b4ae8a7 commit e93c62f

5 files changed

Lines changed: 378 additions & 97 deletions

File tree

core/protocols/aws-json-protocol/src/main/java/software/amazon/awssdk/protocols/json/SdkByteArrayOutputStream.java

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,28 @@
2424
import java.util.ArrayList;
2525
import java.util.Collections;
2626
import java.util.List;
27+
import software.amazon.awssdk.annotations.NotThreadSafe;
2728
import software.amazon.awssdk.annotations.SdkInternalApi;
2829
import software.amazon.awssdk.http.ContentStreamProvider;
2930

3031
/**
3132
* A {@link ByteArrayOutputStream} subclass that behaves identically to the JDK implementation for
32-
* small payloads, but caps internal buffer growth to avoid G1GC humongous object allocations for
33+
* small payloads, but caps internal buffer growth to avoid large object allocations for
3334
* large payloads.
3435
*
35-
* <p><b>How it works:</b> Writes flow into the inherited {@code ByteArrayOutputStream} buffer
36+
* <p>
37+
* Writes flow into the inherited {@code ByteArrayOutputStream} buffer
3638
* normally. When a write would cause the buffer to grow beyond {@link #MAX_BUFFER_SIZE}, the
3739
* current buffer contents are frozen into the first "chunk" and subsequent writes go into
3840
* fixed-size overflow chunks ({@link #CHUNK_SIZE} bytes each). No single allocation ever exceeds
3941
* {@code MAX_BUFFER_SIZE}.
4042
*
41-
* <p><b>Performance characteristics:</b>
42-
* <ul>
43-
* <li>Payloads &le; {@code MAX_BUFFER_SIZE}: Identical to {@code ByteArrayOutputStream} — the
44-
* JIT can inline and optimize the write path exactly as it does for the stock class. Zero
45-
* overhead.</li>
46-
* <li>Payloads &gt; {@code MAX_BUFFER_SIZE}: Overflow writes go through a simple chunked path.
47-
* This is slightly slower per-byte than {@code ByteArrayOutputStream}'s doubling strategy,
48-
* but avoids allocations that exceed half the G1 region size.</li>
49-
* </ul>
50-
*
51-
* <p>This class is not thread-safe.
5243
*/
44+
@NotThreadSafe
5345
@SdkInternalApi
5446
final class SdkByteArrayOutputStream extends ByteArrayOutputStream {
55-
56-
/**
57-
* Maximum size of the primary ByteArrayOutputStream buffer before overflow kicks in.
58-
* Chosen to be well below the G1 humongous threshold (region_size / 2). With a 4 GB heap
59-
* the default region size is 2 MB, so humongous threshold is 1 MB. 128 KB is safely below
60-
* that for any reasonable heap size (humongous threshold is 512 KB for a 256 MB heap).
61-
*/
62-
static final int MAX_BUFFER_SIZE = 128 * 1024;
63-
64-
/**
65-
* Size of each overflow chunk. 64 KB is well below any G1 humongous threshold.
66-
*/
47+
// 128 KB, choosen to be well below 1 MB "humongous threshold" for most heap sizes
48+
static final int MAX_BUFFER_SIZE = 128 * 1024;
6749
static final int CHUNK_SIZE = 64 * 1024;
6850

6951
private List<byte[]> overflowChunks;

0 commit comments

Comments
 (0)