Optimize JSON serializer#5
Merged
Merged
Conversation
Code Coverage OverviewLanguages: Swift Swift / code-coverage/llvm-covThe overall coverage in the Show a code coverage summary of the most impacted files.
Code Coverage is in Public Preview. Learn more and provide us with your feedback. |
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.
Summary
Rewrites the serializer core to write into a
[UInt8]buffer instead of building aStringand re-walking it. Public API (toString/toData/SerializationOptions) and output are unchanged and byte-identical. All techniques are Embedded-safe.Independent of the parser work in #4 (different file), so the two can merge in any order.
Changes
toData()returns the accumulated[UInt8]directly;toString()wraps it inString(decoding:). PreviouslytoData()built aStringthen re-encoded it to UTF-8 — two full traversals.append(contentsOf:)over the source UTF-8, replacing the per-unicodeScalarappend.withUnsafeTemporaryAllocation), replacing.descriptionString allocations.Int64.minhandled via unsigned negation.\u00XXcontrol-escape table is a static[UInt8]instead of a[Character]array rebuilt on every string.Performance (1.10 MB output / 5000 objects, release, Apple Silicon)
toData()toData()toString()~3.5× faster; the remaining 1.22× vs the reference is its buffer capacity pre-estimate (a recursive size walk we chose not to add — diminishing returns for the extra pass).
Verification
wasm-embeddedrelease build clean