Skip to content

Reduce generics in Codable #31278

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 3 commits into from
Apr 29, 2020
Merged

Reduce generics in Codable #31278

merged 3 commits into from
Apr 29, 2020

Conversation

tbkka
Copy link
Contributor

@tbkka tbkka commented Apr 24, 2020

In particular, types generic on the CodingKey produce a lot of runtime metadata.
Reducing the number of such types should help with some of the reported memory
bloat from Codable.

Resolves rdar://62620208

In particular, types generic on the CodingKey produce a lot of runtime metadata.
Reducing the number of such types should help with some of the reported memory
bloat from Codable.
@tbkka
Copy link
Contributor Author

tbkka commented Apr 24, 2020

@swift-ci Please benchmark

@swift-ci
Copy link
Contributor

Performance: -O

Regression OLD NEW DELTA RATIO
UTF8Decode_InitDecoding 142 167 +17.6% 0.85x
UTF8Decode_InitFromCustom_contiguous 139 163 +17.3% 0.85x
ObjectiveCBridgeStringHash 72 81 +12.5% 0.89x
Set.subtracting.Empty.Box 8 9 +12.5% 0.89x (?)
OpenClose 56 61 +8.9% 0.92x (?)
UTF8Decode_InitFromCustom_noncontiguous 274 298 +8.8% 0.92x (?)
 
Improvement OLD NEW DELTA RATIO
MapReduce 219 197 -10.0% 1.11x (?)
RemoveWhereSwapInts 42 38 -9.5% 1.11x (?)
DistinctClassFieldAccesses 201 183 -9.0% 1.10x (?)
ArrayPlusEqualFiveElementCollection 5254 4847 -7.7% 1.08x (?)
ObjectiveCBridgeStubNSDateRefAccess 525 486 -7.4% 1.08x (?)
PrefixWhileSequence 192 178 -7.3% 1.08x (?)
ArraySetElement 305 284 -6.9% 1.07x (?)

Code size: -O

Performance: -Osize

Regression OLD NEW DELTA RATIO
UTF8Decode_InitDecoding 140 163 +16.4% 0.86x (?)
UTF8Decode_InitFromCustom_contiguous 143 164 +14.7% 0.87x
ObjectiveCBridgeStringHash 72 81 +12.5% 0.89x
StringComparison_ascii 348 384 +10.3% 0.91x (?)
Chars2 3200 3500 +9.4% 0.91x (?)
UTF8Decode_InitFromCustom_noncontiguous 276 301 +9.1% 0.92x (?)
 
Improvement OLD NEW DELTA RATIO
MapReduce 246 218 -11.4% 1.13x (?)
Set.isStrictSubset.Int.Empty 52 47 -9.6% 1.11x (?)
Set.isStrictSubset.Seq.Int.Empty 118 107 -9.3% 1.10x (?)
MapReduceAnyCollection 237 216 -8.9% 1.10x (?)
FlattenListLoop 2807 2584 -7.9% 1.09x (?)
ArraySetElement 283 262 -7.4% 1.08x (?)
Array2D 4544 4208 -7.4% 1.08x (?)

Code size: -Osize

Performance: -Onone

Regression OLD NEW DELTA RATIO
UTF8Decode_InitFromCustom_contiguous 151 176 +16.6% 0.86x
UTF8Decode_InitDecoding 148 172 +16.2% 0.86x (?)
ObjectiveCBridgeStringHash 72 81 +12.5% 0.89x
ArrayOfPOD 644 723 +12.3% 0.89x (?)
StringWalk 2760 3040 +10.1% 0.91x (?)
UTF8Decode_InitFromData 160 175 +9.4% 0.91x (?)
UTF8Decode_InitDecoding_ascii_as_ascii 216 236 +9.3% 0.92x (?)
 
Improvement OLD NEW DELTA RATIO
CharacterLiteralsLarge 374 346 -7.5% 1.08x (?)
Breadcrumbs.MutatedUTF16ToIdx.ASCII 15 14 -6.7% 1.07x (?)

Code size: -swiftlibs

How to read the data The tables contain differences in performance which are larger than 8% and differences in code size which are larger than 1%.

If you see any unexpected regressions, you should consider fixing the
regressions before you merge the PR.

Noise: Sometimes the performance results (not code size!) contain false
alarms. Unexpected regressions which are marked with '(?)' are probably noise.
If you see regressions which you cannot explain you can try to run the
benchmarks again. If regressions still show up, please consult with the
performance team (@eeckstein).

Hardware Overview
  Model Name: Mac mini
  Model Identifier: Macmini8,1
  Processor Name: 6-Core Intel Core i7
  Processor Speed: 3.2 GHz
  Number of Processors: 1
  Total Number of Cores: 6
  L2 Cache (per Core): 256 KB
  L3 Cache: 12 MB
  Memory: 64 GB

@tbkka
Copy link
Contributor Author

tbkka commented Apr 24, 2020

This didn't catch any perf regressions on the JSON encode/decode tests. I think we can dismiss the UTF8Decode checks above as unrelated noise, since they're not testing Codable/Decodable.

@tbkka
Copy link
Contributor Author

tbkka commented Apr 24, 2020

@swift-ci Please benchmark

@@ -3845,237 +3843,237 @@ internal class _KeyedDecodingContainerBase<Key: CodingKey> {
fatalError("_KeyedDecodingContainerBase cannot be used directly.")
}

internal var allKeys: [Key] {
internal var allKeys: [CodingKey] {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jckarter I don't suppose you have any ideas for improving this one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Do we have a way using standard library SPI that we could just store the reference to the array's buffer object here, and construct the Array<Key> shell around it in the getter?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the SPI doesn't exist, maybe you could have this variable be of type _ArrayBridgeStorage and bitcast it back and forth to Array<K> in the methods that access it.

@swift-ci
Copy link
Contributor

Performance: -O

Regression OLD NEW DELTA RATIO
AnyHashableWithAClass 119000 129000 +8.4% 0.92x (?)

Code size: -O

Performance: -Osize

Regression OLD NEW DELTA RATIO
ObjectiveCBridgeStubToNSDate2 550 640 +16.4% 0.86x (?)
AnyHashableWithAClass 119000 129000 +8.4% 0.92x (?)
PlistPerfEncode 494 535 +8.3% 0.92x (?)
 
Improvement OLD NEW DELTA RATIO
DictionaryBridgeToObjC_Access 961 828 -13.8% 1.16x (?)
FlattenListFlatMap 7657 6977 -8.9% 1.10x (?)

Code size: -Osize

Performance: -Onone

Regression OLD NEW DELTA RATIO
PlistPerfEncode 513 560 +9.2% 0.92x (?)
AnyHashableWithAClass 120500 130500 +8.3% 0.92x (?)
ObjectiveCBridgeFromNSArrayAnyObjectForced 6940 7500 +8.1% 0.93x (?)
ArrayOfPOD 1030 1110 +7.8% 0.93x (?)

Code size: -swiftlibs

How to read the data The tables contain differences in performance which are larger than 8% and differences in code size which are larger than 1%.

If you see any unexpected regressions, you should consider fixing the
regressions before you merge the PR.

Noise: Sometimes the performance results (not code size!) contain false
alarms. Unexpected regressions which are marked with '(?)' are probably noise.
If you see regressions which you cannot explain you can try to run the
benchmarks again. If regressions still show up, please consult with the
performance team (@eeckstein).

Hardware Overview
  Model Name: Mac Pro
  Model Identifier: MacPro6,1
  Processor Name: 12-Core Intel Xeon E5
  Processor Speed: 2.7 GHz
  Number of Processors: 1
  Total Number of Cores: 12
  L2 Cache (per Core): 256 KB
  L3 Cache: 30 MB
  Memory: 64 GB

… generic and manually asserting that the types match.

Based on a suggestion of @jckarter
@tbkka
Copy link
Contributor Author

tbkka commented Apr 24, 2020

I implemented @jckarter's suggestion -- let's see if that addresses the perf regression here.

@tbkka
Copy link
Contributor Author

tbkka commented Apr 24, 2020

@swift-ci Please benchmark

@tbkka
Copy link
Contributor Author

tbkka commented Apr 24, 2020

@swift-ci Please benchmark

@swift-ci
Copy link
Contributor

Performance: -O

Improvement OLD NEW DELTA RATIO
FlattenListFlatMap 8468 6138 -27.5% 1.38x (?)
FlattenListLoop 4724 4012 -15.1% 1.18x (?)
PrefixAnyCollectionLazy 91267 84599 -7.3% 1.08x (?)
ObjectiveCBridgeFromNSSetAnyObjectToString 69000 64000 -7.2% 1.08x (?)

Code size: -O

Performance: -Osize

Regression OLD NEW DELTA RATIO
FlattenListFlatMap 5429 6641 +22.3% 0.82x (?)
ObjectiveCBridgeStubToNSDate2 500 550 +10.0% 0.91x (?)
 
Improvement OLD NEW DELTA RATIO
ObjectiveCBridgeFromNSArrayAnyObjectForced 4220 3780 -10.4% 1.12x (?)

Code size: -Osize

Performance: -Onone

Improvement OLD NEW DELTA RATIO
ArrayOfPOD 982 907 -7.6% 1.08x (?)

Code size: -swiftlibs

How to read the data The tables contain differences in performance which are larger than 8% and differences in code size which are larger than 1%.

If you see any unexpected regressions, you should consider fixing the
regressions before you merge the PR.

Noise: Sometimes the performance results (not code size!) contain false
alarms. Unexpected regressions which are marked with '(?)' are probably noise.
If you see regressions which you cannot explain you can try to run the
benchmarks again. If regressions still show up, please consult with the
performance team (@eeckstein).

Hardware Overview
  Model Name: Mac Pro
  Model Identifier: MacPro6,1
  Processor Name: 8-Core Intel Xeon E5
  Processor Speed: 3 GHz
  Number of Processors: 1
  Total Number of Cores: 8
  L2 Cache (per Core): 256 KB
  L3 Cache: 25 MB
  Memory: 64 GB

@tbkka
Copy link
Contributor Author

tbkka commented Apr 27, 2020

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - cc1bf62

@tbkka
Copy link
Contributor Author

tbkka commented Apr 27, 2020

@swift-ci Please test macOS

@tbkka tbkka marked this pull request as ready for review April 27, 2020 22:34
@tbkka tbkka merged commit 70b3f01 into swiftlang:master Apr 29, 2020
@tbkka
Copy link
Contributor Author

tbkka commented May 5, 2020

@swift-ci Please nominate

Explanation: Reduce memory requirements for Codable/Decodable support by making internal types non-generic
Scope: Changes to private classes within Codable/Decodable implementation
Radar: rdar://62620208
Risk: Possible performance impacts to Codable/Decodable users
Testing: Full CI, local testing, CI benchmarking

tbkka added a commit to tbkka/swift that referenced this pull request May 13, 2020
In particular, types generic on the CodingKey produce a lot of runtime metadata.
Reducing the number of such types should help with some of the reported memory
bloat from Codable.

Based on a suggestion of @jckarter

Resolves rdar://62620208
tbkka added a commit that referenced this pull request May 14, 2020
In particular, types generic on the CodingKey produce a lot of runtime metadata.
Reducing the number of such types should help with some of the reported memory
bloat from Codable.

Based on a suggestion of @jckarter

Resolves rdar://62620208
@tbkka tbkka deleted the tbkka-decodable branch October 16, 2020 00:33
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.

3 participants