Skip to content

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
merged 1 commit into from
Oct 25, 2024

Conversation

kperryua
Copy link
Contributor

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 on NSJSONSerialization. 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:

  • Serialize to a [UInt8] instead of a Data. It's unfortunately noticeably more expensive to serialize to Data right now.
  • Avoid unnecessary String allocations when serializing escaped characters in Strings.
  • Replace JSONReference (mutable reference / leaf value combination) with JSONEncoderValue (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" of JSONEncoderValue values back into the reference types.
  • Make each __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, including CodingKey and CodingPath optimizations (each encoder holds a CodingKey, and each nested container has a CodingPathNode 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.
  • Reimplemented the _JSONDirectArrayEncodable optimization path to use [UInt8] buffers directly (with substring lengths where needed) instead of holding on to Strings. This will enable us to have the stdlib encode straight to our buffer instead of jumping through an unnecessary transient String allocation if such an API is ever introduced.

@kperryua kperryua requested a review from itingliu October 25, 2024 15:49
@kperryua
Copy link
Contributor Author

@swift-ci Please test

Copy link
Contributor

@itingliu itingliu left a 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?

@kperryua
Copy link
Contributor Author

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:

  • 5.0: 15 iters/s
  • 6.0: 13 iters/s
  • This PR: 20 iters/s

Twitter-encodeToJSON:

  • 5.0: 549 iters/s
  • 6.0: 353 iters/s
  • This PR: 658 iters/s

@kperryua kperryua merged commit 19abae7 into swiftlang:main Oct 25, 2024
3 checks passed
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 25, 2024
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
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants