Skip to content

[stdlib] Stop swapping self in Array.withUnsafeMutableBufferPointer #38867

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
Aug 16, 2021

Conversation

lorentey
Copy link
Member

Array.withUnsafeMutableBufferPointer swaps out self's buffer for an empty array for the duration of its closure call, to prevent the buffer getting released by the closure.

This code predates the introduction of the law of exclusivity, and I think we don't need it anymore.

Removing the swap gets rid of the need to create an empty array, which eliminates a pair of swift_retain/release calls for the dummy value. This can make a measurable difference in performance sensitive code.

The law of exclusivity now allows us to remove the safety belt of swapping out self, getting rid of the need to create an empty array. (Which eliminates a swift_retain call.)
@lorentey
Copy link
Member Author

@swift-ci test

@lorentey
Copy link
Member Author

@swift-ci benchmark

@lorentey
Copy link
Member Author

lorentey commented Aug 13, 2021

Cc @aschwaighofer, @eeckstein -- would this be an okay change? IIUC, the law of exclusivity prevents body from accessing or escaping self in any way. (Can we trust exclusivity checking to catch mistakes?)

@swift-ci
Copy link
Contributor

Performance (x86_64): -O

Regression OLD NEW DELTA RATIO
ArrayAppendOptionals 1130 2080 +84.1% 0.54x (?)
DictionaryOfAnyHashableStrings_insert 2744 5040 +83.7% 0.54x
Breadcrumbs.MutatedUTF16ToIdx.ASCII 3 4 +33.3% 0.75x
Set.isDisjoint.Int25 241 309 +28.2% 0.78x (?)
Set.isDisjoint.Int50 241 303 +25.7% 0.80x (?)
DictionaryKeysContainsNative 19 23 +21.1% 0.83x (?)
ObjectiveCBridgeStubFromNSDateRef 3930 4540 +15.5% 0.87x (?)
String.data.Medium 90 101 +12.2% 0.89x (?)
 
Improvement OLD NEW DELTA RATIO
CSVParsing.Scalar 162 151 -6.8% 1.07x (?)

Code size: -O

Improvement OLD NEW DELTA RATIO
SortIntPyramids.o 9136 9040 -1.1% 1.01x

Performance (x86_64): -Osize

Regression OLD NEW DELTA RATIO
DictionaryKeysContainsNative 24 27 +12.5% 0.89x (?)
 
Improvement OLD NEW DELTA RATIO
ArrayAppendGenericStructs 2080 1130 -45.7% 1.84x (?)

Code size: -Osize

Performance (x86_64): -Onone

Regression OLD NEW DELTA RATIO
ClassArrayGetter2 4260 4650 +9.2% 0.92x (?)

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

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

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

should be fine.
LGTM

@lorentey lorentey merged commit d9b1489 into swiftlang:main Aug 16, 2021
@lorentey lorentey deleted the no-swaps branch August 16, 2021 19:43
lorentey added a commit that referenced this pull request Aug 16, 2021
Implements #38867 for ContiguousArray and ArraySlice -- these have the same unnecessary swapping logic.
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.

3 participants