-
Notifications
You must be signed in to change notification settings - Fork 10.5k
nonmutating swapAt implementations for UnsafeMutable(Raw)BufferPointer #13071
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@@ -492,6 +492,29 @@ public struct Unsafe${Mutable}RawBufferPointer | |||
% end # mutable | |||
} | |||
|
|||
%if Mutable: | |||
/// Exchanges the byte values at the specified offsets | |||
/// in the memory region |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have a period (.) at the end of this sentence.
/// Exchanges the byte values at the specified offsets | ||
/// in the memory region | ||
/// | ||
/// Both parameters must be valid indices, and not |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...valid indices in the memory region?
let pi = (_position! + i) | ||
let pj = (_position! + j) | ||
let tmp = pi.load(fromByteOffset: 0, as: UInt8.self) | ||
pi.copyBytes(from: pj, count: MemoryLayout<UInt8>.size) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not count: 1
? If UInt8
is not one byte, then you've broken the semantic guarantees of the method anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not count: 1?
True. Using literal 1
would me more consistent with the pointer arithmetic above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think using an explicitly typed value from MemoryLayout
is more consistent with the load
and storeBytes
methods, which must be typed.
@@ -492,6 +492,29 @@ public struct Unsafe${Mutable}RawBufferPointer | |||
% end # mutable | |||
} | |||
|
|||
%if Mutable: | |||
/// Exchanges the byte values at the specified offsets | |||
/// in the memory region. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: What is the difference in meaning between this “in the memory region” and the documentation for the other method which says, “of the buffer”?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"memory region" is used regularly in the UnsafeRawPointer
documentation, which I interpret as emphasis on the untyped nature of raw pointers; I didn't use it well here.
b1aae41
to
72f1dac
Compare
@swift-ci please test |
b2c2769
to
a0722b2
Compare
@atrick I realized that one thing these implementations lose is any debug-mode bounds checking. (The default implementation gets it for free by using the subscript). Would it be better to add some |
Thanks. |
13b3ab4
to
829977f
Compare
829977f
to
0c95f90
Compare
@atrick Sorry to bother again, but this seems to have fallen off everyone's radar… |
@swift-ci test. |
Thanks! |
@glessard thanks for bring this to my attention again. It should have been merged a long time ago! |
I thought for a moment it was waiting for 4.1, but then it didn't look like it! |
@glessard We need a motivating benchmark for a change like that. If a motivating benchmark exists then it's certainly relevant. |
@atrick Is it sufficient to start a PR and benchmark it from there? I imagine that if the change is not positive then it would equally justify removing the specialized forms from |
Hopefully there's already a benchmark that relies on the non-raw pointer specialization. I mean that a PR for raw pointer specialization needs to include such a benchmark. I suspect that we don't already have one. The benchmarks only cover things that have already been deliberately optimized. |
Now I see what you meant. I'll see what I can do, then. |
The
BufferPointer
types currently useMutableCollection
's default implementation ofswapAt
, which is unnecessarily mutable and, in the case ofUnsafeMutableBufferPointer
does nothing to minimize ARC traffic. This PR introduces non-mutating implementations ofswapAt
forUnsafeMutableBufferPointer
andUnsafeMutableRawBufferPointer
, in the spirit of #12504. If this gets merged some new "un-mutated var" warnings may appear, but source code would otherwise be unaffected.