-
Notifications
You must be signed in to change notification settings - Fork 189
Fix JSONEncoder performance regression in 6.0 toolchain #1006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
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
@swift-ci Please test |
itingliu
approved these changes
Oct 25, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any performance statistics you can share?
Sure! @jmschonfeld ran our JSON encoding benchmarks for me on Ubuntu, comparing the 5.10, 6.0, and 6.0+this PR versions Canada-encodeToJSON:
Twitter-encodeToJSON:
|
kperryua
added a commit
to kperryua/swift-foundation
that referenced
this pull request
Oct 25, 2024
… toolchain to 6.0) (swiftlang#1006)" This reverts commit 19abae7.
kperryua
added a commit
to kperryua/swift-foundation
that referenced
this pull request
Oct 25, 2024
kperryua
added a commit
that referenced
this pull request
Oct 28, 2024
cthielen
pushed a commit
to cthielen/swift-foundation
that referenced
this pull request
Nov 8, 2024
cthielen
pushed a commit
to cthielen/swift-foundation
that referenced
this pull request
Nov 8, 2024
… toolchain to 6.0) (swiftlang#1006)" (swiftlang#1010) This reverts commit 19abae7.
cthielen
pushed a commit
to cthielen/swift-foundation
that referenced
this pull request
Nov 8, 2024
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.
Major refactoring of Darwin Foundation's (and hence swift-foundation's)
JSONEncoder
took place a couple of years ago, with the rewrite being entirely in Swift, no longer relying onNSJSONSerialization
. This effort resulted in a significant performance boost on Darwin. However, when swift-corelibs-foundation was re-cored on top of swift-foundation in 6.0, a performance regression was discovered there compared to the 5.10 toolchain.After examining the code from the 5.10 toolchain's
JSONEncoder
, I've adapted the swift-foundation implementation to use its general architecture, with some additional important correctness fixes and other adjustments. I also included some other optimizations on top of it. All of this resulted in slightly improved performance in usage within the toolchain (over 5.10), as well via the swift-foundation package and Darwin Foundation.framework over previous.To summarize the various changes:
[UInt8]
instead of aData
. It's unfortunately noticeably more expensive to serialize toData
right now.String
allocations when serializing escaped characters inString
s.JSONReference
(mutable reference / leaf value combination) withJSONEncoderValue
(immutable value type) +JSONFuture
(container of either a value or one of the reference types) +JSONFuture.RefArray
+JSONFuture.RefObject
(pure reference types). This approach resulted in less overall retain/release traffic, at the cost of some rare "unwrapping" ofJSONEncoderValue
values back into the reference types.__JSONEncoder
responsible for one layer of decoding, instead of reusing the same one multiple times. This enabled some performance optimizations over what the 5.10 SCL-F implementation had, includingCodingKey
andCodingPath
optimizations (each encoder holds aCodingKey
, and each nested container has aCodingPathNode
linked list descending from the original encoder; the path, when needed (rarely) is constructed by walking up this chain of encoders), and__JSONEncoder
instance reuse to avoid allocation churn._JSONDirectArrayEncodable
optimization path to use[UInt8]
buffers directly (with substring lengths where needed) instead of holding on toString
s. This will enable us to have the stdlib encode straight to our buffer instead of jumping through an unnecessary transientString
allocation if such an API is ever introduced.