perf: optimize JSON serialization for OCI manifests#388
Draft
sajayantony wants to merge 4 commits into
Draft
Conversation
- Add NeedsEscaping() fast-path to skip escape pass for clean strings - Replace StringBuilder with ArrayPool<char> in EscapeJsonString - Use direct hex-digit lookup table instead of string interpolation - ASCII fast-path sort in OciDictionaryConverter (ordinal == UTF-8) - Pre-encode UTF-8 keys once for non-ASCII slow path sort - Replace DeserializeAsync with sync Deserialize for buffered streams Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Sajay Antony <sajaya@microsoft.com>
BenchmarkDotNet-based performance tests for OCI manifest serialization and deserialization paths. Covers serialize, sync deserialize, and async deserialize with varying annotation counts. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Sajay Antony <sajaya@microsoft.com>
Replace ms.ToArray() with MemoryStream.TryGetBuffer() to avoid allocating a copy of the buffer. Add Deserialize<T>(ReadOnlySpan<byte>) overload to support span-based deserialization from buffer segments. This reduces GC pressure in multi-tenant server scenarios where many manifests are deserialized concurrently. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Sajay Antony <sajaya@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Sajay Antony <sajaya@microsoft.com>
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #388 +/- ##
==========================================
- Coverage 92.03% 91.85% -0.19%
==========================================
Files 67 67
Lines 2938 3069 +131
Branches 380 398 +18
==========================================
+ Hits 2704 2819 +115
- Misses 139 152 +13
- Partials 95 98 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Performance optimizations for the Go-compatible JSON serialization introduced in #348. Now that #366 has merged comprehensive serialization tests, we have high confidence these changes preserve correctness.
Changes
Fast-path escape detection (
NeedsEscaping())Allocation reduction
StringBuilderwithArrayPool<char>inEscapeJsonStringReadOnlySpan<byte>instead of string interpolationASCII fast-path sort (
OciDictionaryConverter)string.CompareOrdinal— equivalent to UTF-8 bytewise order with zero extra allocationsZero-copy deserialization for buffered streams (
ManifestStore)MemoryStream.TryGetBuffer()instead of.ToArray()to avoid buffer copyDeserialize<T>(ReadOnlySpan<byte>)overload for span-based deserializationDeserializeAsyncwith syncDeserializefor already-buffered data — async adds ~50-80% overhead for no benefit on buffered streamsBenchmark project (
benchmarks/)benchmarks/README.mdfor usageThread Safety (Multi-Tenant Server Use)
All changes are safe for concurrent multi-tenant use:
NeedsEscaping()EscapeJsonString()ArrayPoolSortByUtf8Key()ArrayPoolHexDigitsReadOnlySpanNo static dictionaries, caches, or shared mutable collections introduced.
Benchmark Results
Known Issues / Follow-ups
OciJsonSerializer— acceptable for perf testing but will break if internals are renamed. Consider making benchmark types InternalsVisibleTo in a follow-up.Testing
All 502 existing tests pass. The serialization test suite (#366) provides comprehensive coverage for the escaping and sorting behavior.
Generated with AI assistance