Skip to content

Commit b71bc56

Browse files
committed
Extract SdkByteArrayOutputStream and GC optimizations to separate branch
Remove optimizations 3 (SdkByteArrayOutputStream for G1GC humongous allocation avoidance) and 4 (contentStreamProvider/contentSize zero-copy streaming) from this branch. These will be reviewed separately on alexwoo/json_huge_gc_opt. This branch now contains only: - Optimization 1: Megamorphic interface dispatch fix (switch-based marshallPayloadField) - Optimization 2: Redundant registry lookup caching (marshallFieldViaRegistry with ConcurrentHashMap cache) - writeBinaryValue addition (used by SDK_BYTES switch case)
1 parent 5198d8e commit b71bc56

6 files changed

Lines changed: 8 additions & 761 deletions

File tree

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

Lines changed: 0 additions & 225 deletions
This file was deleted.

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515

1616
package software.amazon.awssdk.protocols.json;
1717

18+
import java.io.ByteArrayOutputStream;
1819
import java.io.IOException;
1920
import java.math.BigDecimal;
2021
import java.math.BigInteger;
2122
import java.nio.ByteBuffer;
2223
import java.time.Instant;
2324
import software.amazon.awssdk.annotations.SdkProtectedApi;
2425
import software.amazon.awssdk.core.exception.SdkClientException;
25-
import software.amazon.awssdk.http.ContentStreamProvider;
2626
import software.amazon.awssdk.thirdparty.jackson.core.JsonFactory;
2727
import software.amazon.awssdk.thirdparty.jackson.core.JsonGenerator;
2828
import software.amazon.awssdk.utils.BinaryUtils;
@@ -39,7 +39,7 @@ public class SdkJsonGenerator implements StructuredJsonGenerator {
3939
* prevent frequent resizings but small enough to avoid wasted allocations for small requests.
4040
*/
4141
private static final int DEFAULT_BUFFER_SIZE = 1024;
42-
private final SdkByteArrayOutputStream baos = new SdkByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
42+
private final ByteArrayOutputStream baos = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
4343
private final JsonGenerator generator;
4444
private final String contentType;
4545

@@ -287,24 +287,6 @@ public byte[] getBytes() {
287287
return baos.toByteArray();
288288
}
289289

290-
/**
291-
* Returns the size of the generated content in bytes without copying.
292-
*/
293-
public int contentSize() {
294-
close();
295-
return baos.contentSize();
296-
}
297-
298-
/**
299-
* Returns a {@link ContentStreamProvider} that streams directly from the internal buffers
300-
* without creating a contiguous copy. For small payloads this wraps the single base buffer;
301-
* for large payloads it chains the base buffer and overflow chunks.
302-
*/
303-
public ContentStreamProvider contentStreamProvider() {
304-
close();
305-
return baos.contentStreamProvider();
306-
}
307-
308290
@Override
309291
public String getContentType() {
310292
return contentType;

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

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515

1616
package software.amazon.awssdk.protocols.json;
1717

18-
import java.io.ByteArrayInputStream;
1918
import java.math.BigDecimal;
2019
import java.math.BigInteger;
2120
import java.nio.ByteBuffer;
2221
import java.time.Instant;
2322
import software.amazon.awssdk.annotations.SdkProtectedApi;
24-
import software.amazon.awssdk.http.ContentStreamProvider;
2523

2624
/**
2725
* Interface for generating a JSON
@@ -200,28 +198,4 @@ default StructuredJsonGenerator writeBinaryValue(byte[] bytes) {
200198
*/
201199
@Deprecated
202200
String getContentType();
203-
204-
/**
205-
* Returns the size of the generated content in bytes without copying. The default
206-
* implementation falls back to {@link #getBytes()}.length.
207-
*/
208-
default int contentSize() {
209-
byte[] bytes = getBytes();
210-
return bytes == null ? 0 : bytes.length;
211-
}
212-
213-
/**
214-
* Returns a {@link ContentStreamProvider} that streams the generated content. The default
215-
* implementation wraps the result of {@link #getBytes()} in a {@code ByteArrayInputStream}.
216-
* Implementations may override this to stream directly from internal buffers without copying.
217-
*
218-
* @return a content stream provider, or {@code null} if {@link #getBytes()} returns null
219-
*/
220-
default ContentStreamProvider contentStreamProvider() {
221-
byte[] bytes = getBytes();
222-
if (bytes == null) {
223-
return null;
224-
}
225-
return () -> new ByteArrayInputStream(bytes);
226-
}
227201
}

core/protocols/aws-json-protocol/src/main/java/software/amazon/awssdk/protocols/json/internal/marshall/JsonProtocolMarshaller.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import software.amazon.awssdk.core.traits.RequiredTrait;
4444
import software.amazon.awssdk.core.traits.TimestampFormatTrait;
4545
import software.amazon.awssdk.core.traits.TraitType;
46-
import software.amazon.awssdk.http.ContentStreamProvider;
4746
import software.amazon.awssdk.http.SdkHttpFullRequest;
4847
import software.amazon.awssdk.protocols.core.InstantToString;
4948
import software.amazon.awssdk.protocols.core.OperationInfo;
@@ -291,12 +290,12 @@ private SdkHttpFullRequest finishMarshalling() {
291290
jsonGenerator.writeEndObject();
292291
}
293292

294-
ContentStreamProvider contentProvider = jsonGenerator.contentStreamProvider();
295-
if (contentProvider != null) {
296-
request.contentStreamProvider(contentProvider);
297-
int contentSize = jsonGenerator.contentSize();
298-
if (contentSize > 0) {
299-
request.putHeader(CONTENT_LENGTH, Integer.toString(contentSize));
293+
byte[] content = jsonGenerator.getBytes();
294+
295+
if (content != null) {
296+
request.contentStreamProvider(() -> new ByteArrayInputStream(content));
297+
if (content.length > 0) {
298+
request.putHeader(CONTENT_LENGTH, Integer.toString(content.length));
300299
}
301300
}
302301
}

0 commit comments

Comments
 (0)