Skip to content

[stdlib] Add fast string interpolation for metatypes #32113

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
Jun 26, 2020

Conversation

airspeedswift
Copy link
Member

@airspeedswift airspeedswift commented May 31, 2020

"\(T.self)" is pretty common, and sometimes performance sensitive, but currently takes the slowest of paths. This gives them a custom string interpolation that cuts straight to what a long process of conformance checks and mirror reflection used to do.

This makes _typeName ABI (it was already public but underscored) which I think is OK.

@airspeedswift
Copy link
Member Author

@swift-ci please test

@airspeedswift airspeedswift marked this pull request as draft May 31, 2020 05:43
@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - ecdca44b9bef843f979f609b61435ebfcf33944e

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - ecdca44b9bef843f979f609b61435ebfcf33944e

@airspeedswift airspeedswift force-pushed the typename-fastpath branch 2 times, most recently from 5bcdc82 to 215806f Compare May 31, 2020 21:41
@airspeedswift
Copy link
Member Author

@swift-ci please test

@airspeedswift
Copy link
Member Author

@swift-ci please benchmark

@airspeedswift
Copy link
Member Author

@swift-ci please test source compatibility

@swift-ci
Copy link
Contributor

swift-ci commented Jun 1, 2020

Build failed
Swift Test OS X Platform
Git Sha - ecdca44b9bef843f979f609b61435ebfcf33944e

@swift-ci
Copy link
Contributor

swift-ci commented Jun 1, 2020

Build failed
Swift Test Linux Platform
Git Sha - ecdca44b9bef843f979f609b61435ebfcf33944e

@swift-ci
Copy link
Contributor

swift-ci commented Jun 1, 2020

Performance: -O

Regression OLD NEW DELTA RATIO
UTF8Decode_InitDecoding 141 167 +18.4% 0.84x
ObjectiveCBridgeStubFromNSDate 3970 4650 +17.1% 0.85x (?)
UTF8Decode_InitFromCustom_contiguous 143 167 +16.8% 0.86x
Breadcrumbs.MutatedIdxToUTF16.Mixed 186 216 +16.1% 0.86x
Breadcrumbs.MutatedUTF16ToIdx.Mixed 183 212 +15.8% 0.86x (?)
 
Improvement OLD NEW DELTA RATIO
StringToDataSmall 600 550 -8.3% 1.09x (?)
NormalizedIterator_fastPrenormal 670 620 -7.5% 1.08x (?)
NormalizedIterator_latin1 228 212 -7.0% 1.08x (?)

Code size: -O

Performance: -Osize

Regression OLD NEW DELTA RATIO
UTF8Decode_InitDecoding 144 169 +17.4% 0.85x
UTF8Decode_InitFromCustom_contiguous 144 168 +16.7% 0.86x
Breadcrumbs.MutatedUTF16ToIdx.Mixed 182 212 +16.5% 0.86x (?)
Breadcrumbs.MutatedIdxToUTF16.Mixed 186 216 +16.1% 0.86x (?)
Dictionary4OfObjects 278 317 +14.0% 0.88x
ObjectiveCBridgeStubNSDateRefAccess 479 534 +11.5% 0.90x (?)
ObjectiveCBridgeStubToNSStringRef 68 74 +8.8% 0.92x (?)
ObjectiveCBridgeStringHash 70 76 +8.6% 0.92x (?)
 
Improvement OLD NEW DELTA RATIO
MapReduce 252 219 -13.1% 1.15x (?)
PrefixWhileSequence 225 196 -12.9% 1.15x
MapReduceAnyCollection 250 218 -12.8% 1.15x
RemoveWhereSwapInts 40 36 -10.0% 1.11x (?)
PrefixWhileAnySeqCntRange 202 182 -9.9% 1.11x (?)
RandomShuffleLCG2 496 448 -9.7% 1.11x (?)
PrefixWhileAnySeqCRangeIter 203 185 -8.9% 1.10x (?)
RangeAssignment 248 227 -8.5% 1.09x (?)
ArrayPlusEqualFiveElementCollection 4921 4514 -8.3% 1.09x (?)
FlattenListLoop 3068 2818 -8.1% 1.09x (?)
RemoveWhereFilterInts 25 23 -8.0% 1.09x (?)
NormalizedIterator_fastPrenormal 690 640 -7.2% 1.08x (?)
Array2D 4624 4304 -6.9% 1.07x (?)

Code size: -Osize

Performance: -Onone

Regression OLD NEW DELTA RATIO
UTF8Decode_InitFromCustom_contiguous 150 176 +17.3% 0.85x
UTF8Decode_InitDecoding 150 173 +15.3% 0.87x (?)
Breadcrumbs.MutatedUTF16ToIdx.Mixed 198 228 +15.2% 0.87x
Breadcrumbs.MutatedIdxToUTF16.Mixed 202 231 +14.4% 0.87x (?)
ObjectiveCBridgeStringIsEqualAllSwift 56 62 +10.7% 0.90x (?)
ObjectiveCBridgeStubNSDateRefAccess 3372 3705 +9.9% 0.91x (?)
ObjectiveCBridgeStringIsEqual2 187 202 +8.0% 0.93x (?)
 
Improvement OLD NEW DELTA RATIO
ArrayOfPOD 722 643 -10.9% 1.12x (?)
StringWalk 2920 2640 -9.6% 1.11x (?)
DataSubscriptMedium 69 63 -8.7% 1.10x (?)
ArrayOfGenericPOD2 574 531 -7.5% 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 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

@airspeedswift
Copy link
Member Author

@swift-ci please benchmark

@airspeedswift airspeedswift marked this pull request as ready for review June 3, 2020 19:07
@airspeedswift airspeedswift requested a review from beccadax June 3, 2020 19:08
@swift-ci
Copy link
Contributor

swift-ci commented Jun 3, 2020

Performance: -O

Regression OLD NEW DELTA RATIO
AngryPhonebook.Strasse 182 219 +20.3% 0.83x
AngryPhonebook.Armenian 206 241 +17.0% 0.85x
AngryPhonebook.Cyrillic 221 258 +16.7% 0.86x
CharIndexing_punctuatedJapanese_unicodeScalars 1040 1200 +15.4% 0.87x
CharIndexing_punctuated_unicodeScalars 1120 1240 +10.7% 0.90x
RemoveWhereSwapInts 53 58 +9.4% 0.91x
String.replaceSubrange.Substring.Small 66 72 +9.1% 0.92x
FlattenListLoop 3579 3889 +8.7% 0.92x (?)
ArrayPlusEqualFiveElementCollection 6956 7511 +8.0% 0.93x (?)
String.replaceSubrange.String.Small 51 55 +7.8% 0.93x (?)
MapReduce 334 360 +7.8% 0.93x (?)
CharIteration_punctuated_unicodeScalars_Backwards 1040 1120 +7.7% 0.93x
MapReduceAnyCollection 332 357 +7.5% 0.93x
 
Improvement OLD NEW DELTA RATIO
TypeName 13567 1900 -86.0% 7.14x
DictionaryBridgeToObjC_Access 838 667 -20.4% 1.26x (?)
EqualSubstringSubstring 37 34 -8.1% 1.09x (?)
LessSubstringSubstring 37 34 -8.1% 1.09x
EqualSubstringSubstringGenericEquatable 37 34 -8.1% 1.09x
EqualSubstringString 37 34 -8.1% 1.09x
LessSubstringSubstringGenericComparable 37 34 -8.1% 1.09x
EqualStringSubstring 38 35 -7.9% 1.09x (?)
ObjectiveCBridgeToNSSet 14100 13000 -7.8% 1.08x (?)
ObjectiveCBridgeFromNSSetAnyObjectToString 65500 60500 -7.6% 1.08x (?)
StringComparison_slowerPrenormal 1450 1340 -7.6% 1.08x (?)
Set.subtracting.Empty.Box 14 13 -7.1% 1.08x

Code size: -O

Regression OLD NEW DELTA RATIO
Substring.o 15731 15907 +1.1% 0.99x

Performance: -Osize

Regression OLD NEW DELTA RATIO
AngryPhonebook.Strasse 181 219 +21.0% 0.83x
DropWhileAnySequenceLazy 1906 2231 +17.1% 0.85x (?)
AngryPhonebook.Armenian 206 241 +17.0% 0.85x
AngryPhonebook.Cyrillic 221 258 +16.7% 0.86x
RemoveWhereSwapInts 53 58 +9.4% 0.91x
FlattenListLoop 3561 3888 +9.2% 0.92x (?)
ArrayPlusEqualFiveElementCollection 6956 7511 +8.0% 0.93x (?)
 
Improvement OLD NEW DELTA RATIO
TypeName 13655 1963 -85.6% 6.96x
Breadcrumbs.UTF16ToIdxRange.longASCII 29 25 -13.8% 1.16x
SubstringComparable 15 13 -13.3% 1.15x
PrefixAnySeqCRangeIterLazy 158 142 -10.1% 1.11x (?)
PrefixAnySeqCntRangeLazy 158 142 -10.1% 1.11x (?)
EqualSubstringSubstring 37 34 -8.1% 1.09x
EqualStringSubstring 37 34 -8.1% 1.09x (?)
EqualSubstringSubstringGenericEquatable 37 34 -8.1% 1.09x
EqualSubstringString 37 34 -8.1% 1.09x (?)
LessSubstringSubstringGenericComparable 38 35 -7.9% 1.09x (?)
StringComparison_nonBMPSlowestPrenormal 1510 1400 -7.3% 1.08x
FindString.Rec3.String 214 199 -7.0% 1.08x (?)

Code size: -Osize

Regression OLD NEW DELTA RATIO
RomanNumbers.o 6138 6779 +10.4% 0.91x
CSVParsing.o 50669 52030 +2.7% 0.97x
StringMatch.o 4786 4898 +2.3% 0.98x
FindStringNaive.o 8770 8966 +2.2% 0.98x
Substring.o 15694 15950 +1.6% 0.98x
StringBuilder.o 7434 7531 +1.3% 0.99x
 
Improvement OLD NEW DELTA RATIO
Mirror.o 13192 12370 -6.2% 1.07x

Performance: -Onone

Regression OLD NEW DELTA RATIO
AngryPhonebook.Strasse 181 218 +20.4% 0.83x
AngryPhonebook.Armenian 206 241 +17.0% 0.85x
AngryPhonebook.Cyrillic 222 258 +16.2% 0.86x
StringBuilderWithLongSubstring 2690 2980 +10.8% 0.90x (?)
FindString.Rec3.Substring 729 799 +9.6% 0.91x (?)
StringUTF16SubstringBuilder 16010 17350 +8.4% 0.92x (?)
ObjectiveCBridgeStringHash 111 120 +8.1% 0.93x
RangeReplaceableCollectionPlusDefault 5672 6120 +7.9% 0.93x (?)
 
Improvement OLD NEW DELTA RATIO
TypeName 14527 2377 -83.6% 6.11x
DictionaryBridgeToObjC_Access 989 774 -21.7% 1.28x (?)
StringRemoveDupes 832 681 -18.1% 1.22x
String.data.LargeUnicode 120 105 -12.5% 1.14x (?)
String.data.Medium 115 101 -12.2% 1.14x (?)
ParseFloat.Double.Exp 43 38 -11.6% 1.13x (?)
ObjectiveCBridgeFromNSSetAnyObjectToStringForced 86000 77500 -9.9% 1.11x (?)
FrequenciesUsingReduceInto 4010 3730 -7.0% 1.08x (?)
EqualSubstringSubstringGenericEquatable 43 40 -7.0% 1.07x (?)
LessSubstringSubstringGenericComparable 43 40 -7.0% 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 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

Copy link
Contributor

@beccadax beccadax left a comment

Choose a reason for hiding this comment

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

I'm okay with this change in principle, but I have some questions about the specifics.

@@ -471,7 +471,7 @@ var stringInterp = "\(#^STRING_INTERP_3^#)"
_ = "" + "\(#^STRING_INTERP_4^#)" + ""
// STRING_INTERP: Begin completions
// STRING_INTERP-DAG: Decl[InstanceMethod]/CurrNominal/IsSystem: ['(']{#(value): T#}[')'][#Void#];
// STRING_INTERP-DAG: Decl[Struct]/CurrModule: FooStruct[#FooStruct#];
// STRING_INTERP-DAG: Decl[Struct]/CurrModule/TypeRelation[Convertible]: FooStruct[#FooStruct#]; name=FooStruct
Copy link
Member Author

Choose a reason for hiding this comment

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

@rintaro I tracked down the real difference and I think this is it. Does that make sense, given we've overloaded interpolation to take a metatype?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, it makes sense 👍

@airspeedswift
Copy link
Member Author

@swift-ci please test

@airspeedswift
Copy link
Member Author

@swift-ci please benchmark

@airspeedswift
Copy link
Member Author

@swift-ci please benchmark

@swift-ci
Copy link
Contributor

Performance: -O

Regression OLD NEW DELTA RATIO
EqualSubstringSubstring 21 29 +38.1% 0.72x
LessSubstringSubstring 21 29 +38.1% 0.72x
EqualSubstringSubstringGenericEquatable 21 29 +38.1% 0.72x
EqualSubstringString 21 29 +38.1% 0.72x
LessSubstringSubstringGenericComparable 21 29 +38.1% 0.72x
EqualStringSubstring 21 28 +33.3% 0.75x
AngryPhonebook.Strasse 120 146 +21.7% 0.82x
CharIndexing_punctuatedJapanese_unicodeScalars 760 920 +21.1% 0.83x
AngryPhonebook.Armenian 141 162 +14.9% 0.87x (?)
AngryPhonebook.Cyrillic 152 174 +14.5% 0.87x (?)
StringComparison_longSharedPrefix 337 373 +10.7% 0.90x
FlattenListFlatMap 2372 2615 +10.2% 0.91x (?)
CharIndexing_punctuated_unicodeScalars 840 920 +9.5% 0.91x (?)
DataAppendSequence 5900 6400 +8.5% 0.92x (?)
IterateData 810 876 +8.1% 0.92x (?)
 
Improvement OLD NEW DELTA RATIO
TypeName 9261 1468 -84.1% 6.31x

Code size: -O

Regression OLD NEW DELTA RATIO
Substring.o 17155 17347 +1.1% 0.99x

Performance: -Osize

Regression OLD NEW DELTA RATIO
FlattenListLoop 928 1516 +63.4% 0.61x (?)
LessSubstringSubstring 21 29 +38.1% 0.72x
EqualStringSubstring 21 29 +38.1% 0.72x
LessSubstringSubstringGenericComparable 21 29 +38.1% 0.72x
EqualSubstringSubstring 21 28 +33.3% 0.75x
EqualSubstringSubstringGenericEquatable 21 28 +33.3% 0.75x
EqualSubstringString 21 28 +33.3% 0.75x
Dictionary4 161 203 +26.1% 0.79x
AngryPhonebook.Strasse 120 145 +20.8% 0.83x (?)
AngryPhonebook.Armenian 141 162 +14.9% 0.87x (?)
Dictionary4OfObjects 230 264 +14.8% 0.87x (?)
AngryPhonebook.Cyrillic 152 174 +14.5% 0.87x (?)
StringComparison_longSharedPrefix 337 373 +10.7% 0.90x (?)
IterateData 803 869 +8.2% 0.92x (?)
 
Improvement OLD NEW DELTA RATIO
TypeName 9220 1539 -83.3% 5.99x
CharacterLiteralsLarge 71 63 -11.3% 1.13x (?)
FindString.Rec3.String 180 161 -10.6% 1.12x (?)
FindString.Rec3.Substring 212 194 -8.5% 1.09x (?)
CharacterLiteralsSmall 233 215 -7.7% 1.08x (?)

Code size: -Osize

Regression OLD NEW DELTA RATIO
RomanNumbers.o 6106 6747 +10.5% 0.90x
CSVParsing.o 50493 51710 +2.4% 0.98x
StringMatch.o 4786 4898 +2.3% 0.98x
FindStringNaive.o 8628 8824 +2.3% 0.98x
Substring.o 17315 17571 +1.5% 0.99x
StringBuilder.o 7259 7356 +1.3% 0.99x
 
Improvement OLD NEW DELTA RATIO
Mirror.o 13248 12442 -6.1% 1.06x

Performance: -Onone

Regression OLD NEW DELTA RATIO
EqualSubstringSubstringGenericEquatable 25 32 +28.0% 0.78x (?)
LessSubstringSubstringGenericComparable 25 32 +28.0% 0.78x
EqualSubstringSubstring 26 33 +26.9% 0.79x
LessSubstringSubstring 26 33 +26.9% 0.79x
EqualStringSubstring 26 33 +26.9% 0.79x (?)
EqualSubstringString 26 33 +26.9% 0.79x (?)
AngryPhonebook.Strasse 120 145 +20.8% 0.83x
AngryPhonebook.Armenian 141 162 +14.9% 0.87x
AngryPhonebook.Cyrillic 152 174 +14.5% 0.87x (?)
String.replaceSubrange.String.Small 38 42 +10.5% 0.90x (?)
StringWalk 3040 3320 +9.2% 0.92x (?)
 
Improvement OLD NEW DELTA RATIO
TypeName 9811 1826 -81.4% 5.37x

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

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 7b5a4b9

@airspeedswift
Copy link
Member Author

@swift-ci please smoke test Linux platform

@airspeedswift airspeedswift merged commit 87df996 into swiftlang:master Jun 26, 2020
kylemacomber pushed a commit to kylemacomber/swift that referenced this pull request Jul 17, 2020
airspeedswift added a commit that referenced this pull request Jul 17, 2020
* Add fast string interpolation for metatypes (#32113)

* Whitespace fixes

Co-authored-by: Ben Cohen <[email protected]>
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.

4 participants