Skip to content

[stdlib] Resolve unsafeBitCast warnings in Runtime.swift.gyb. #7116

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

Conversation

jrose-apple
Copy link
Contributor

No intended functionality change.

@jrose-apple jrose-apple requested a review from moiseev January 28, 2017 01:44
@jrose-apple
Copy link
Contributor Author

@swift-ci Please benchmark

@moiseev moiseev requested a review from atrick January 28, 2017 02:01
Copy link
Contributor

@moiseev moiseev left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks, @jrose-apple !

Copy link
Contributor

@atrick atrick left a comment

Choose a reason for hiding this comment

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

It's a good cleanup. I'm concerned about adding withMemoryRebound closures to atomic operations. It's not the sort of place we want heap allocation. In the atomicFetch cast, just use assumingMemoryBound(to:). Int is an Int64. I guess we can ignore the performance of the generic entry point unless the benchmark results look bad.

return _stdlib_atomicCompareExchangeStrongPtr(
object: target,
expected: expected,
desired: UnsafeRawPointer(desired))
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure why desired was originally bitcast, nor why it needs a seemingly redundant initializer call here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Purely style. I don't like relying on pointer-to-pointer conversions in pure Swift code.

@atrick
Copy link
Contributor

atrick commented Jan 28, 2017

Mandatory inlining should actually get rid of the closure. But still, assumingMemoryBound(to:) is perfectly fine for UnsafePointer -> UnsafePointer. And in general it's fine if the pointer is only used inside a C function.

@jrose-apple
Copy link
Contributor Author

There's no assumingMemoryBound on the typed pointers, which makes it a little more annoying, but I can switch to that if you prefer.

@swift-ci
Copy link
Contributor

Build comment file:

Optimized (O)

Regression (1)
TEST OLD_MIN NEW_MIN DELTA (%) SPEEDUP
Calculator 31 33 +6.5% 0.94x
Improvement (0)
No Changes (158)
TEST OLD_MIN NEW_MIN DELTA (%) SPEEDUP
SuperChars 217815 211929 -2.7% 1.03x(?)
StringHasPrefixUnicode 13996 13651 -2.5% 1.03x
ObjectiveCBridgeStubToNSDate 15492 15247 -1.6% 1.02x(?)
SetExclusiveOr_OfObjects 7420 7266 -2.1% 1.02x(?)
SetUnion_OfObjects 6217 6091 -2.0% 1.02x(?)
ObjectiveCBridgeFromNSSetAnyObjectToString 100629 99005 -1.6% 1.02x(?)
SortStrings 1633 1612 -1.3% 1.01x
ObjectiveCBridgeFromNSDictionaryAnyObjectForced 5329 5268 -1.1% 1.01x(?)
ReversedDictionary 94 93 -1.1% 1.01x(?)
ObjectiveCBridgeFromNSDictionaryAnyObject 147514 146640 -0.6% 1.01x(?)
ArrayAppendOptionals 1233 1224 -0.7% 1.01x(?)
StaticArray 133 132 -0.8% 1.01x(?)
SortSortedStrings 793 786 -0.9% 1.01x
StringHasSuffixUnicode 60576 60246 -0.5% 1.01x
DictionarySwapOfObjects 7168 7115 -0.7% 1.01x(?)
ObjectiveCBridgeStubToNSStringRef 132 131 -0.8% 1.01x(?)
SortStringsUnicode 7184 7096 -1.2% 1.01x(?)
ObjectiveCBridgeStubToArrayOfNSString 31432 31096 -1.1% 1.01x(?)
Phonebook 7082 7007 -1.1% 1.01x(?)
ReversedBidirectional 49680 49326 -0.7% 1.01x(?)
ArrayLiteral 1382 1375 -0.5% 1.01x(?)
RGBHistogram 2263 2245 -0.8% 1.01x(?)
ArraySubscript 1439 1438 -0.1% 1.00x(?)
ObjectiveCBridgeToNSString 1274 1274 +0.0% 1.00x
MonteCarloPi 44859 44849 -0.0% 1.00x(?)
StackPromo 21692 21626 -0.3% 1.00x(?)
PopFrontArray 1098 1097 -0.1% 1.00x(?)
RecursiveOwnedParameter 1929 1929 +0.0% 1.00x
ObjectiveCBridgeStubToNSString 1494 1496 +0.1% 1.00x(?)
ObjectiveCBridgeFromNSArrayAnyObjectToString 85271 85415 +0.2% 1.00x(?)
ClassArrayGetter 13 13 +0.0% 1.00x
Array2D 1910 1915 +0.3% 1.00x(?)
Histogram 288 289 +0.3% 1.00x(?)
ObjectiveCBridgeStubFromNSDateRef 3703 3703 +0.0% 1.00x
ArrayOfGenericPOD 219 219 +0.0% 1.00x
StringWithCString 154631 154620 -0.0% 1.00x(?)
ObjectiveCBridgeFromNSStringForced 2307 2307 +0.0% 1.00x
Prims 754 755 +0.1% 1.00x(?)
SortLettersInPlace 1056 1057 +0.1% 1.00x(?)
DictionarySwap 411 411 +0.0% 1.00x
ArrayAppendToFromGeneric 597 597 +0.0% 1.00x
Dictionary3OfObjects 920 920 +0.0% 1.00x
RangeAssignment 293 292 -0.3% 1.00x(?)
StrComplexWalk 2978 2983 +0.2% 1.00x(?)
ByteSwap 0 0 +0.0% 1.00x
ArrayAppendGenericStructs 1225 1219 -0.5% 1.00x(?)
ArrayAppendLazyMap 913 913 +0.0% 1.00x
ArrayPlusEqualFiveElementCollection 59002 59031 +0.1% 1.00x(?)
XorLoop 352 352 +0.0% 1.00x
StringInterpolation 11117 11100 -0.1% 1.00x(?)
ObserverClosure 2121 2122 +0.1% 1.00x(?)
CharacterLiteralsSmall 812 812 +0.0% 1.00x
Integrate 237 237 +0.0% 1.00x
ArrayPlusEqualSingleElementCollection 56329 56180 -0.3% 1.00x(?)
ArrayAppendStrings 12084 12104 +0.2% 1.00x(?)
Join 496 496 +0.0% 1.00x
ObjectiveCBridgeStubFromNSStringRef 199 199 +0.0% 1.00x
ProtocolDispatch 3030 3030 +0.0% 1.00x
ObjectAllocation 174 174 +0.0% 1.00x
TypeFlood 0 0 +0.0% 1.00x
AngryPhonebook 3229 3225 -0.1% 1.00x(?)
Walsh 324 323 -0.3% 1.00x(?)
Dictionary3 531 531 +0.0% 1.00x
Dictionary2 2093 2096 +0.1% 1.00x(?)
SetIntersect_OfObjects 1455 1457 +0.1% 1.00x(?)
ErrorHandling 3152 3166 +0.4% 1.00x(?)
ArrayOfRef 3851 3857 +0.2% 1.00x(?)
ObserverUnappliedMethod 2550 2556 +0.2% 1.00x(?)
ArrayPlusEqualArrayOfInt 597 597 +0.0% 1.00x
ObjectiveCBridgeToNSArray 32137 32152 +0.1% 1.00x(?)
NSError 350 351 +0.3% 1.00x(?)
DictionaryOfObjects 2465 2464 -0.0% 1.00x(?)
ObjectiveCBridgeStubToNSDateRef 3393 3379 -0.4% 1.00x(?)
PopFrontArrayGeneric 1103 1103 +0.0% 1.00x
PopFrontUnsafePointer 8951 8952 +0.0% 1.00x(?)
PolymorphicCalls 21 21 +0.0% 1.00x
ArrayAppendReserved 739 739 +0.0% 1.00x
ArrayAppendFromGeneric 596 597 +0.2% 1.00x(?)
MapReduce 341 341 +0.0% 1.00x
ObjectiveCBridgeStubDateMutation 272 272 +0.0% 1.00x
IterateData 2516 2515 -0.0% 1.00x(?)
DictionaryLiteral 1464 1464 +0.0% 1.00x
Hanoi 3176 3166 -0.3% 1.00x(?)
OpenClose 54 54 +0.0% 1.00x
DictionaryRemoveOfObjects 21988 22006 +0.1% 1.00x(?)
UTF8Decode 282 281 -0.3% 1.00x(?)
SetIsSubsetOf 242 242 +0.0% 1.00x
Dictionary 748 749 +0.1% 1.00x(?)
NopDeinit 22677 22690 +0.1% 1.00x(?)
ObjectiveCBridgeFromNSArrayAnyObject 66329 66647 +0.5% 1.00x(?)
SetIntersect 349 348 -0.3% 1.00x(?)
ObjectiveCBridgeStubDataAppend 3462 3456 -0.2% 1.00x(?)
SetExclusiveOr 2861 2872 +0.4% 1.00x(?)
RGBHistogramOfObjects 22146 22092 -0.2% 1.00x(?)
ObjectiveCBridgeStubNSDateRefAccess 345 345 +0.0% 1.00x
ArrayInClass 61 61 +0.0% 1.00x
ArrayOfGenericRef 3991 4001 +0.2% 1.00x(?)
ObjectiveCBridgeStubDateAccess 181 181 +0.0% 1.00x
Sim2DArray 276 276 +0.0% 1.00x
ArrayAppendRepeatCol 755 756 +0.1% 1.00x(?)
MonteCarloE 10459 10460 +0.0% 1.00x(?)
ObjectiveCBridgeStubNSDateMutationRef 12868 12809 -0.5% 1.00x(?)
RC4 157 157 +0.0% 1.00x
ArrayAppendToGeneric 597 597 +0.0% 1.00x
HashTest 1811 1809 -0.1% 1.00x(?)
SetIsSubsetOf_OfObjects 309 309 +0.0% 1.00x
ArrayAppend 983 982 -0.1% 1.00x(?)
DictionaryRemove 2333 2334 +0.0% 1.00x(?)
LinkedList 7193 7229 +0.5% 1.00x(?)
ObjectiveCBridgeFromNSSetAnyObjectToStringForced 70002 70339 +0.5% 1.00x(?)
ArrayAppendSequence 976 977 +0.1% 1.00x(?)
ArrayAppendArrayOfInt 597 597 +0.0% 1.00x
ArrayOfPOD 165 165 +0.0% 1.00x
SetUnion 2270 2281 +0.5% 1.00x(?)
ReversedArray 49 49 +0.0% 1.00x
StringBuilder 1428 1427 -0.1% 1.00x(?)
ObserverForwarderStruct 913 914 +0.1% 1.00x(?)
DeadArray 181 181 +0.0% 1.00x
ObjectiveCBridgeStubNSDataAppend 2284 2281 -0.1% 1.00x(?)
BitCount 1 1 +0.0% 1.00x
SevenBoom 1470 1471 +0.1% 1.00x(?)
StringWalk 5955 5955 +0.0% 1.00x
ArrayValueProp 6 6 +0.0% 1.00x
StringHasSuffix 798 798 +0.0% 1.00x
GlobalClass 0 0 +0.0% 1.00x
Memset 234 234 +0.0% 1.00x
Dictionary2OfObjects 3600 3589 -0.3% 1.00x(?)
ArrayValueProp4 6 6 +0.0% 1.00x
TwoSum 1345 1346 +0.1% 1.00x(?)
ArrayValueProp2 6 6 +0.0% 1.00x
ArrayValueProp3 6 6 +0.0% 1.00x
ObjectiveCBridgeStubFromNSDate 3920 3919 -0.0% 1.00x(?)
ObserverPartiallyAppliedMethod 3627 3642 +0.4% 1.00x(?)
ObjectiveCBridgeFromNSDictionaryAnyObjectToString 157640 159181 +1.0% 0.99x(?)
DictionaryBridge 3035 3075 +1.3% 0.99x(?)
ObjectiveCBridgeFromNSArrayAnyObjectForced 6225 6266 +0.7% 0.99x(?)
ObjectiveCBridgeStubFromNSString 925 936 +1.2% 0.99x(?)
ObjectiveCBridgeFromNSSetAnyObject 69908 70418 +0.7% 0.99x(?)
ProtocolDispatch2 158 159 +0.6% 0.99x
CharacterLiteralsLarge 12273 12346 +0.6% 0.99x(?)
StringHasPrefix 751 759 +1.1% 0.99x(?)
ObjectiveCBridgeStubURLAppendPathRef 221893 223798 +0.9% 0.99x(?)
ObjectiveCBridgeToNSSet 37102 37663 +1.5% 0.99x(?)
StringEqualPointerComparison 7274 7335 +0.8% 0.99x(?)
ObjectiveCBridgeStubURLAppendPath 222824 224326 +0.7% 0.99x(?)
NSDictionaryCastToSwift 5425 5457 +0.6% 0.99x(?)
ObjectiveCBridgeToNSDictionary 60818 61481 +1.1% 0.99x(?)
158 2810545 2831206 +0.7% 0.99x
NSStringConversion 725 736 +1.5% 0.99x(?)
ObjectiveCBridgeFromNSSetAnyObjectForced 4293 4331 +0.9% 0.99x(?)
Chars 611 615 +0.7% 0.99x(?)
ObjectiveCBridgeFromNSArrayAnyObjectToStringForced 79168 79760 +0.8% 0.99x(?)
ObjectiveCBridgeStubFromArrayOfNSString 59506 59810 +0.5% 0.99x(?)
ObjectiveCBridgeFromNSDictionaryAnyObjectToStringForced 95425 97185 +1.8% 0.98x(?)
StrToInt 5017 5136 +2.4% 0.98x
AnyHashableWithAClass 64559 65650 +1.7% 0.98x(?)
ObjectiveCBridgeFromNSString 1380 1428 +3.5% 0.97x(?)
CaptureProp 3950 4166 +5.5% 0.95x(?)
**Unoptimized (Onone)**
Regression (0)
Improvement (6)
TEST OLD_MIN NEW_MIN DELTA (%) SPEEDUP
ArrayOfGenericPOD 3231 3057 -5.4% 1.06x
ObjectiveCBridgeStubURLAppendPathRef 231409 219148 -5.3% 1.06x(?)
ObjectiveCBridgeStubDataAppend 3795 3577 -5.7% 1.06x(?)
OpenClose 422 392 -7.1% 1.08x(?)
TypeFlood 205 187 -8.8% 1.10x(?)
PopFrontUnsafePointer 177911 161900 -9.0% 1.10x
No Changes (153)
TEST OLD_MIN NEW_MIN DELTA (%) SPEEDUP
StringHasPrefix 1597 1516 -5.1% 1.05x
StringHasSuffix 1698 1627 -4.2% 1.04x
ObjectiveCBridgeStubURLAppendPath 225444 217807 -3.4% 1.04x(?)
ObjectiveCBridgeFromNSSetAnyObjectToString 109649 106196 -3.1% 1.03x(?)
SortStrings 2492 2440 -2.1% 1.02x
SuperChars 270955 264633 -2.3% 1.02x(?)
StringInterpolation 15563 15208 -2.3% 1.02x(?)
StaticArray 3768 3693 -2.0% 1.02x
StringHasPrefixUnicode 15083 14804 -1.9% 1.02x
ObjectiveCBridgeFromNSDictionaryAnyObjectForced 7551 7494 -0.8% 1.01x(?)
PopFrontArray 13402 13268 -1.0% 1.01x(?)
ObjectAllocation 615 609 -1.0% 1.01x(?)
SortSortedStrings 1250 1243 -0.6% 1.01x
Walsh 13283 13146 -1.0% 1.01x
Dictionary 1885 1867 -0.9% 1.01x
ObjectiveCBridgeToNSDictionary 61364 60510 -1.4% 1.01x(?)
ArrayLiteral 1442 1434 -0.6% 1.01x
NSStringConversion 1259 1249 -0.8% 1.01x(?)
RGBHistogram 35279 35048 -0.7% 1.01x(?)
SetUnion 11956 11854 -0.8% 1.01x
ObjectiveCBridgeStubNSDataAppend 2655 2632 -0.9% 1.01x(?)
StringWalk 23192 22982 -0.9% 1.01x
ArrayValueProp2 3572 3551 -0.6% 1.01x(?)
ArraySubscript 5613 5620 +0.1% 1.00x(?)
ObjectiveCBridgeToNSString 1302 1301 -0.1% 1.00x(?)
StackPromo 128631 128816 +0.1% 1.00x(?)
ObjectiveCBridgeFromNSDictionaryAnyObject 149861 150562 +0.5% 1.00x(?)
RecursiveOwnedParameter 10858 10884 +0.2% 1.00x(?)
Integrate 366 366 +0.0% 1.00x
ObjectiveCBridgeFromNSArrayAnyObjectToString 87151 87544 +0.5% 1.00x(?)
ClassArrayGetter 1273 1273 +0.0% 1.00x
Array2D 814327 810581 -0.5% 1.00x(?)
Histogram 10258 10243 -0.1% 1.00x(?)
ObjectiveCBridgeStubFromNSDateRef 3992 3997 +0.1% 1.00x(?)
MonteCarloPi 53458 53459 +0.0% 1.00x(?)
StringWithCString 151570 150911 -0.4% 1.00x
ObjectiveCBridgeFromNSStringForced 2663 2658 -0.2% 1.00x
Prims 12809 12779 -0.2% 1.00x(?)
SortLettersInPlace 2662 2672 +0.4% 1.00x(?)
DictionarySwap 6200 6194 -0.1% 1.00x(?)
ReversedDictionary 32573 32510 -0.2% 1.00x(?)
ArrayAppendToFromGeneric 715 715 +0.0% 1.00x
Dictionary3OfObjects 2176 2177 +0.1% 1.00x(?)
RangeAssignment 7458 7428 -0.4% 1.00x(?)
ByteSwap 9 9 +0.0% 1.00x
ArrayAppendGenericStructs 1367 1364 -0.2% 1.00x(?)
ArrayAppendLazyMap 274502 274111 -0.1% 1.00x
ArrayPlusEqualFiveElementCollection 545220 547761 +0.5% 1.00x(?)
XorLoop 19936 19942 +0.0% 1.00x(?)
ArrayAppendReserved 3050 3049 -0.0% 1.00x
ObserverClosure 7234 7235 +0.0% 1.00x(?)
CharacterLiteralsSmall 972 972 +0.0% 1.00x
ObjectiveCBridgeStubToNSString 1545 1540 -0.3% 1.00x(?)
ArrayPlusEqualSingleElementCollection 538999 536999 -0.4% 1.00x(?)
ArrayAppendStrings 11796 11827 +0.3% 1.00x(?)
ProtocolDispatch 6111 6095 -0.3% 1.00x
AngryPhonebook 3366 3357 -0.3% 1.00x(?)
ProtocolDispatch2 441 440 -0.2% 1.00x
CharacterLiteralsLarge 13426 13382 -0.3% 1.00x(?)
Dictionary3 1393 1395 +0.1% 1.00x(?)
Dictionary2 3685 3684 -0.0% 1.00x(?)
StrComplexWalk 7694 7677 -0.2% 1.00x(?)
SetIntersect_OfObjects 11643 11644 +0.0% 1.00x(?)
Join 1465 1468 +0.2% 1.00x(?)
ObserverUnappliedMethod 8949 8936 -0.1% 1.00x(?)
ArrayPlusEqualArrayOfInt 712 713 +0.1% 1.00x(?)
ObjectiveCBridgeToNSArray 31354 31362 +0.0% 1.00x(?)
DictionaryOfObjects 4718 4701 -0.4% 1.00x(?)
ObjectiveCBridgeStubToNSDateRef 3357 3365 +0.2% 1.00x(?)
PopFrontArrayGeneric 9249 9254 +0.1% 1.00x(?)
StringEqualPointerComparison 9527 9485 -0.4% 1.00x(?)
CaptureProp 121879 121715 -0.1% 1.00x
PolymorphicCalls 684 684 +0.0% 1.00x
RC4 9354 9335 -0.2% 1.00x
ArrayAppendFromGeneric 715 716 +0.1% 1.00x(?)
ObjectiveCBridgeStubDateMutation 433 433 +0.0% 1.00x
DictionaryLiteral 16235 16271 +0.2% 1.00x(?)
DictionaryRemoveOfObjects 49611 49652 +0.1% 1.00x(?)
UTF8Decode 45607 45472 -0.3% 1.00x(?)
SortStringsUnicode 8285 8279 -0.1% 1.00x(?)
SetIsSubsetOf 2029 2031 +0.1% 1.00x(?)
NopDeinit 45724 45530 -0.4% 1.00x(?)
ObjectiveCBridgeStubToArrayOfNSString 31590 31673 +0.3% 1.00x(?)
ObjectiveCBridgeFromNSArrayAnyObject 68722 68948 +0.3% 1.00x(?)
SetIntersect 12213 12192 -0.2% 1.00x
SetExclusiveOr 21489 21414 -0.3% 1.00x
RGBHistogramOfObjects 84087 84159 +0.1% 1.00x(?)
ObjectiveCBridgeStubNSDateRefAccess 1187 1187 +0.0% 1.00x
ArrayInClass 3959 3959 +0.0% 1.00x
ArrayOfGenericRef 10251 10222 -0.3% 1.00x(?)
Phonebook 20631 20564 -0.3% 1.00x(?)
ObjectiveCBridgeStubDateAccess 970 970 +0.0% 1.00x
Sim2DArray 14522 14523 +0.0% 1.00x(?)
SetExclusiveOr_OfObjects 39336 39285 -0.1% 1.00x
ArrayAppendRepeatCol 214867 214225 -0.3% 1.00x
MonteCarloE 107751 107641 -0.1% 1.00x(?)
SetUnion_OfObjects 28260 28158 -0.4% 1.00x
ReversedBidirectional 139113 138471 -0.5% 1.00x(?)
ArrayAppendToGeneric 716 718 +0.3% 1.00x
HashTest 5558 5562 +0.1% 1.00x(?)
SetIsSubsetOf_OfObjects 1847 1852 +0.3% 1.00x
ArrayAppend 3341 3340 -0.0% 1.00x(?)
DictionaryRemove 17899 17905 +0.0% 1.00x(?)
158 6733055 6712266 -0.3% 1.00x
ObjectiveCBridgeFromNSSetAnyObjectForced 6696 6695 -0.0% 1.00x(?)
ArrayAppendArrayOfInt 712 712 +0.0% 1.00x
ArrayOfPOD 1832 1832 +0.0% 1.00x
ReversedArray 520 518 -0.4% 1.00x(?)
DeadArray 123075 122706 -0.3% 1.00x(?)
BitCount 96 96 +0.0% 1.00x
SevenBoom 1600 1596 -0.2% 1.00x(?)
ObjectiveCBridgeStubFromArrayOfNSString 60296 60130 -0.3% 1.00x(?)
GlobalClass 0 0 +0.0% 1.00x
Memset 20582 20586 +0.0% 1.00x(?)
Dictionary2OfObjects 6011 6012 +0.0% 1.00x(?)
ArrayValueProp4 3382 3383 +0.0% 1.00x(?)
TwoSum 4898 4886 -0.2% 1.00x(?)
ArrayValueProp3 3432 3440 +0.2% 1.00x(?)
ObjectiveCBridgeStubFromNSDate 3920 3918 -0.1% 1.00x(?)
ObserverPartiallyAppliedMethod 8773 8757 -0.2% 1.00x(?)
DictionarySwapOfObjects 20407 20547 +0.7% 0.99x(?)
ObjectiveCBridgeFromNSArrayAnyObjectForced 9508 9593 +0.9% 0.99x(?)
ErrorHandling 3991 4035 +1.1% 0.99x(?)
ObjectiveCBridgeStubFromNSStringRef 208 210 +1.0% 0.99x
ObjectiveCBridgeStubFromNSString 931 938 +0.8% 0.99x(?)
ObjectiveCBridgeFromNSSetAnyObject 72662 73538 +1.2% 0.99x(?)
NSError 703 710 +1.0% 0.99x(?)
ObjectiveCBridgeStubToNSStringRef 146 147 +0.7% 0.99x(?)
ArrayAppendOptionals 1357 1366 +0.7% 0.99x(?)
IterateData 10770 10841 +0.7% 0.99x
Hanoi 19147 19432 +1.5% 0.99x
ObjectiveCBridgeFromNSDictionaryAnyObjectToStringForced 98171 99235 +1.1% 0.99x(?)
NSDictionaryCastToSwift 6489 6538 +0.8% 0.99x(?)
StringBuilder 2881 2899 +0.6% 0.99x(?)
ObjectiveCBridgeFromNSString 4016 4037 +0.5% 0.99x(?)
StringHasSuffixUnicode 62114 62681 +0.9% 0.99x
LinkedList 27533 27902 +1.3% 0.99x(?)
MapReduce 45263 45502 +0.5% 0.99x(?)
ObjectiveCBridgeFromNSSetAnyObjectToStringForced 74181 75192 +1.4% 0.99x(?)
ArrayAppendSequence 75011 75732 +1.0% 0.99x(?)
Chars 6413 6496 +1.3% 0.99x
StrToInt 5350 5426 +1.4% 0.99x
ObserverForwarderStruct 5234 5272 +0.7% 0.99x(?)
AnyHashableWithAClass 79791 80780 +1.2% 0.99x
ObjectiveCBridgeFromNSArrayAnyObjectToStringForced 80070 80810 +0.9% 0.99x(?)
ArrayValueProp 2997 3030 +1.1% 0.99x(?)
DictionaryBridge 3110 3176 +2.1% 0.98x(?)
ObjectiveCBridgeStubToNSDate 14693 14938 +1.7% 0.98x(?)
ArrayOfRef 9235 9423 +2.0% 0.98x
ObjectiveCBridgeToNSSet 37507 38260 +2.0% 0.98x(?)
ObjectiveCBridgeFromNSDictionaryAnyObjectToString 161179 166747 +3.5% 0.97x(?)
Calculator 949 991 +4.4% 0.96x
ObjectiveCBridgeStubNSDateMutationRef 14966 15655 +4.6% 0.96x(?)
**Hardware Overview** Model Name: Mac mini Model Identifier: Macmini7,1 Processor Name: Intel Core i5 Processor Speed: 2.8 GHz Number of Processors: 1 Total Number of Cores: 2 L2 Cache (per Core): 256 KB L3 Cache: 3 MB Memory: 16 GB

@atrick
Copy link
Contributor

atrick commented Jan 28, 2017

In my mind, an atomic increment is more primitive than a closure taking function. I don't like unnecessary abstraction in the implementation of language primitives. I've seen too much SIL code and waiting too long for builds.

I confess, I was thinking that Int, Int64 were typealiases on a 64-bit platform, which is incorrect.

It's still safe to use assumingMemoryBound. In fact, that's no less safe than unsafeBitCast was. But proving correctness depends on the fact that Int and Int64 are imported from C for the purpose of alias analysis. In other words, they need to be special-cased as "related types" anyway for C interoperability to be safe.

@atrick
Copy link
Contributor

atrick commented Jan 28, 2017

Also, none of this is meant to be pretty or convenient. I wouldn't worry about elegance or best practices at this level. Yes, we should for stdlib data structures, but this is just a builtin that happens to be implementable in Swift.

@jrose-apple jrose-apple force-pushed the unsafeBitCast-Runtime.swift.gyb branch from 9fd0298 to 6501834 Compare January 31, 2017 00:51
@jrose-apple
Copy link
Contributor Author

jrose-apple commented Jan 31, 2017

Updated. @atrick, is this what you were thinking? Are all of these uses safe? (Compare with the previous version: jrose-apple/swift@9fd0298)

@jrose-apple
Copy link
Contributor Author

@swift-ci Please smoke test

@atrick
Copy link
Contributor

atrick commented Jan 31, 2017

Yes, I'll stand by the correctness of this patch...

I had to think about it overnight. Your original patch was generally the right way to handle these casts. But since this is what I call a "language primitive" it's a good place to explore the boundaries of manual optimization, both as a performance guarantee and because adding abstraction in "inlineable" primitives could conceivably hurt build time.

I was arguing that Int is related to Int64 on 64-bit. That's not naturally true though. Even in C, long and long long are not AFAIK "compatible" types (that's analogous with Swift's definition of related types). @rjmccall could weigh in here. However, I recommend that we explicitly call-out Int as related to Int32/Int64 depending on the platform. That way #if arch code like this will be sane and consistent with the intuition of users. I would never want the compiler to disambiguate these cases.

The rule in the compiler should actually fall out of this logic:

struct Int and struct Int64 represent C types in imports, so alias
analysis needs to look at their members to determine whether pointers
to them can alias. Both just hold a Builtin.Int64, so they are
related by virtue of being structurally compatible C types, and their
pointers might alias.

I was not originally suggesting you change the other cases, but as long as we're being maximally aggressive in the atomics implementation... I also claim that we should explicitly call out AnyObject, UnsafeRawPointer, UnsafePointer<T>, and their optional variants as related types. I have seen other code that makes this assumption and already have a note to add that case to the specs.

UnsafeRawPointer and UnsafePointer<T> are both, for the purpose of alias analysis, imported from C, and both hold Builtin.RawPointer. So they are related by the same logic a Int and Int64.

I suggest we call out AnyObject in the type system spec to be a magic protocol that is by fiat related to UnsafeRawPointer, UnsafePointer<T> and any other class or class protocol. That way, pointers-to-generic-pointers may alias.

UnsafePointer<T> is still unrelated UnsafePointer<U> and arbitrary class protocols are unrelated to each other.

@jckarter
Copy link
Contributor

Can we sidestep the memory binding questions by having the _swift_stdlib_atomic* wrappers take an UnsafeRawPointer? As you noted, @atrick, the underlying LLVM operations are untyped operations on Builtin.RawPointer, so requiring the memory to be bound to a type seems unnecessary.

@atrick
Copy link
Contributor

atrick commented Jan 31, 2017

It depends on how clear you want the wrapper API's to be. To use _stdlib_atomicCompareExchange correctly, it's important to realize that you're passing a pointer-to-pointer. To use _stdlib_AtomicFetchAndAdd it's important to realize you're passing a pointer-to-Int.

The assumingMemoryBound cast could be removed from both the generic _stdlib_atomicCompareExchangeStrongPtr<T> and the ArcRef wrapper by introducing an even lower level Impl helper that just takes UnsafeRawPointer and does an untyped pointer load of the expected value from that.

@jrose-apple can decide if that's worth it. Or just hand it to me if you're bored of this.

@jrose-apple jrose-apple force-pushed the unsafeBitCast-Runtime.swift.gyb branch from 6501834 to 498afb8 Compare January 31, 2017 23:54
@jrose-apple
Copy link
Contributor Author

I tried for a moment and it didn't look like it was going to go very well due to the need to load from and store to expected. I'll let you make the final call.

@swift-ci Please smoke test

@atrick
Copy link
Contributor

atrick commented Feb 3, 2017

@jrose-apple I won't get to this today so I'll go ahead and push your change to fix the warning. I made a note to come back to it later. Thanks!

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.

5 participants