adding strategy serialization optimization #6507
Draft
+316
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.



This PR addresses a performance degradation issue between v1 and v2. After considering multiple implementation approaches, the proposed optimization approach yielded roughly 10% reduction in latency from the v1 performance.
toJson() Benchmarks
SDKJsonGenerator- writes tobyteArrayOutputStream(enum based dispatch)JsonGenerator- writes tostringWriter(enum based dispatch)SDKJsonGenerator(static method dispatch)getJson() Benchmarks
2KB payload
50KB payload
Key Design Decisions
1. Enum based dispatch outperforms static methods through JIT optimization
When the JVM encounters the enum approach, it sees strategy.serialize(generator, av) calls where each enum constant (STRING, NUMBER, etc.) is a singleton instance. The JIT compiler recognizes these as monomorphic call sites - meaning each location in the code consistently calls the same concrete method.
In contrast, the static method approach forces all serialization logic through a single serializeAttributeValue() method containing if-else branches. While the JIT can optimize the branching logic, it cannot eliminate the method call itself or the internal conditional overhead.
The JFR data confirms that the enum implementation approach generated 1,333 Jackson Generator method calls compared to 2,159 in the static approach.
2. SdkJsonGenerator (ByteArrayOutputStream) over JsonGenerator (StringWriter)
Tested both serialization approaches with identical enum dispatch logic:
•
SdkJsonGeneratorwrapsJsonFactory.createGenerator(ByteArrayOutputStream)→ createsUTF8JsonGenerator- 19% faster (550µs vs 680µs at p90)•
JsonGeneratorwithJsonFactory.createGenerator(StringWriter)→ createsWriterBasedJsonGeneratorUTF8JsonGeneratorescapes non ASCII as \uXXXX by default. Jackson 2.18+ providesCOMBINE_UNICODE_SURROGATES_IN_UTF8feature to output literal UTF-8 (verified via test), but the SDK bundles older shaded Jackson version that lacks this feature.see FasterXML/jackson-core#223