Skip to content

[stdlib] Span gardening #77688

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 4 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions stdlib/public/core/Span/RawSpan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ extension RawSpan {
@available(SwiftStdlib 6.1, *)
extension RawSpan {

//FIXME: mark closure parameter as non-escaping
/// Calls the given closure with a pointer to the underlying bytes of
/// the viewed contiguous storage.
///
Expand Down Expand Up @@ -633,7 +632,7 @@ extension RawSpan {
}
}

//MARK: one-sided slicing operations
//MARK: prefixes and suffixes
@_disallowFeatureSuppression(NonescapableTypes)
@available(SwiftStdlib 6.1, *)
extension RawSpan {
Expand Down
91 changes: 7 additions & 84 deletions stdlib/public/core/Span/Span.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ extension Span where Element: ~Copyable {
public init(
_unsafeElements buffer: borrowing UnsafeBufferPointer<Element>
) {
let baseAddress = buffer.baseAddress //FIXME: rdar://138665760
//FIXME: Workaround for https://github.com/swiftlang/swift/issues/77235
let baseAddress = buffer.baseAddress
_precondition(
((Int(bitPattern: baseAddress) &
(MemoryLayout<Element>.alignment &- 1)) == 0),
Expand Down Expand Up @@ -204,7 +205,8 @@ extension Span where Element: BitwiseCopyable {
public init(
_unsafeBytes buffer: borrowing UnsafeRawBufferPointer
) {
let baseAddress = buffer.baseAddress //FIXME: rdar://138665760
//FIXME: Workaround for https://github.com/swiftlang/swift/issues/77235
let baseAddress = buffer.baseAddress
_precondition(
((Int(bitPattern: baseAddress) &
(MemoryLayout<Element>.alignment &- 1)) == 0),
Expand Down Expand Up @@ -321,87 +323,6 @@ extension Span where Element: BitwiseCopyable {
}
}

@_disallowFeatureSuppression(NonescapableTypes)
@available(SwiftStdlib 6.1, *)
extension Span where Element: Equatable {

/// Returns a Boolean value indicating whether this and another span
/// contain equal elements in the same order.
///
/// - Parameters:
/// - other: A span to compare to this one.
/// - Returns: `true` if this sequence and `other` contain equivalent items,
/// using `areEquivalent` as the equivalence test; otherwise, `false.`
///
/// - Complexity: O(*m*), where *m* is the lesser of the length of the
/// sequence and the length of `other`.
@_disallowFeatureSuppression(NonescapableTypes)
@_alwaysEmitIntoClient
public func _elementsEqual(_ other: Self) -> Bool {
guard count == other.count else { return false }
if count == 0 { return true }

// This could be short-cut with a layout constraint
// where stride equals size, as long as there is
// at most 1 unused bit pattern, e.g.:
// if Element is BitwiseEquatable {
// return _swift_stdlib_memcmp(lhs.baseAddress, rhs.baseAddress, count) == 0
// }
if _pointer != other._pointer {
for o in 0..<count {
if self[unchecked: o] != other[unchecked: o] { return false }
}
}
return true
}

/// Returns a Boolean value indicating whether this span and a Collection
/// contain equal elements in the same order.
///
/// - Parameters:
/// - other: A Collection to compare to this span.
/// - Returns: `true` if this sequence and `other` contain equivalent items,
/// using `areEquivalent` as the equivalence test; otherwise, `false.`
///
/// - Complexity: O(*m*), where *m* is the lesser of the length of the
/// sequence and the length of `other`.
@_disallowFeatureSuppression(NonescapableTypes)
@_alwaysEmitIntoClient
public func _elementsEqual(_ other: some Collection<Element>) -> Bool {
let equal = other.withContiguousStorageIfAvailable {
_elementsEqual(Span(_unsafeElements: $0))
}
if let equal { return equal }

guard count == other.count else { return false }
if count == 0 { return true }

return _elementsEqual(AnySequence(other))
}

/// Returns a Boolean value indicating whether this span and a Sequence
/// contain equal elements in the same order.
///
/// - Parameters:
/// - other: A Sequence to compare to this span.
/// - Returns: `true` if this sequence and `other` contain equivalent items,
/// using `areEquivalent` as the equivalence test; otherwise, `false.`
///
/// - Complexity: O(*m*), where *m* is the lesser of the length of the
/// sequence and the length of `other`.
@_disallowFeatureSuppression(NonescapableTypes)
@_alwaysEmitIntoClient
public func _elementsEqual(_ other: some Sequence<Element>) -> Bool {
var offset = 0
for otherElement in other {
if offset >= count { return false }
if self[unchecked: offset] != otherElement { return false }
offset += 1
}
return offset == count
}
}

@_disallowFeatureSuppression(NonescapableTypes)
@available(SwiftStdlib 6.1, *)
extension Span where Element: ~Copyable {
Expand Down Expand Up @@ -701,7 +622,9 @@ extension Span where Element: BitwiseCopyable {
public func withUnsafeBytes<E: Error, Result: ~Copyable>(
_ body: (_ buffer: UnsafeRawBufferPointer) throws(E) -> Result
) throws(E) -> Result {
try RawSpan(_elements: self).withUnsafeBytes(body)
try body(
.init(start: _pointer, count: _count * MemoryLayout<Element>.stride)
)
}
}

Expand Down