|
24 | 24 | import java.util.ArrayList; |
25 | 25 | import java.util.Collections; |
26 | 26 | import java.util.List; |
| 27 | +import software.amazon.awssdk.annotations.NotThreadSafe; |
27 | 28 | import software.amazon.awssdk.annotations.SdkInternalApi; |
28 | 29 | import software.amazon.awssdk.http.ContentStreamProvider; |
29 | 30 |
|
30 | 31 | /** |
31 | 32 | * 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 |
33 | 34 | * large payloads. |
34 | 35 | * |
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 |
36 | 38 | * normally. When a write would cause the buffer to grow beyond {@link #MAX_BUFFER_SIZE}, the |
37 | 39 | * current buffer contents are frozen into the first "chunk" and subsequent writes go into |
38 | 40 | * fixed-size overflow chunks ({@link #CHUNK_SIZE} bytes each). No single allocation ever exceeds |
39 | 41 | * {@code MAX_BUFFER_SIZE}. |
40 | 42 | * |
41 | | - * <p><b>Performance characteristics:</b> |
42 | | - * <ul> |
43 | | - * <li>Payloads ≤ {@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 > {@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. |
52 | 43 | */ |
| 44 | +@NotThreadSafe |
53 | 45 | @SdkInternalApi |
54 | 46 | 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; |
67 | 49 | static final int CHUNK_SIZE = 64 * 1024; |
68 | 50 |
|
69 | 51 | private List<byte[]> overflowChunks; |
|
0 commit comments