Skip to content

[Stdlib performance] Make integer conversion operations transparent #79707

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
Mar 17, 2025

Conversation

DougGregor
Copy link
Member

The integer conversion operations were inlinable, but aren't getting inlined in debug builds, which results in unreasonably poor performance. Mark them as transparent so we don't end up with unspecialized generic code in the hot path.

Fixes #78501

@DougGregor DougGregor requested review from rjmccall and a team as code owners February 28, 2025 21:49
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please benchmark Apple Silicon

@Catfish-Man
Copy link
Contributor

@swift-ci please Apple Silicon benchmark

@DougGregor
Copy link
Member Author

grrr, IR differences

@DougGregor DougGregor force-pushed the transparent-integer-conversions branch from 63678ab to 6282cf8 Compare February 28, 2025 23:00
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please Apple Silicon benchmark

@DougGregor
Copy link
Member Author

Performance (arm64): -O

Regression OLD NEW DELTA RATIO
FindString.Rec3.Array 52.702 70.286 +33.4% 0.75x
StringFromLongWholeSubstring 1.856 2.373 +27.8% 0.78x
CharacterRecognizer.ascii 54.326 65.432 +20.4% 0.83x
StringFromLongWholeSubstringGeneric 3.144 3.664 +16.5% 0.86x
ConvertFloatingPoint.MockFloat64ToInt64 301.125 348.714 +15.8% 0.86x
Chars2 1916.964 2116.667 +10.4% 0.91x
String.replaceSubrange.ArrChar 18.337 19.907 +8.6% 0.92x
 
Improvement OLD NEW DELTA RATIO
StringWordBuilder 871.5 623.125 -28.5% 1.40x
UTF8Decode_InitFromCustom_contiguous 130.118 96.217 -26.1% 1.35x
UTF8Decode_InitDecoding 130.938 96.85 -26.0% 1.35x
UTF8Decode_InitFromCustom_noncontiguous 145.813 111.75 -23.4% 1.30x
CxxStringConversion.cxx.to.swift 62.0 49.533 -20.1% 1.25x (?)
Join 58.6 51.0 -13.0% 1.15x
StringComparison_fastPrenormal 261.739 242.3 -7.4% 1.08x (?)
StringInterpolationManySmallSegments 3541.379 3308.333 -6.6% 1.07x (?)

Code size: -O

Improvement OLD NEW DELTA RATIO
MonteCarloE.o 1805 1769 -2.0% 1.02x
BufferFill.o 6609 6533 -1.1% 1.01x

Performance (arm64): -Osize

Regression OLD NEW DELTA RATIO
FindString.Rec3.Array 51.383 74.212 +44.4% 0.69x
CharacterRecognizer.ascii 51.452 63.371 +23.2% 0.81x
ConvertFloatingPoint.MockFloat64ToInt64 300.0 348.0 +16.0% 0.86x
CxxStringConversion.cxx.to.swift 52.273 60.25 +15.3% 0.87x (?)
String.replaceSubrange.ArrChar 18.0 19.988 +11.0% 0.90x (?)
 
Improvement OLD NEW DELTA RATIO
UTF8Decode_InitDecoding 130.706 95.364 -27.0% 1.37x
UTF8Decode_InitFromCustom_contiguous 130.235 95.5 -26.7% 1.36x
UTF8Decode_InitFromCustom_noncontiguous 197.083 163.417 -17.1% 1.21x
Join 53.731 49.6 -7.7% 1.08x (?)
Breadcrumbs.MutatedIdxToUTF16.ASCII 1.29 1.192 -7.6% 1.08x (?)
StringComparison_fastPrenormal 310.125 286.875 -7.5% 1.08x
DataAppendDataMediumToMedium 1907.0 1766.154 -7.4% 1.08x (?)
ObjectiveCBridgeStubFromNSDate 5200.0 4817.5 -7.4% 1.08x (?)

Code size: -Osize

Improvement OLD NEW DELTA RATIO
MonteCarloE.o 2041 2009 -1.6% 1.02x
BufferFill.o 5625 5549 -1.4% 1.01x

Performance (arm64): -Onone

Regression OLD NEW DELTA RATIO
DataCountMedium 42.793 53.486 +25.0% 0.80x (?)
Set.filter.Int100.24k 57.233 63.769 +11.4% 0.90x
Set.filter.Int100.20k 48.096 53.489 +11.2% 0.90x
Set.filter.Int100.16k 38.966 43.216 +10.9% 0.90x
String.replaceSubrange.ArrChar 18.425 20.418 +10.8% 0.90x
Set.filter.Int100.28k 68.667 76.031 +10.7% 0.90x
CharacterRecognizer.ascii 99.391 106.952 +7.6% 0.93x (?)
 
Improvement OLD NEW DELTA RATIO
RangeIterationSigned 3068.0 371.0 -87.9% 8.27x
CreateObjects 285.375 42.792 -85.0% 6.67x
MonteCarloPi 924250.0 165250.0 -82.1% 5.59x
ArraySubscript 18776.0 3518.0 -81.3% 5.34x
MonteCarloE 214960.0 44420.0 -79.3% 4.84x
RC4 4065.0 1098.5 -73.0% 3.70x
ByteSwap 318.857 106.273 -66.7% 3.00x
NSStringConversion.InlineBuffer.ASCII 2356.0 888.5 -62.3% 2.65x
PolymorphicCalls 1269.0 546.75 -56.9% 2.32x
NSStringConversion.InlineBuffer.UTF8 1555.0 813.5 -47.7% 1.91x
FloatingPointPrinting_Float_description_uniform 4869.048 2975.862 -38.9% 1.64x
ArrayAppendGenericStructs 1167.0 753.0 -35.5% 1.55x (?)
CharIteration_utf16_unicodeScalars 37260.0 26773.333 -28.1% 1.39x
FloatingPointPrinting_Double_description_uniform 8604.0 6390.625 -25.7% 1.35x
UTF8Decode_InitFromCustom_contiguous 134.75 100.381 -25.5% 1.34x
UTF8Decode_InitDecoding 134.143 100.0 -25.5% 1.34x
CharIteration_utf16_unicodeScalars_Backwards 60240.0 46840.0 -22.2% 1.29x
CharIteration_ascii_unicodeScalars 45440.0 36600.0 -19.5% 1.24x
CharIteration_russian_unicodeScalars 38940.0 31586.667 -18.9% 1.23x
CharIteration_korean_unicodeScalars 45760.0 37260.0 -18.6% 1.23x
CxxStringConversion.cxx.to.swift 75.5 61.625 -18.4% 1.23x (?)
CharIteration_tweet_unicodeScalars 89560.0 73280.0 -18.2% 1.22x
CharIteration_japanese_unicodeScalars 55360.0 45320.0 -18.1% 1.22x (?)
CharIteration_punctuatedJapanese_unicodeScalars 8069.091 6642.857 -17.7% 1.21x
CharIteration_punctuated_unicodeScalars 10400.0 8603.636 -17.3% 1.21x
CharIteration_chinese_unicodeScalars 35180.0 29200.0 -17.0% 1.20x
CharIteration_japanese_unicodeScalars_Backwards 86680.0 73200.0 -15.6% 1.18x
FloatingPointPrinting_Double_interpolated 17416.0 14858.065 -14.7% 1.17x
HashTest 3457.5 2950.0 -14.7% 1.17x
CharIteration_ascii_unicodeScalars_Backwards 70040.0 59840.0 -14.6% 1.17x
MapReduceString 490.0 420.6 -14.2% 1.17x
CharIndexing_utf16_unicodeScalars 84040.0 72200.0 -14.1% 1.16x
CharIteration_russian_unicodeScalars_Backwards 58720.0 50600.0 -13.8% 1.16x
CharIteration_tweet_unicodeScalars_Backwards 137400.0 118560.0 -13.7% 1.16x
CharIteration_korean_unicodeScalars_Backwards 69680.0 60240.0 -13.5% 1.16x
FloatingPointPrinting_Float_interpolated 16164.286 13987.097 -13.5% 1.16x
CharIteration_punctuatedJapanese_unicodeScalars_Backwards 12145.0 10520.0 -13.4% 1.15x
CharIndexing_utf16_unicodeScalars_Backwards 84800.0 74040.0 -12.7% 1.15x (?)
CharIteration_chinese_unicodeScalars_Backwards 53480.0 46740.0 -12.6% 1.14x
CharIndexing_russian_unicodeScalars_Backwards 86960.0 76480.0 -12.1% 1.14x
DataCreateMedium 94250.0 83600.0 -11.3% 1.13x
CharIndexing_punctuatedJapanese_unicodeScalars_Backwards 18136.0 16176.0 -10.8% 1.12x
CharIndexing_korean_unicodeScalars_Backwards 102240.0 91560.0 -10.4% 1.12x
CharIndexing_chinese_unicodeScalars_Backwards 78920.0 70680.0 -10.4% 1.12x (?)
CharIndexing_japanese_unicodeScalars_Backwards 125040.0 112000.0 -10.4% 1.12x (?)
CharIndexing_ascii_unicodeScalars_Backwards 103120.0 92520.0 -10.3% 1.11x (?)
CharIndexing_punctuatedJapanese_unicodeScalars 17592.0 15800.0 -10.2% 1.11x
CharIteration_punctuated_unicodeScalars_Backwards 15640.0 14060.0 -10.1% 1.11x
CharIndexing_tweet_unicodeScalars_Backwards 202880.0 182400.0 -10.1% 1.11x (?)
CharIndexing_punctuated_unicodeScalars_Backwards 22820.0 20570.0 -9.9% 1.11x
MapReduceShortString 45.516 41.147 -9.6% 1.11x
CharIndexing_korean_unicodeScalars 97440.0 88560.0 -9.1% 1.10x (?)
CharIndexing_ascii_unicodeScalars 100680.0 91520.0 -9.1% 1.10x (?)
CharIndexing_russian_unicodeScalars 83440.0 75880.0 -9.1% 1.10x (?)
StringInterpolationSmall 1110.5 1010.0 -9.0% 1.10x (?)
CharIndexing_chinese_unicodeScalars 76080.0 69400.0 -8.8% 1.10x (?)
CharIndexing_punctuated_unicodeScalars 22040.0 20120.0 -8.7% 1.10x (?)
CharIndexing_tweet_unicodeScalars 196720.0 180640.0 -8.2% 1.09x (?)
CharIndexing_japanese_unicodeScalars 119680.0 110280.0 -7.9% 1.09x (?)
DataCreateSmall 12400.0 11455.0 -7.6% 1.08x
StringInterpolation 2533.898 2347.945 -7.3% 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: Mac14,12
  Total Number of Cores: 12 (8 performance and 4 efficiency)
  Memory: 32 GB

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please smoke test macOS

ravikandhadai and others added 3 commits March 12, 2025 07:40
OSLogOptimization pass

The OSLogOptimization pass constant evaluates and folds SIL instructions
that are inlined from the construction of the string interpolations passed
to the log calls, which enables replacing the dynamic format string construction
with a static format string. In addition to folding constant strings, it also folds
constant integers and arrays whose elements are constants. This change makes it
skip folding static strings, since they are already efficiently represented.

rdar://146028438
The integer conversion operations were inlinable, but aren't getting
inlined in debug builds, which results in unreasonably poor
performance. Mark them as transparent so we don't end up with
unspecialized generic code in the hot path.

Fixes swiftlang#78501
@DougGregor DougGregor force-pushed the transparent-integer-conversions branch from a17cce1 to c2417e0 Compare March 12, 2025 14:41
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

Pulling in #79941 to test with it

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor DougGregor merged commit 9c2c4ea into swiftlang:main Mar 17, 2025
5 of 7 checks passed
@DougGregor DougGregor deleted the transparent-integer-conversions branch March 17, 2025 23:37
meg-gupta added a commit to meg-gupta/swift that referenced this pull request Apr 11, 2025
…t-integer-conversions"

This reverts commit 9c2c4ea, reversing
changes made to 829e03c.
@meg-gupta meg-gupta mentioned this pull request Apr 11, 2025
meg-gupta added a commit to meg-gupta/swift that referenced this pull request Apr 11, 2025
Revert "Merge pull request swiftlang#79707 from DougGregor/transparent-integer-conversions"

This reverts commit 9c2c4ea, reversing
changes made to 829e03c.
@meg-gupta meg-gupta mentioned this pull request Apr 11, 2025
shahmishal added a commit that referenced this pull request Apr 15, 2025
meg-gupta added a commit that referenced this pull request Apr 15, 2025
meg-gupta added a commit to meg-gupta/swift that referenced this pull request Apr 19, 2025
Revert "Merge pull request swiftlang#80767 from meg-gupta/reverttransparent"

This reverts commit 198a802, reversing
changes made to 8eb43af.
@meg-gupta meg-gupta mentioned this pull request Apr 19, 2025
meg-gupta added a commit that referenced this pull request Apr 21, 2025
meg-gupta added a commit to meg-gupta/swift that referenced this pull request Apr 21, 2025
Revert "Merge pull request swiftlang#80767 from meg-gupta/reverttransparent"

This reverts commit 198a802, reversing
changes made to 8eb43af.
@meg-gupta meg-gupta mentioned this pull request Apr 21, 2025
meg-gupta added a commit that referenced this pull request Apr 23, 2025
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.

Integer conversions cause witness table lookups.
4 participants