Skip to content

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

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions stdlib/public/core/UnsafeRawBufferPointer.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,21 @@ extension UnsafeRawBufferPointer.Iterator: IteratorProtocol, Sequence {
public mutating func next() -> UInt8? {
if _position == _end { return nil }
Copy link
Contributor

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.


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!)
Copy link
Contributor

Choose a reason for hiding this comment

The 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 _position <= _end? That would give us the same assurances, but IMO would be a bit cleaner; we wouldn't need to talk about 'validly constructed buffer pointer's any more, more like 'validly constructed iterators'. Additionally, it may improve performance in debug builds, which is also important.

Copy link
Contributor

Choose a reason for hiding this comment

The 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)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stephentyrone Would you like a != nil _debugPrecondition as well?

Copy link
Contributor

Choose a reason for hiding this comment

The 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
}
}
Expand Down