-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Remove checks in UR[M]BP.Iterator.next() #62965
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,8 +148,21 @@ extension UnsafeRawBufferPointer.Iterator: IteratorProtocol, Sequence { | |
public mutating func next() -> UInt8? { | ||
if _position == _end { return nil } | ||
|
||
let result = _position!.load(as: UInt8.self) | ||
_position! += 1 | ||
// We can do an unchecked unwrap here by borrowing invariants from the pointer. | ||
// For a validly constructed buffer pointer, the only way _position can be nil is | ||
// if _end is also nil. We checked that case above. Thus, we can safely do an | ||
// unchecked unwrap here. | ||
// | ||
// Additionally, validly constructed buffer pointers also have an _end that is | ||
// strictly greater than or equal to _position, and so we do not need to do checked | ||
// arithmetic here as we cannot possibly overflow. | ||
// | ||
// We check these invariants in debug builds to defend against invalidly constructed | ||
// pointers. | ||
_debugPrecondition(_position! < _end!) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't this be hoisted in to the iterator's initializer, as a check that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's value in validating invariants outside of the initializer, to detect bugs that corrupt them sooner rather than later. Looking at the codegen, this doesn't cost us much, so it should probably stick around (but I would prefer they be _unsafelyUnwrappedUnchecked) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @stephentyrone Would you like a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'm fine with it as you have it. |
||
let position = _position._unsafelyUnwrappedUnchecked | ||
let result = position.load(as: UInt8.self) | ||
_position = position + 1 | ||
return result | ||
} | ||
} | ||
|
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 should compile to a single comparison, but doesn't. I'm opening an LLVM bug to see what we can do about that.