Skip to content

[Feature] Add "fast" AttributeValue factory methods to avoid Builder allocations and wire into DDB Enhanced Client#7107

Open
RanVaknin wants to merge 5 commits into
masterfrom
feature/master/DDB-enhanced-fast-AV-construction-optimization
Open

[Feature] Add "fast" AttributeValue factory methods to avoid Builder allocations and wire into DDB Enhanced Client#7107
RanVaknin wants to merge 5 commits into
masterfrom
feature/master/DDB-enhanced-fast-AV-construction-optimization

Conversation

@RanVaknin

@RanVaknin RanVaknin commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds a codegen customization that generates per member static factory methods (createS, createN, createM, etc.) on union shapes, scoped exclusively to DynamoDB's AttributeValue, and wires those factories into the DDB Enhanced Client's WRITE path converters.

Wiring into the READ path (JSON unmarshalling) would require changes to the JSON protocol layer and is out of scope.

This feature branch consists of:

Context

AttributeValue construction and allocations is a substantial performance factor for the DDB client and consequently the DDB Enhanced client accounting for 1 to 15% overall CPU time (and on the HUGE payload size it accounted for 50% of overall Memory Allocations).

AttributeValue construction is a measurable cost on the DDB Enhanced Client write path. Each call to AttributeValue.builder().s(value).build() performs a 3 allocations:
1. builder() allocates a BuilderImpl and an EnumSet to track which union member is set
2. .s(value) assigns the value and runs handleUnionValueChange (EnumSet add + iterator)
3. .build() allocates the final immutable AttributeValue, copying fields from the builder

The createX factories eliminate steps 1 and 2 entirely.

Results:

Throughput (ops/s)

Payload Master CreateX Improvement
TINY 27,398 30,063 +9.7%
SMALL 23,346 26,066 +11.7%
HUGE 8,115 10,582 +30.4%
HUGE_FLAT 11,227 13,739 +22.4%

Allocations (B/op)

Payload Master CreateX Reduction
TINY 46,256 45,872 −384 B (−0.8%)
SMALL 48,292 47,472 −820 B (−1.7%)
HUGE 93,152 75,696 −17,456 B (−18.7%)
HUGE_FLAT 71,660 62,988 −8,672 B (−12.1%)

Implementaion

The codegen (AwsServiceModel.java) now supports a generateDirectUnionConstructors customization, a list of shape names for which to emit direct factories. When enabled for a shape, it generates:

A shared constant for the null/unset case:

private static final AttributeValue UNSET_INSTANCE = new AttributeValue(
    Type.UNKNOWN_TO_SDK_VERSION, null, null, null,
    DefaultSdkAutoConstructList.getInstance(), ...,
    DefaultSdkAutoConstructMap.getInstance(), ..., null, null);

A private all-args constructor that assigns every field directly (no builder, no EnumSet):

private AttributeValue(Type type, String s, String n, SdkBytes b, ...) {
    this.type = type;
    this.s = s;
    this.n = n;
    ...
}

A public static factory per union member , slightly different for scalars and for collection types (collections need a copy since they are mutable)

public static AttributeValue createS(String s) {
    if (s == null) {
        return UNSET_INSTANCE;
    }
    return new AttributeValue(Type.S, s, null, null, <sentinels>, null, null);
}

The flag is enabled for DynamoDB and DynamoDB Streams only (the sole union in both models is AttributeValue).

…ircumvent builder pattern (#7039)

* Add generated opt in fast construction factories for DDB AttributeValue to bypass builder allocations

* Remove benchmark to use the existing Enhanced Client benchmark instead

* Add FAST_UNSET to avoid repetitive codegen

* Change scope of customization config from per service, to target specific shapes

* Refactor collection type factories to use FAST_UNSET, codegen guard to apply only to MODEL shapes, and require copiers to generate

* Remove customization to generate boilerplate codegen drift (without our changes) for ease of review

* Add back customization flag to generate only codegen diff relevant to this PR

* Add javadoc and more test coverage

* Remove customization flag to showcase changes in subsequent commit

* Re-enable flag for ease of review of codegen changes

* Change factory method name from fastX() to createX()

* Add validation logic
@RanVaknin RanVaknin added the api-surface-area-approved-by-team Indicate API surface area introduced by this PR has been approved by team label Jul 7, 2026
@RanVaknin RanVaknin marked this pull request as ready for review July 7, 2026 19:00
@RanVaknin RanVaknin requested a review from a team as a code owner July 7, 2026 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api-surface-area-approved-by-team Indicate API surface area introduced by this PR has been approved by team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant