Skip to content

Conversation

@lorentey
Copy link
Member

This is a spinoff of #34961 that reintroduces a _precondition for count >= 0; that particular static invariant may have a beneficial effect on overall performance.

karwa and others added 15 commits December 4, 2020 02:06
Every other Unsafe(Mutable)BufferPointer precondition in this file is debug-mode only.

This information is already part of the documentation for this function, and it's really hard to get rid of these branches and traps otherwise.
…ckedBounds:)

Unchecked APIs must still perform checks in debug builds to ensure that invariants aren’t violated.
(I.e., these aren’t a license to perform invalid operations — they just let us get rid of the checks when we know for sure they are unnecessary.)
`Slice` does not technically guarantee that its indices are valid in its base, so these initializers accidentally allowed the creation of obviously out-of-bounds buffers.
This eliminates a couple of _debugPreconditions which seem to change inlining enough to interfere with some benchmarks.
When the stdlib says not to check, it’s a good idea to actually not have any checking, so that we leave existing code paths unchanged. (And because we do trust that the stdlib is responsible about such things.)
…nversions

The UnboundRange → Range conversion path is complicated and it involves at least one unnecessary _precondition check for startIndex ..< endIndex.
@lorentey
Copy link
Member Author

@swift-ci benchmark

@swift-ci
Copy link
Contributor

Performance: -O

Regression OLD NEW DELTA RATIO
ObjectiveCBridgeStubDataAppend 2360 3020 +28.0% 0.78x
ObjectiveCBridgeStringHash 99 118 +19.2% 0.84x
StringHasPrefixAscii 1760 2070 +17.6% 0.85x
Data.append.Sequence.64kB.Count0.RE 249 288 +15.7% 0.86x
Data.append.Sequence.64kB.Count0.RE.I 249 285 +14.5% 0.87x
Data.init.Sequence.64kB.Count0.RE 249 285 +14.5% 0.87x
Data.append.Sequence.809B.Count0.RE 348 398 +14.4% 0.87x (?)
Data.append.Sequence.64kB.Count0.I 248 283 +14.1% 0.88x
Data.init.Sequence.64kB.Count0.RE.I 250 285 +14.0% 0.88x
Data.append.Sequence.809B.Count0.RE.I 350 397 +13.4% 0.88x
Data.init.Sequence.64kB.Count0 248 281 +13.3% 0.88x
Data.append.Sequence.64kB.Count0 249 282 +13.3% 0.88x
StringHasSuffixAscii 1970 2230 +13.2% 0.88x
Data.append.Sequence.809B.Count0 345 390 +13.0% 0.88x (?)
Data.init.Sequence.64kB.Count0.I 249 281 +12.9% 0.89x
SubstringEqualString 365 411 +12.6% 0.89x
Data.append.Sequence.809B.Count0.I 345 388 +12.5% 0.89x (?)
DataToStringLargeUnicode 6250 7000 +12.0% 0.89x (?)
Data.init.Sequence.2049B.Count0.I 453 507 +11.9% 0.89x
Data.init.Sequence.2047B.Count0.I 453 506 +11.7% 0.90x (?)
ObjectiveCBridgeStringGetASCIIContents 396 440 +11.1% 0.90x (?)
String.data.LargeUnicode 119 132 +10.9% 0.90x (?)
Data.init.Sequence.809B.Count0.RE 406 449 +10.6% 0.90x (?)
Data.init.Sequence.809B.Count0.RE.I 406 448 +10.3% 0.91x (?)
InsertCharacterEndIndex 147 162 +10.2% 0.91x (?)
Data.init.Sequence.809B.Count0.I 407 447 +9.8% 0.91x (?)
ObjectiveCBridgeStubFromNSDateRef 4030 4420 +9.7% 0.91x (?)
Breadcrumbs.IdxToUTF16Range.longASCII 31 34 +9.7% 0.91x (?)
ObjectiveCBridgeStubFromNSDate 4550 4980 +9.5% 0.91x (?)
Data.init.Sequence.511B.Count0.I 424 464 +9.4% 0.91x (?)
Data.init.Sequence.513B.Count0.I 424 464 +9.4% 0.91x (?)
StringToDataLargeUnicode 3200 3500 +9.4% 0.91x (?)
Data.init.Sequence.809B.Count0 407 445 +9.3% 0.91x (?)
DataSubscriptMedium 56 61 +8.9% 0.92x
ObjectiveCBridgeStringCompare2 896 968 +8.0% 0.93x (?)
SubstringEquatable 617 666 +7.9% 0.93x (?)
Breadcrumbs.UTF16ToIdxRange.longASCII 26 28 +7.7% 0.93x
InsertCharacterEndIndexNonASCII 53 57 +7.5% 0.93x (?)
 
Improvement OLD NEW DELTA RATIO
ArrayAppendLatin1Substring 24048 15588 -35.2% 1.54x
ArrayAppendAsciiSubstring 23544 15264 -35.2% 1.54x
ArrayAppendUTF16Substring 23544 15300 -35.0% 1.54x
Data.init.Sequence.64kB.Count.I 52 35 -32.7% 1.49x
Data.init.Sequence.64kB.Count 52 35 -32.7% 1.49x
Data.init.Sequence.2047B.Count.I 91 65 -28.6% 1.40x
Data.init.Sequence.2049B.Count.I 91 65 -28.6% 1.40x
Data.init.Sequence.809B.Count 86 65 -24.4% 1.32x
Data.init.Sequence.809B.Count.I 86 65 -24.4% 1.32x
Data.init.Sequence.511B.Count.I 99 80 -19.2% 1.24x (?)
FlattenListLoop 1771 1449 -18.2% 1.22x (?)
Data.init.Sequence.513B.Count.I 100 82 -18.0% 1.22x (?)
FlattenListFlatMap 5207 4351 -16.4% 1.20x (?)
DataCreateSmallArray 2900 2500 -13.8% 1.16x
DataAccessBytesMedium 74 64 -13.5% 1.16x (?)
Data.hash.Empty 69 61 -11.6% 1.13x
UTF8Decode_InitFromData_ascii_as_ascii 652 600 -8.0% 1.09x (?)
RandomShuffleLCG2 432 400 -7.4% 1.08x (?)
Array2D 6464 6000 -7.2% 1.08x

Code size: -O

Regression OLD NEW DELTA RATIO
ArrayAppend.o 23637 24101 +2.0% 0.98x
CSVParsing.o 54339 55043 +1.3% 0.99x
 
Improvement OLD NEW DELTA RATIO
ObjectiveCBridgingStubs.o 16165 14181 -12.3% 1.14x

Performance: -Osize

Regression OLD NEW DELTA RATIO
ObjectiveCBridgeStubDataAppend 2380 3000 +26.1% 0.79x
DataCopyBytesSmall 84 105 +25.0% 0.80x
String.data.Medium 104 126 +21.2% 0.83x (?)
StringHasPrefixAscii 1660 2000 +20.5% 0.83x
ObjectiveCBridgeStringHash 99 118 +19.2% 0.84x
StringHasSuffixAscii 1860 2150 +15.6% 0.87x
Data.append.Sequence.64kB.Count0.I 268 306 +14.2% 0.88x
Data.append.Sequence.64kB.Count0 269 307 +14.1% 0.88x
Data.init.Sequence.64kB.Count0.RE 265 300 +13.2% 0.88x
Breadcrumbs.IdxToUTF16Range.longASCII 31 35 +12.9% 0.89x (?)
Data.append.Sequence.809B.Count0.I 373 421 +12.9% 0.89x
Data.init.Sequence.64kB.Count0.RE.I 265 299 +12.8% 0.89x
Data.append.Sequence.809B.Count0 373 420 +12.6% 0.89x
CharacterLiteralsLarge 89 100 +12.4% 0.89x
InsertCharacterEndIndex 147 165 +12.2% 0.89x
Data.append.Sequence.64kB.Count0.RE.I 263 295 +12.2% 0.89x
Data.init.Sequence.64kB.Count0 269 299 +11.2% 0.90x
ObjectiveCBridgeStringGetASCIIContents 397 441 +11.1% 0.90x (?)
Data.init.Sequence.64kB.Count0.I 270 299 +10.7% 0.90x
SubstringEqualString 378 418 +10.6% 0.90x (?)
Data.append.Sequence.809B.Count0.RE.I 369 408 +10.6% 0.90x
Data.init.Sequence.2049B.Count0.I 486 537 +10.5% 0.91x (?)
Data.init.Sequence.2047B.Count0.I 486 535 +10.1% 0.91x
Data.init.Sequence.809B.Count0.RE 430 473 +10.0% 0.91x (?)
Data.init.Sequence.809B.Count0.RE.I 430 473 +10.0% 0.91x (?)
ObjectiveCBridgeStringCStringUsingEncoding 674 740 +9.8% 0.91x (?)
Data.init.Sequence.809B.Count0 435 476 +9.4% 0.91x (?)
Data.init.Sequence.809B.Count0.I 435 476 +9.4% 0.91x (?)
InsertCharacterEndIndexNonASCII 54 59 +9.3% 0.92x (?)
String.replaceSubrange.String.Small 55 60 +9.1% 0.92x
Breadcrumbs.UTF16ToIdxRange.longASCII 26 28 +7.7% 0.93x (?)
Data.init.Sequence.511B.Count0.I 456 491 +7.7% 0.93x (?)
Data.init.Sequence.513B.Count0.I 457 492 +7.7% 0.93x (?)
RomanNumbers2 536 577 +7.6% 0.93x (?)
 
Improvement OLD NEW DELTA RATIO
CharIteration_tweet_unicodeScalars 6360 4480 -29.6% 1.42x
CharIteration_ascii_unicodeScalars 3040 2320 -23.7% 1.31x
DataAccessBytesMedium 92 72 -21.7% 1.28x
DataCreateSmallArray 3250 2700 -16.9% 1.20x
Array2D 7280 6224 -14.5% 1.17x
RemoveWhereFilterInts 44 39 -11.4% 1.13x
RandomShuffleLCG2 432 384 -11.1% 1.12x
ArraySetElement 462 411 -11.0% 1.12x
CharIteration_punctuated_unicodeScalars 760 680 -10.5% 1.12x (?)
ObjectiveCBridgeStubDateMutation 256 230 -10.2% 1.11x (?)
CharIteration_punctuatedJapanese_unicodeScalars 800 720 -10.0% 1.11x
PrefixWhileSequence 365 333 -8.8% 1.10x
CharIteration_chinese_unicodeScalars 3240 3000 -7.4% 1.08x (?)
CharIteration_russian_unicodeScalars 3840 3560 -7.3% 1.08x
Data.hash.Empty 69 64 -7.2% 1.08x

Code size: -Osize

Regression OLD NEW DELTA RATIO
CSVParsing.o 51591 52147 +1.1% 0.99x
 
Improvement OLD NEW DELTA RATIO
ObjectiveCBridgingStubs.o 15254 13872 -9.1% 1.10x
RemoveWhere.o 14270 13425 -5.9% 1.06x
StringMatch.o 4140 4027 -2.7% 1.03x
Combos.o 6287 6128 -2.5% 1.03x

Performance: -Onone

Regression OLD NEW DELTA RATIO
DataCopyBytesSmall 1455 2690 +84.9% 0.54x
DataCopyBytesMedium 1568 2806 +79.0% 0.56x
ArrayValueProp3 5965 8223 +37.9% 0.73x (?)
ArrayValueProp4 5951 7986 +34.2% 0.75x (?)
StackPromo 51800 65600 +26.6% 0.79x (?)
PopFrontArrayGeneric 5000 6200 +24.0% 0.81x (?)
StringToDataSmall 3600 4400 +22.2% 0.82x (?)
StringToDataEmpty 3450 4200 +21.7% 0.82x (?)
String.data.Empty 83 101 +21.7% 0.82x (?)
RandomIntegersDef 57700 70000 +21.3% 0.82x (?)
String.data.Small 87 105 +20.7% 0.83x (?)
DevirtualizeProtocolComposition 6858 8271 +20.6% 0.83x
ObjectiveCBridgeStringHash 99 118 +19.2% 0.84x
Walsh 7282 8631 +18.5% 0.84x
ObjectiveCBridgeStubFromNSDateRef 4800 5680 +18.3% 0.85x (?)
ObjectiveCBridgeStubDataAppend 3900 4580 +17.4% 0.85x
StringBuilderLong 2290 2660 +16.2% 0.86x (?)
ArrayInitFromSlice 484 555 +14.7% 0.87x (?)
ObjectiveCBridgeStringGetASCIIContents 397 441 +11.1% 0.90x
Sim2DArray 12462 13786 +10.6% 0.90x
String.replaceSubrange.String.Small 60 66 +10.0% 0.91x (?)
ObjectiveCBridgeStringCStringUsingEncoding 671 737 +9.8% 0.91x (?)
Data.init.Sequence.809B.Count.RE 29191 32021 +9.7% 0.91x (?)
Radix2CooleyTukeyf 79824 87504 +9.6% 0.91x (?)
RomanNumbers2 7767 8513 +9.6% 0.91x (?)
Data.append.Sequence.64kB.Count0.RE.I 23668 25904 +9.4% 0.91x (?)
Data.init.Sequence.64kB.Count0.RE.I 23678 25913 +9.4% 0.91x (?)
Data.append.Sequence.64kB.Count.RE 23702 25939 +9.4% 0.91x (?)
Data.append.Sequence.64kB.Count.RE.I 23797 26025 +9.4% 0.91x (?)
Data.init.Sequence.64kB.Count.RE 23824 26030 +9.3% 0.92x (?)
DataCreateSmall 72610 79320 +9.2% 0.92x (?)
Data.append.Sequence.809B.Count0.RE 29581 32191 +8.8% 0.92x (?)
NSStringConversion.Mutable 3276 3565 +8.8% 0.92x (?)
Data.init.Sequence.809B.Count.RE.I 29557 32094 +8.6% 0.92x (?)
Data.append.Sequence.809B.Count0.RE.I 29560 32085 +8.5% 0.92x (?)
Data.init.Sequence.64kB.Count0.RE 23848 25877 +8.5% 0.92x (?)
Data.append.Sequence.64kB.Count0.RE 23810 25832 +8.5% 0.92x (?)
Data.append.Sequence.809B.Count.RE.I 29820 32335 +8.4% 0.92x (?)
Data.init.Sequence.64kB.Count.RE.I 23783 25742 +8.2% 0.92x (?)
Data.init.Sequence.809B.Count0.RE.I 29752 32177 +8.2% 0.92x (?)
Data.append.Sequence.809B.Count.RE 29895 32288 +8.0% 0.93x (?)
Data.init.Sequence.809B.Count0.RE 29850 32165 +7.8% 0.93x (?)
DataAppendSequence 2978300 3203100 +7.5% 0.93x (?)
 
Improvement OLD NEW DELTA RATIO
SevenBoom 1928 1658 -14.0% 1.16x (?)
DataCreateSmallArray 36750 33250 -9.5% 1.11x (?)
ArrayAppend 6860 6290 -8.3% 1.09x (?)
Integrate 3651 3385 -7.3% 1.08x (?)
ArrayAppendReserved 6570 6120 -6.8% 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

@lorentey
Copy link
Member Author

Nah, this is slightly worse than the original.

@lorentey lorentey closed this Dec 12, 2020
@lorentey lorentey deleted the buffers-need-to-be-fast-but-not-too-fast2 branch December 12, 2020 03:13
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