Skip to content

[stdlib] Implement withContiguousStorageIfAvailable for raw buffer types #41288

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 4 commits into from
Feb 14, 2022

Conversation

glessard
Copy link
Contributor

@glessard glessard commented Feb 9, 2022

Until now, UnsafeRawBufferPointer and UnsafeMutableRawBufferPointer have not had an implementation of the withContiguousStorageIfAvailable and withContiguousMutableStorageIfAvailable functions, making a frequently-used fast path unavailable to those types.

The recently-landed SE-0333 (apple/swift#39529) means implementing the withContiguous{Mutable}StorageIfAvailable functions for the raw buffer types is now possible.

Resolves SR-15433 (rdar://84980367)

@glessard
Copy link
Contributor Author

glessard commented Feb 9, 2022

@swift-ci please smoke test linux platform

@glessard
Copy link
Contributor Author

glessard commented Feb 9, 2022

@swift-ci please test macOS platform

@glessard
Copy link
Contributor Author

glessard commented Feb 9, 2022

@swift-ci apple silicon benchmark

@glessard
Copy link
Contributor Author

glessard commented Feb 9, 2022

@swift-ci apple silicon benchmark

@swift-ci
Copy link
Contributor

Performance (arm64): -O

Regression OLD NEW DELTA RATIO
RawBuffer.copyContents 10 12 +20.0% 0.83x (?)
NSArray.bridged.objectAtIndex 193 218 +13.0% 0.89x (?)
ArrayPlusEqualArrayOfInt 170 190 +11.8% 0.89x (?)
ObjectiveCBridgeFromNSStringForced 1435 1560 +8.7% 0.92x (?)
 
Improvement OLD NEW DELTA RATIO
Data.init.Sequence.64kB.Count.I 3 2 -33.3% 1.50x (?)
Data.init.Sequence.2047B.Count.I 8 7 -12.5% 1.14x (?)
DataCreateMedium 1000 900 -10.0% 1.11x (?)
BufferFillFromSlice 13 12 -7.7% 1.08x (?)

Code size: -O

Performance (arm64): -Osize

Regression OLD NEW DELTA RATIO
RawBuffer.copyContents 10 11 +10.0% 0.91x (?)
RawBufferCopyBytes 11 12 +9.1% 0.92x (?)
NSArray.bridged.objectAtIndex 202 220 +8.9% 0.92x (?)
 
Improvement OLD NEW DELTA RATIO
ArrayAppendOptionals 430 400 -7.0% 1.07x (?)
ObjectiveCBridgeStubNSDataAppend 1180 1100 -6.8% 1.07x (?)

Code size: -Osize

Performance (arm64): -Onone

Regression OLD NEW DELTA RATIO
ArrayAppendToGeneric 170 190 +11.8% 0.89x (?)
RawBufferCopyBytes 11 12 +9.1% 0.92x (?)
 
Improvement OLD NEW DELTA RATIO
ParseFloat.Float.Exp 8 7 -12.5% 1.14x (?)

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: Macmini9,1
  Total Number of Cores: 8 (4 performance and 4 efficiency)
  Memory: 16 GB

@glessard
Copy link
Contributor Author

glessard commented Feb 11, 2022

I don't think any of these benchmark (performance) results are relevant. Thoughts, @lorentey?

@eeckstein
Copy link
Contributor

yes, it looks like noise.

Copy link
Member

@lorentey lorentey left a comment

Choose a reason for hiding this comment

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

This looks good!

Incidentally: I keep having a hard time grokking the nature of withMemoryRebound -- is rebinding memory semantically mutating the global state of a Swift program? Or is its effect localized somehow, to, say, a single thread? (I.e., is it really safe to call it from a nonmutating context, while some other parts of the program may be accessing the same memory, bound differently?)

@lorentey
Copy link
Member

RawBuffer.copyContents is the one result that may be worth a look. Afaict it goes through URBP._copyContents instead of this path, though, so...

@glessard
Copy link
Contributor Author

I looked at RawBuffer.copyContents, and it doesn't go through this path. Seems like a fluke.

@glessard glessard merged commit d870a9f into swiftlang:main Feb 14, 2022
@glessard glessard deleted the sr15433 branch February 14, 2022 22:37
grynspan added a commit to swiftlang/swift-testing that referenced this pull request Mar 22, 2024
`UnsafeRawBufferPointer` only gained an implementation of
`withContiguousStorageIfAvailable(_:)` with [SE-0333](https://github.com/apple/swift-evolution/blob/main/proposals/0333-with-memory-rebound.md)
and swiftlang/swift#41288. Because swift-testing can be
used on older versions of Darwin that don't have this implementation, one of our
unit tests now crashes there when calling
`FileHandle.write(_: some Sequence<UInt8>)`. This PR resolves the crash by
providing a concretely-typed overload that takes an instance of
`UnsafeRawBufferPointer`.

This code path is technically dead right now (it survives from an intermediate
implementation of #286) but because "writing bytes to a file" remains a useful
tool we'll probably need in the future, I'm not ripping it out just yet.
briancroom pushed a commit to swiftlang/swift-testing that referenced this pull request Mar 22, 2024
…309)

`UnsafeRawBufferPointer` only gained an implementation of
`withContiguousStorageIfAvailable(_:)` with
[SE-0333](https://github.com/apple/swift-evolution/blob/main/proposals/0333-with-memory-rebound.md)
and swiftlang/swift#41288. Because swift-testing can
be used on older versions of Darwin that don't have this implementation,
one of our unit tests now crashes there when calling
`FileHandle.write(_: some Sequence<UInt8>)`. This PR resolves the crash
by providing a concretely-typed overload that takes an instance of
`UnsafeRawBufferPointer`.

This code path is technically dead right now (it survives from an
intermediate implementation of #286) but because "writing bytes to a
file" remains a useful tool we'll probably need in the future, I'm not
ripping it out just yet.

### Checklist:

- [x] Code and documentation should follow the style of the [Style
Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md).
- [x] If public symbols are renamed or modified, DocC references should
be updated.
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