-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SE-0101] Implement: Reconfiguring sizeof and related functions into a unified MemoryLayout struct - Part 1 #3854
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
373e7ff
to
924a504
Compare
@rintaro To get CI coverage and avoid lockstep issues, you could add old declarations as deprecated. Then we can independently land fixes to |
@gribozavr OK, I'll try it. |
@@ -114,7 +114,7 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable { | |||
/// | |||
/// - parameter buffer: The buffer of bytes to append. The size is calculated from `SourceType` and `buffer.count`. | |||
public mutating func append<SourceType>(_ buffer : UnsafeBufferPointer<SourceType>) { | |||
self.append(UnsafePointer(buffer.baseAddress!), count: buffer.count * sizeof(SourceType.self)) | |||
self.append(UnsafePointer(buffer.baseAddress!), count: buffer.count * MemoryLayout<SourceType>.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.
This code was changed in the master
branch, would you mind rebasing?
Also, could you change sizeof
to strideof
? Using sizeof
here can cause bugs.
924a504
to
373e7ff
Compare
public static func of(_ candidate : @autoclosure () -> T) -> MemoryLayout<T>.Type { | ||
return MemoryLayout.init(candidate).dynamicType | ||
} | ||
} |
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 we can implement it simpler:
extension MemoryLayout {
public static func of(_ candidate: @autoclosure () -> T) -> MemoryLayout<T>.Type {
return MemoryLayout<T>.self
}
}
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 just copy pasted it from the proposal. 😓
I'll test it.
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.
It worked.
I reviewed the patch, LGTM as written, but I will do another pass after you rebase. |
MiscDiagnostics.cpp changes LGTM! |
373e7ff
to
2128555
Compare
buffer.baseAddress!.withMemoryRebound(to: UInt8.self, capacity: buffer.count * strideof(SourceType.self)) { | ||
self.append($0, count: buffer.count * sizeof(SourceType.self)) | ||
buffer.baseAddress!.withMemoryRebound(to: UInt8.self, capacity: buffer.count * MemoryLayout<SourceType>.stride) { | ||
self.append($0, count: buffer.count * MemoryLayout<SourceType>.stride) |
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.
@gribozavr You didn't mention this sizeof
-> stride
change. This should be .size
?
This was introduced in recent change.(https://github.com/apple/swift/blob/0b75ee975e55ffa7c8a69a0f076f33ff82b64f44/stdlib/public/SDK/Dispatch/Data.swift#L118) @atrick ?
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.
This is supposed to be .stride on both modified lines.
I forgot to change the original code to strideof() and hadn't pushed my fix yet.
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.
@atrick
Thank you! If you have more fix, feel free to push them first.
I'd happy to rebase on it.
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 won't push my fix, it makes more sense as part of your PR. Thanks.
2128555
to
ae977ab
Compare
@swift-ci Please test |
IIUC, the core team decision for SE-0101 was that MemoryLayout should be implemented as an enum with no cases. I imagine that this also means the |
As for If we decide to drop it, the implementation of migration fix-it cannot be fully implemented, or will be very complicated. Contrived example here: func fn<S: Sequence>(s: S) {
var i = s.makeIterator()
let size = sizeofValue(i.next()!)
// Compare:
// let size = MemoryLayout<S.Iterator.Element>.size; _ = i.next()!
// let size = MemoryLayout.of(i.next()!).size We have to actually look into the type of the value. |
|
||
extension MemoryLayout { | ||
@_transparent | ||
public static func of(_ candidate : @autoclosure () -> T) -> MemoryLayout<T>.Type { |
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.
candidate
is not needed here.
@rintaro Thanks again for the implementation. I just checked with @dabrahams and he said that the core team did not approve the You should keep the uses of this underscored Does this sound good? Sorry for not providing this feedback earlier! |
@gribozavr no problem 👍 I'll update the PR soon. |
@gribozavr Can I split the changes to |
@rintaro Absolutely! |
…MemoryLayout struct As of now: * old APIs are just marked as `deprecated` not `unavaiable`. To make it easier to co-operate with other toolchain repos. * Value variant of API is implemented as public @Private `_ofInstance(_:)`.
9e9149f
to
06603c1
Compare
@gribozavr Updated. |
@swift-ci Please smoke test |
Since |
Since all of current usages of @swift-ci Please benchmark |
Build comment file: |
@rintaro There should be no difference with and without autoclosure. I will look at the generated code right now. |
@@ -149,7 +149,7 @@ public struct DispatchData : RandomAccessCollection, _ObjectiveCBridgeable { | |||
|
|||
/// Copy the contents of the data into a buffer. | |||
/// | |||
/// This function copies the bytes in `range` from the data into the buffer. If the count of the `range` is greater than `sizeof(DestinationType) * buffer.count` then the first N bytes will be copied into the buffer. | |||
/// This function copies the bytes in `range` from the data into the buffer. If the count of the `range` is greater than `MemoryLayout<DestinationType>.size * buffer.count` then the first N bytes will be copied into 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.
.stride
. Please make this change as a separate commit so that it does not invalidate the CI results. (This is comment-only, so it does not matter for the purposes of CI.)
@rintaro The code as it is now, with the autoclosure, is optimal. The optimizer eliminates the closure. Let's keep the autoclosure. |
Build comment file: |
@swift-ci Please test |
The changes look good to me, but I want to get a full CI run, with all the platforms and downstream projects. @rintaro Please submit the C++ code changes and the comment change I requested as separate pull requests, thank you! |
Linux failures are unrelated. Merging. |
@gribozavr Can I ask you to update |
Implement SE-0101
Currently
sizeof
family functions are simply marked asdeprecated
.no needswift-corelibs-xctest
only in comments.swift-xcode-playground-support
unavailable
, and turn on migration fix-it.Still need more test case for migration support.
CC:
@tkremenek
Are you still accepting new implementation?
@gribozavr
Could you review stdlib part? Added
stdlib/public/core/MemoryLayout.swift
@jrose-apple, @DougGregor
Could you review fix-it part?Removed from this PR. I will split them into another PR.Before merging this pull request to apple/swift repository:
Triggering Swift CI
The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:
Smoke Testing
A smoke test on macOS does the following:
device standard libraries are not built.
version of these tests are not run.
A smoke test on Linux does the following:
tests are not run.
Validation Testing
Lint Testing
Note: Only members of the Apple organization can trigger swift-ci.