Skip to content

Resolve ABI FIXME#158 #5722

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
Nov 16, 2016
Merged

Resolve ABI FIXME#158 #5722

merged 1 commit into from
Nov 16, 2016

Conversation

Gankra
Copy link
Contributor

@Gankra Gankra commented Nov 11, 2016

So the comment and rdar are entirely unhelpful as to the actual intent here. I get the impression that completely killing _UnmanagedAnyObjectArray was the desired outcome, but quite frankly this just makes the code way more messy and error-prone for no obvious benefit.

That said, if anyone knows what the actual performance concern here was, I'm all ears. I have no real intuition on when stricter aliasing is and isn't a win. (Although this hardly seems like it makes the aliasing any stricter?)

At very least, this code is a bit cleaner.

@Gankra
Copy link
Contributor Author

Gankra commented Nov 11, 2016

@swift-ci Please test

@Gankra
Copy link
Contributor Author

Gankra commented Nov 11, 2016

@swift-ci Please benchmark

@Gankra
Copy link
Contributor Author

Gankra commented Nov 11, 2016

(Throwing in benchmarks because, hey, it's supposed to be a perf thing, right?)

@Gankra
Copy link
Contributor Author

Gankra commented Nov 11, 2016

Oh, right:

Fixes rdar://problem/16861508

@swift-ci
Copy link
Contributor

Build comment file:

Optimized (O)

Regression (3)

TEST OLD_MIN NEW_MIN DELTA (%) SPEEDUP
PopFrontArray 1123 1257 +11.9% 0.89x(?)
PopFrontArrayGeneric 1118 1255 +12.2% 0.89x(?)
ClassArrayGetter 12 13 +8.3% 0.92x(?)

Improvement (3)

TEST OLD_MIN NEW_MIN DELTA (%) SPEEDUP
ObjectiveCBridgeStubToNSDate 13727 12811 -6.7% 1.07x(?)
OpenClose 54 48 -11.1% 1.12x
ByteSwap 1 0 -99.9% 1001.00x

No Changes (141)

TEST OLD_MIN NEW_MIN DELTA (%) SPEEDUP
RangeAssignment 293 280 -4.4% 1.05x
NopDeinit 38139 36657 -3.9% 1.04x
Hanoi 3447 3336 -3.2% 1.03x
ObjectiveCBridgeStubURLAppendPathRef 234876 230762 -1.8% 1.02x(?)
ObjectiveCBridgeToNSSet 40372 39624 -1.9% 1.02x(?)
AnyHashableWithAClass 63571 63163 -0.6% 1.01x(?)
StringHasSuffixUnicode 63372 62994 -0.6% 1.01x(?)
HashTest 1752 1734 -1.0% 1.01x(?)
StringEqualPointerComparison 7341 7300 -0.6% 1.01x(?)
ObjectiveCBridgeStubURLAppendPath 236314 233751 -1.1% 1.01x(?)
SortStringsUnicode 8224 8162 -0.8% 1.01x(?)
ObjectiveCBridgeFromNSArrayAnyObject 75919 75457 -0.6% 1.01x(?)
ObjectiveCBridgeStubDataAppend 3007 2988 -0.6% 1.01x(?)
DictionaryRemove 2337 2307 -1.3% 1.01x(?)
ObjectiveCBridgeFromNSArrayAnyObjectToStringForced 90582 90086 -0.6% 1.01x(?)
ObjectiveCBridgeStubFromArrayOfNSString 58653 58213 -0.8% 1.01x(?)
Dictionary2OfObjects 3460 3434 -0.8% 1.01x(?)
TwoSum 1336 1328 -0.6% 1.01x(?)
ArraySubscript 1428 1421 -0.5% 1.00x(?)
ObjectiveCBridgeToNSString 1070 1075 +0.5% 1.00x(?)
DictionarySwapOfObjects 6311 6307 -0.1% 1.00x(?)
StackPromo 21492 21534 +0.2% 1.00x(?)
ObjectiveCBridgeFromNSDictionaryAnyObject 177030 177426 +0.2% 1.00x(?)
RecursiveOwnedParameter 1937 1934 -0.1% 1.00x(?)
ObjectiveCBridgeStubToNSString 1283 1279 -0.3% 1.00x(?)
Array2D 2029 2023 -0.3% 1.00x(?)
ObjectiveCBridgeFromNSDictionaryAnyObjectForced 5432 5406 -0.5% 1.00x(?)
ObjectiveCBridgeStubFromNSDateRef 3652 3652 +0.0% 1.00x
MonteCarloPi 44920 45005 +0.2% 1.00x(?)
StringWithCString 155052 154990 -0.0% 1.00x(?)
ObjectiveCBridgeFromNSArrayAnyObjectForced 6110 6122 +0.2% 1.00x(?)
ObjectiveCBridgeFromNSStringForced 2665 2674 +0.3% 1.00x(?)
SortLettersInPlace 973 975 +0.2% 1.00x(?)
DictionarySwap 417 415 -0.5% 1.00x
ObjectiveCBridgeFromNSString 1822 1816 -0.3% 1.00x(?)
Dictionary3OfObjects 872 868 -0.5% 1.00x(?)
StrComplexWalk 2906 2905 -0.0% 1.00x(?)
ArrayAppendGenericStructs 1235 1238 +0.2% 1.00x(?)
SuperChars 210781 210639 -0.1% 1.00x(?)
ArrayAppendLazyMap 849 848 -0.1% 1.00x(?)
XorLoop 380 380 +0.0% 1.00x
StringInterpolation 10911 10879 -0.3% 1.00x(?)
Integrate 238 238 +0.0% 1.00x
ArrayAppendStrings 12090 12111 +0.2% 1.00x(?)
Join 470 468 -0.4% 1.00x(?)
ProtocolDispatch 3039 3040 +0.0% 1.00x(?)
ObjectAllocation 153 153 +0.0% 1.00x
TypeFlood 0 0 +0.0% 1.00x
ObjectiveCBridgeFromNSSetAnyObject 84688 84452 -0.3% 1.00x(?)
ProtocolDispatch2 158 158 +0.0% 1.00x
Dictionary3 521 521 +0.0% 1.00x
Dictionary2 2024 2026 +0.1% 1.00x(?)
ErrorHandling 2935 2922 -0.4% 1.00x(?)
ArrayOfRef 3565 3567 +0.1% 1.00x(?)
ArrayAppendOptionals 1240 1237 -0.2% 1.00x(?)
NSError 326 325 -0.3% 1.00x(?)
DictionaryOfObjects 2287 2295 +0.3% 1.00x(?)
PopFrontUnsafePointer 9163 9160 -0.0% 1.00x(?)
CaptureProp 4571 4562 -0.2% 1.00x(?)
PolymorphicCalls 22 22 +0.0% 1.00x
ArrayAppendReserved 536 536 +0.0% 1.00x
ObjectiveCBridgeStubToNSStringRef 116 116 +0.0% 1.00x
Calculator 34 34 +0.0% 1.00x
MapReduce 342 342 +0.0% 1.00x
ObjectiveCBridgeStubDateMutation 273 273 +0.0% 1.00x
IterateData 2546 2551 +0.2% 1.00x(?)
ArrayOfGenericPOD 220 220 +0.0% 1.00x
UTF8Decode 288 288 +0.0% 1.00x
SetIsSubsetOf 250 250 +0.0% 1.00x
Dictionary 716 717 +0.1% 1.00x(?)
ObjectiveCBridgeStubToArrayOfNSString 29327 29339 +0.0% 1.00x(?)
SetExclusiveOr 2913 2899 -0.5% 1.00x(?)
NSDictionaryCastToSwift 4997 4999 +0.0% 1.00x(?)
ObjectiveCBridgeStubNSDateRefAccess 338 338 +0.0% 1.00x
StringHasSuffix 800 800 +0.0% 1.00x
Phonebook 7517 7508 -0.1% 1.00x(?)
ObjectiveCBridgeStubDateAccess 182 182 +0.0% 1.00x
Sim2DArray 277 277 +0.0% 1.00x
SetExclusiveOr_OfObjects 7411 7431 +0.3% 1.00x(?)
ArrayAppendRepeatCol 643 643 +0.0% 1.00x
ObjectiveCBridgeToNSDictionary 62777 62966 +0.3% 1.00x(?)
MonteCarloE 10553 10532 -0.2% 1.00x(?)
SetUnion_OfObjects 6233 6237 +0.1% 1.00x(?)
GlobalClass 0 0 +0.0% 1.00x
SetIsSubsetOf_OfObjects 307 308 +0.3% 1.00x
ArrayAppend 775 774 -0.1% 1.00x(?)
LinkedList 7213 7208 -0.1% 1.00x(?)
RGBHistogramOfObjects 21591 21647 +0.3% 1.00x(?)
RGBHistogram 2265 2269 +0.2% 1.00x(?)
ObjectiveCBridgeFromNSSetAnyObjectForced 4724 4721 -0.1% 1.00x(?)
ArrayAppendSequence 1062 1061 -0.1% 1.00x(?)
ArrayAppendArrayOfInt 599 599 +0.0% 1.00x
ArrayOfPOD 182 182 +0.0% 1.00x
SetUnion 2425 2425 +0.0% 1.00x
StringBuilder 1316 1314 -0.1% 1.00x(?)
146 3062572 3067047 +0.1% 1.00x
DeadArray 185 185 +0.0% 1.00x
ArrayValueProp2 6 6 +0.0% 1.00x
BitCount 1 1 +0.0% 1.00x
SevenBoom 1354 1352 -0.1% 1.00x(?)
StringWalk 5914 5912 -0.0% 1.00x(?)
ArrayValueProp 6 6 +0.0% 1.00x
ObjectiveCBridgeStubNSDateMutationRef 11801 11759 -0.4% 1.00x(?)
Memset 235 235 +0.0% 1.00x
ArrayValueProp4 6 6 +0.0% 1.00x
ObjectiveCBridgeStubNSDataAppend 2440 2437 -0.1% 1.00x(?)
ArrayValueProp3 6 6 +0.0% 1.00x
ObserverPartiallyAppliedMethod 3351 3357 +0.2% 1.00x(?)
ObjectiveCBridgeFromNSDictionaryAnyObjectToString 200400 202847 +1.2% 0.99x(?)
ObjectiveCBridgeFromNSArrayAnyObjectToString 117097 117693 +0.5% 0.99x(?)
Histogram 288 292 +1.4% 0.99x
SortStrings 1813 1836 +1.3% 0.99x
DictionaryBridge 3683 3718 +0.9% 0.99x(?)
Prims 729 733 +0.6% 0.99x(?)
ObserverClosure 1947 1972 +1.3% 0.99x(?)
ObjectiveCBridgeStubFromNSString 765 773 +1.1% 0.99x(?)
StaticArray 2739 2776 +1.4% 0.99x(?)
ArrayLiteral 1171 1181 +0.8% 0.99x(?)
StringHasPrefix 713 719 +0.8% 0.99x(?)
SetIntersect_OfObjects 1448 1458 +0.7% 0.99x(?)
ObserverUnappliedMethod 2384 2409 +1.1% 0.99x(?)
ObjectiveCBridgeToNSArray 30225 30595 +1.2% 0.99x(?)
DictionaryLiteral 1266 1273 +0.6% 0.99x(?)
DictionaryRemoveOfObjects 19420 19619 +1.0% 0.99x(?)
ObjectiveCBridgeFromNSDictionaryAnyObjectToStringForced 127686 128480 +0.6% 0.99x(?)
ObjectiveCBridgeFromNSSetAnyObjectToString 138787 140532 +1.3% 0.99x(?)
ArrayOfGenericRef 3592 3645 +1.5% 0.99x(?)
StringHasPrefixUnicode 14126 14258 +0.9% 0.99x
ObjectiveCBridgeFromNSSetAnyObjectToStringForced 101380 102091 +0.7% 0.99x(?)
Chars 619 623 +0.7% 0.99x
ObserverForwarderStruct 893 902 +1.0% 0.99x(?)
AngryPhonebook 2837 2866 +1.0% 0.99x(?)
RC4 165 166 +0.6% 0.99x
ObjectiveCBridgeStubFromNSDate 3584 3619 +1.0% 0.99x(?)
ObjectiveCBridgeStubFromNSStringRef 133 136 +2.3% 0.98x(?)
ObjectiveCBridgeStubToNSDateRef 3235 3296 +1.9% 0.98x(?)
SetIntersect 355 363 +2.2% 0.98x(?)
StrToInt 5119 5241 +2.4% 0.98x
ArrayInClass 62 63 +1.6% 0.98x(?)
NSStringConversion 794 812 +2.3% 0.98x(?)
Walsh 310 321 +3.5% 0.97x
**Unoptimized (Onone)**

Regression (1)

TEST OLD_MIN NEW_MIN DELTA (%) SPEEDUP
OpenClose 436 462 +6.0% 0.94x

Improvement (5)

TEST OLD_MIN NEW_MIN DELTA (%) SPEEDUP
ArrayOfGenericPOD 3638 3429 -5.7% 1.06x
PopFrontUnsafePointer 258709 242550 -6.2% 1.07x
StringHasPrefix 1685 1557 -7.6% 1.08x
StringHasSuffix 1762 1573 -10.7% 1.12x
TypeFlood 206 180 -12.6% 1.14x(?)

No Changes (141)

TEST OLD_MIN NEW_MIN DELTA (%) SPEEDUP
ArrayOfPOD 2513 2391 -4.8% 1.05x
StringWalk 21323 20442 -4.1% 1.04x
StrComplexWalk 7636 7385 -3.3% 1.03x
CaptureProp 118154 114576 -3.0% 1.03x
ObjectiveCBridgeStubNSDateMutationRef 14789 14317 -3.2% 1.03x(?)
ObjectiveCBridgeFromNSDictionaryAnyObjectToString 215262 211761 -1.6% 1.02x(?)
ObjectiveCBridgeFromNSArrayAnyObjectForced 10402 10215 -1.8% 1.02x(?)
DictionarySwap 6078 5972 -1.7% 1.02x
PopFrontArray 24664 24220 -1.8% 1.02x
ArrayAppendGenericStructs 1322 1301 -1.6% 1.02x(?)
ObjectiveCBridgeStubFromNSString 821 803 -2.2% 1.02x
ObjectiveCBridgeStubURLAppendPath 242700 237976 -1.9% 1.02x(?)
ArrayAppendRepeatCol 216112 211762 -2.0% 1.02x(?)
ObjectiveCBridgeToNSDictionary 63767 62743 -1.6% 1.02x(?)
ObjectiveCBridgeFromNSSetAnyObjectToString 158793 156021 -1.8% 1.02x(?)
ArrayAppendSequence 107014 104858 -2.0% 1.02x
StrToInt 5824 5716 -1.9% 1.02x
ObjectiveCBridgeFromNSArrayAnyObjectToStringForced 93053 91360 -1.8% 1.02x(?)
RecursiveOwnedParameter 10976 10892 -0.8% 1.01x(?)
ObjectiveCBridgeStubToNSDate 13956 13830 -0.9% 1.01x(?)
AnyHashableWithAClass 77663 77092 -0.7% 1.01x
StaticArray 35328 35079 -0.7% 1.01x(?)
ObjectiveCBridgeFromNSSetAnyObject 90274 89643 -0.7% 1.01x(?)
ObjectiveCBridgeToNSArray 30571 30124 -1.5% 1.01x(?)
Hanoi 19644 19389 -1.3% 1.01x(?)
SortStringsUnicode 9368 9312 -0.6% 1.01x(?)
ObjectiveCBridgeFromNSDictionaryAnyObjectToStringForced 135375 134058 -1.0% 1.01x(?)
Dictionary 1846 1836 -0.5% 1.01x
ObjectiveCBridgeStubDataAppend 3417 3393 -0.7% 1.01x(?)
SetUnion_OfObjects 28298 28105 -0.7% 1.01x
146 6008088 5961719 -0.8% 1.01x
SevenBoom 1501 1492 -0.6% 1.01x(?)
TwoSum 4977 4930 -0.9% 1.01x(?)
ArraySubscript 5629 5629 +0.0% 1.00x
ObjectiveCBridgeToNSString 1104 1107 +0.3% 1.00x(?)
DictionarySwapOfObjects 19233 19252 +0.1% 1.00x(?)
StackPromo 127372 127987 +0.5% 1.00x(?)
ObjectiveCBridgeFromNSDictionaryAnyObject 182821 182366 -0.2% 1.00x(?)
ObjectiveCBridgeStubToNSString 1333 1333 +0.0% 1.00x
ObjectiveCBridgeFromNSArrayAnyObjectToString 119563 120019 +0.4% 1.00x(?)
ClassArrayGetter 1266 1268 +0.2% 1.00x(?)
Array2D 813433 812707 -0.1% 1.00x(?)
Histogram 10062 10057 -0.1% 1.00x(?)
ObjectiveCBridgeFromNSDictionaryAnyObjectForced 7781 7811 +0.4% 1.00x(?)
ObjectiveCBridgeStubFromNSDateRef 4041 4045 +0.1% 1.00x(?)
MonteCarloPi 53689 53677 -0.0% 1.00x(?)
StringWithCString 152685 152714 +0.0% 1.00x(?)
ObjectiveCBridgeFromNSStringForced 3090 3092 +0.1% 1.00x(?)
Prims 13265 13215 -0.4% 1.00x(?)
SortLettersInPlace 2588 2585 -0.1% 1.00x(?)
Dictionary3OfObjects 2000 1999 -0.1% 1.00x(?)
RangeAssignment 13266 13281 +0.1% 1.00x(?)
ByteSwap 9 9 +0.0% 1.00x
SuperChars 262319 262225 -0.0% 1.00x(?)
ArrayAppendLazyMap 245493 246232 +0.3% 1.00x(?)
XorLoop 20010 20004 -0.0% 1.00x(?)
ArrayAppendReserved 3302 3303 +0.0% 1.00x(?)
ObserverClosure 6998 6986 -0.2% 1.00x(?)
ArrayAppendOptionals 1313 1319 +0.5% 1.00x(?)
Integrate 368 369 +0.3% 1.00x(?)
ArrayAppendStrings 11811 11815 +0.0% 1.00x(?)
ErrorHandling 3836 3836 +0.0% 1.00x
ArrayLiteral 1254 1250 -0.3% 1.00x(?)
ProtocolDispatch2 444 444 +0.0% 1.00x
HashTest 5271 5257 -0.3% 1.00x(?)
Dictionary3 1386 1385 -0.1% 1.00x(?)
Dictionary2 4056 4056 +0.0% 1.00x
Join 1460 1460 +0.0% 1.00x
ArrayOfRef 9176 9203 +0.3% 1.00x(?)
ObjectiveCBridgeStubURLAppendPathRef 237262 237432 +0.1% 1.00x(?)
ArrayAppend 3555 3555 +0.0% 1.00x
ObjectiveCBridgeStubDateMutation 517 517 +0.0% 1.00x
NSError 686 686 +0.0% 1.00x
DictionaryOfObjects 4537 4542 +0.1% 1.00x(?)
ObjectiveCBridgeStubToNSDateRef 3344 3349 +0.1% 1.00x(?)
PopFrontArrayGeneric 9873 9841 -0.3% 1.00x(?)
StringEqualPointerComparison 9572 9548 -0.2% 1.00x(?)
PolymorphicCalls 1143 1148 +0.4% 1.00x(?)
RC4 9393 9396 +0.0% 1.00x(?)
ObjectiveCBridgeStubToNSStringRef 155 155 +0.0% 1.00x
MapReduce 45076 45230 +0.3% 1.00x
IterateData 10699 10715 +0.1% 1.00x(?)
DictionaryLiteral 15717 15734 +0.1% 1.00x(?)
DictionaryRemoveOfObjects 46572 46575 +0.0% 1.00x(?)
UTF8Decode 43489 43504 +0.0% 1.00x(?)
SetIsSubsetOf 2128 2125 -0.1% 1.00x
NopDeinit 58128 58113 -0.0% 1.00x(?)
ObjectiveCBridgeFromNSArrayAnyObject 78595 78526 -0.1% 1.00x(?)
SetIntersect 13024 13045 +0.2% 1.00x(?)
SetExclusiveOr 23621 23614 -0.0% 1.00x(?)
RGBHistogramOfObjects 89417 89455 +0.0% 1.00x(?)
ObjectiveCBridgeStubNSDateRefAccess 1230 1230 +0.0% 1.00x
StringBuilder 2775 2776 +0.0% 1.00x(?)
ArrayInClass 3958 3958 +0.0% 1.00x
ArrayOfGenericRef 10173 10189 +0.2% 1.00x(?)
ObjectiveCBridgeFromNSString 5112 5111 -0.0% 1.00x(?)
ObjectiveCBridgeStubDateAccess 1065 1065 +0.0% 1.00x
Sim2DArray 14782 14778 -0.0% 1.00x(?)
SetExclusiveOr_OfObjects 40294 40225 -0.2% 1.00x
MonteCarloE 108820 108623 -0.2% 1.00x(?)
StringHasSuffixUnicode 65101 65082 -0.0% 1.00x(?)
Walsh 13343 13282 -0.5% 1.00x(?)
SetIsSubsetOf_OfObjects 1901 1906 +0.3% 1.00x
ObjectiveCBridgeToNSSet 40351 40218 -0.3% 1.00x(?)
LinkedList 27646 27593 -0.2% 1.00x(?)
ObjectiveCBridgeFromNSSetAnyObjectForced 7525 7519 -0.1% 1.00x(?)
ArrayAppendArrayOfInt 641 641 +0.0% 1.00x
SetUnion 12162 12183 +0.2% 1.00x(?)
ObserverForwarderStruct 5220 5221 +0.0% 1.00x(?)
ArrayValueProp2 3143 3136 -0.2% 1.00x(?)
BitCount 96 96 +0.0% 1.00x
ArrayValueProp 2619 2625 +0.2% 1.00x(?)
ObjectiveCBridgeStubFromArrayOfNSString 59924 59955 +0.1% 1.00x(?)
GlobalClass 0 0 +0.0% 1.00x
Memset 20960 20958 -0.0% 1.00x(?)
Dictionary2OfObjects 5926 5936 +0.2% 1.00x(?)
ArrayValueProp4 3040 3035 -0.2% 1.00x(?)
ObjectiveCBridgeStubNSDataAppend 2771 2770 -0.0% 1.00x(?)
ArrayValueProp3 3087 3079 -0.3% 1.00x(?)
ObjectiveCBridgeStubFromNSDate 3970 3955 -0.4% 1.00x(?)
ObserverPartiallyAppliedMethod 8336 8315 -0.2% 1.00x(?)
DictionaryBridge 3789 3834 +1.2% 0.99x(?)
ObjectiveCBridgeStubFromNSStringRef 169 170 +0.6% 0.99x(?)
ProtocolDispatch 6473 6529 +0.9% 0.99x(?)
SetIntersect_OfObjects 11819 11949 +1.1% 0.99x(?)
ObserverUnappliedMethod 8707 8752 +0.5% 0.99x(?)
Calculator 921 933 +1.3% 0.99x
NSDictionaryCastToSwift 6152 6237 +1.4% 0.99x(?)
DictionaryRemove 16910 16996 +0.5% 0.99x(?)
ObjectiveCBridgeFromNSSetAnyObjectToStringForced 112338 113334 +0.9% 0.99x(?)
NSStringConversion 2956 2986 +1.0% 0.99x(?)
RGBHistogram 39938 40161 +0.6% 0.99x(?)
Chars 4984 5016 +0.6% 0.99x
DeadArray 120304 121035 +0.6% 0.99x(?)
AngryPhonebook 2987 3004 +0.6% 0.99x(?)
StringInterpolation 15353 15678 +2.1% 0.98x(?)
ObjectAllocation 549 559 +1.8% 0.98x(?)
ObjectiveCBridgeStubToArrayOfNSString 28971 29666 +2.4% 0.98x(?)
Phonebook 62445 63461 +1.6% 0.98x(?)
StringHasPrefixUnicode 15434 15700 +1.7% 0.98x
SortStrings 2696 2786 +3.3% 0.97x
**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

@Gankra
Copy link
Contributor Author

Gankra commented Nov 14, 2016

@atrick any thoughts on this? Seems like your wheelhouse.

atrick
atrick previously requested changes Nov 15, 2016
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.

I think the code was correct as it was, but we need to replace the comment with something meaningful like:

Temprarily view an array of AnyObject as an array of Unmanaged<AnyObject> for fast iteration and transformation of the elements. Access the underlying raw memory as Unmanaged<AnyObject> using untyped memory access. The memory remains bound to managed AnyObjects.

(or if you choose to rebind memory adjust the comment appropriately)

The only "messy" thing in the original code is the byte offset computation. That could easily be done with a convenience method on UnsafeRawPointer. I had a proposal to do that, but we could not decide on good names for the argument labels, so I gave up on it. e.g.

UnsafeRawPointer {
  // func load<T>(fromContiguous: T.Type, atIndex: Int) -> T
  func load<T>(fromContiguous: T.Type, at: Int) -> T
}


internal init(_ up: UnsafeMutablePointer<AnyObject>) {
self.value = UnsafeMutableRawPointer(up)
self.value = UnsafeMutableRawPointer(up).assumingMemoryBound(
to: Unmanaged<AnyObject>.self)
Copy link
Contributor

Choose a reason for hiding this comment

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

You can't assume memory was bound to Unmanaged<AnyObject> because no one ever bound the memory to that type. The safe thing to do is to simply avoid binding memory to Unmanaged<AnyObject> and access the raw pointer, giving up on strict aliasing. That's what the code does before your change. After your change, the code is more potentially more performant, but undefined.

I don't know that we actually care about strict aliasing here. None of your reported -O speedups are legit. The -Onone speedups have nothing to do with strict aliasing--it's just an artifact of not having an @transparent method somewhere.

If we do care about strict aliasing, then creating the _UnmanagedAnyObjectArray needs to bind the memory to Unmanaged<AnyObject> (init() needs the capacity). And we need to bind it back to managed AnyObject when through. Of course, that's only safe if no other code loads or stores at those memory locations, which does seem to be the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've been meaning to ask: what's the deal with the capacity argument? Are there real alias analyses and use-cases fine-grain enough to actually take advantage of this?

Copy link
Contributor

Choose a reason for hiding this comment

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

The optimizer is not taking advantage of the capacity. The user is making a contract with the language. The memory's bound type is a property of the memory not of the pointer value.

ptrA: UnsafePointer<A>
let ptrB = UnsafeRawPointer(ptrA).bindMemory(B.self)
let a = ptrA[1] // Is this undefined behavior? (yes if capacity > 1)
let b = ptrB[1] // Is this undefined behavior? (yes if capacity < 2)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So it's useless to the compiler and programmer, but it exists to make the system much more simple and consistent, from a formal modeling perspective?

Copy link
Contributor

Choose a reason for hiding this comment

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

The other hope is that someone could use the info to write a sanitizer-like dynamic analysis some day.

@Gankra
Copy link
Contributor Author

Gankra commented Nov 15, 2016

@swift-ci please smoke test

@Gankra
Copy link
Contributor Author

Gankra commented Nov 15, 2016

I opted for just updating the comment; the absence of value type destructors in Swift makes properly rebinding the memory too dangerous/tedious.

@Gankra
Copy link
Contributor Author

Gankra commented Nov 16, 2016

@swift-ci please smoke test

@Gankra Gankra dismissed atrick’s stale review November 16, 2016 21:42

Signed off on the new version

@Gankra Gankra merged commit 7536b94 into swiftlang:master Nov 16, 2016
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