swiftlang / swift-foundation

The Foundation project
Apache License 2.0
2.4k stars 160 forks source link

Fix JSONEncoder performance regression in 6.0 toolchain #1006

Closed kperryua closed 2 weeks ago

kperryua commented 2 weeks ago

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:

kperryua commented 2 weeks ago

@swift-ci Please test

kperryua commented 2 weeks ago

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: