-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[stdlib] Fix bug in small string uninitialized init #36667
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
Fix a bug and enforce the invariant that all bits between the last code unit and the descriminator in a small string should be unset.
let len = try f(rawBufPtr) | ||
UnsafeMutableRawBufferPointer( | ||
rebasing: rawBufPtr[len...] | ||
).initializeMemory(as: UInt8.self, repeating: 0) |
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 is this a proper use of initializeMemory
? It's basically a convenient way to mask off our unused bytes and make sure they're set to 0.
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.
@milseman Now that I'm looking at this again, I think this needs to rebind the memory; otherwise, after initializing memory as UInt8
, portions of the memory are bound simultaneously as RawBitPattern.self
and UInt8.self
😕
let count = self.count
let len = try withUnsafeMutableBytes(of: &self._storage) {
(rawBufPtr: UnsafeMutableRawBufferPointer) -> Int in
let len = try f(rawBufPtr)
rawBufPtr.bindMemory(to: UInt8.self, capacity: count)
defer {
_ = rawBufPtr.bindMemory(to: RawBitPattern.self, capacity: 1)
}
UnsafeMutableRawBufferPointer(
rebasing: rawBufPtr[len...]
).initializeMemory(as: UInt8.self, repeating: 0)
return len
}
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 don't see any API for initializing raw bytes without using a typed pointer. You can do the silly rebinding thing above or just write your own raw initialize that doesn't care about the in-memory type
var nextPtr = self
for _ in 0..<count {
Builtin.initialize(repeatedValue, nextPtr._rawValue)
nextPtr += MemoryLayout<T>.stride
}
@swift-ci please smoke test |
@swift-ci please benchmark |
Performance: -O
Code size: -OPerformance: -Osize
Code size: -OsizePerformance: -Onone
Code size: -swiftlibsHow to read the dataThe 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 Noise: Sometimes the performance results (not code size!) contain false Hardware Overview
|
@swift-ci please smoke test macOS platform |
2 similar comments
@swift-ci please smoke test macOS platform |
@swift-ci please smoke test macOS platform |
@swift-ci please smoke test macOS platform. |
Waiting on swiftlang/llvm-project#2770 |
@swift-ci please smoke test |
Seriously... @swift-ci please smoke test |
@swift-ci please smoke test linux platform |
@swift-ci please smoke test windows platform |
1 similar comment
@swift-ci please smoke test windows platform |
Fix a bug and enforce the invariant that all bits between the last code unit
and the descriminator in a small string should be unset.