From 214d75cb737c2514f5a70755b8bedbc1933f3cba Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 5 Mar 2026 15:09:53 -0800 Subject: [PATCH] =?UTF-8?q?BorrowingSequence:=20estimatedCount=20=E2=9F=B9?= =?UTF-8?q?=20underestimatedCount?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit EstimatedCount is largely pointless. Knowing the exact count does enable a fast negative path for `elementsEqual`, but we can otherwise make do with just the (much simpler to understand/deal with) `underestimatedCount` that already exists on `Sequence`. --- .../RigidArray/RigidArray+Container.swift | 4 +- .../RigidDictionary+Indices.swift | 4 +- .../RigidSet/RigidSet+BorrowingSequence.swift | 4 +- .../UniqueArray/UniqueArray+Container.swift | 5 +- .../UniqueSet+BorrowingSequence.swift | 4 +- ...rowingSequence+Standard Conformances.swift | 54 +++++++------------ .../BorrowingSequence+Utilities.swift | 10 ---- .../Protocols/BorrowingSequence.swift | 33 +----------- .../Protocols/Container.swift | 2 +- .../RigidDictionaryTests.swift | 2 +- .../ConformanceCheckers/CheckContainer.swift | 10 +--- .../MinimalTypes/StaccatoContainer.swift | 4 +- 12 files changed, 32 insertions(+), 104 deletions(-) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift index eb8228f3..cac42c03 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Container.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Container.swift @@ -22,9 +22,7 @@ extension RigidArray: BorrowingSequence where Element: ~Copyable { public typealias BorrowingIterator = Span.BorrowingIterator @inlinable - public var estimatedCount: EstimatedCount { - .exactly(count) - } + public var underestimatedCount: Int { count } @_alwaysEmitIntoClient @inline(__always) diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Indices.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Indices.swift index fe86158f..5fdd9207 100644 --- a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Indices.swift +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Indices.swift @@ -65,9 +65,7 @@ where Key: ~Copyable, Value: ~Copyable } @inlinable - public var estimatedCount: EstimatedCount { - .exactly(self._base.value.count) - } + public var underestimatedCount: Int { self._base.value.count } @_lifetime(copy self) public func makeBorrowingIterator() -> BorrowingIterator { diff --git a/Sources/BasicContainers/RigidSet/RigidSet+BorrowingSequence.swift b/Sources/BasicContainers/RigidSet/RigidSet+BorrowingSequence.swift index 07bcefc8..f262a453 100644 --- a/Sources/BasicContainers/RigidSet/RigidSet+BorrowingSequence.swift +++ b/Sources/BasicContainers/RigidSet/RigidSet+BorrowingSequence.swift @@ -18,9 +18,7 @@ import ContainersPreview @available(SwiftStdlib 5.0, *) extension RigidSet: BorrowingSequence where Element: ~Copyable { @inlinable - public var estimatedCount: EstimatedCount { - .exactly(count) - } + public var underestimatedCount: Int { count } @inlinable public func _customContainsEquatableElement( diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift index fcd03511..b633723f 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Container.swift @@ -21,9 +21,8 @@ import ContainersPreview extension UniqueArray: BorrowingSequence where Element: ~Copyable { public typealias BorrowingIterator = RigidArray.BorrowingIterator - public var estimatedCount: EstimatedCount { - .exactly(count) - } + @inlinable + public var underestimatedCount: Int { count } @_alwaysEmitIntoClient @inline(__always) diff --git a/Sources/BasicContainers/UniqueSet/UniqueSet+BorrowingSequence.swift b/Sources/BasicContainers/UniqueSet/UniqueSet+BorrowingSequence.swift index fec0f9d5..2540d1b5 100644 --- a/Sources/BasicContainers/UniqueSet/UniqueSet+BorrowingSequence.swift +++ b/Sources/BasicContainers/UniqueSet/UniqueSet+BorrowingSequence.swift @@ -21,9 +21,7 @@ extension UniqueSet: BorrowingSequence where Element: ~Copyable { public typealias BorrowingIterator = RigidSet.BorrowingIterator @inlinable - public var estimatedCount: EstimatedCount { - .exactly(count) - } + public var underestimatedCount: Int { count } @inlinable public func _customContainsEquatableElement( diff --git a/Sources/ContainersPreview/Protocols/BorrowingSequence+Standard Conformances.swift b/Sources/ContainersPreview/Protocols/BorrowingSequence+Standard Conformances.swift index d43216c7..02631b8b 100644 --- a/Sources/ContainersPreview/Protocols/BorrowingSequence+Standard Conformances.swift +++ b/Sources/ContainersPreview/Protocols/BorrowingSequence+Standard Conformances.swift @@ -18,11 +18,6 @@ extension Span: BorrowingSequence where Element: ~Copyable { // we'll need to rather introduce a type more like `RigidArray.BorrowingIterator`. public typealias BorrowingIterator = Self - @inlinable - public var estimatedCount: EstimatedCount { - .exactly(count) - } - @_lifetime(copy self) @inlinable public func makeBorrowingIterator() -> Span { @@ -34,11 +29,6 @@ extension Span: BorrowingSequence where Element: ~Copyable { extension MutableSpan: BorrowingSequence where Element: ~Copyable { public typealias BorrowingIterator = Span.BorrowingIterator - @inlinable - public var estimatedCount: EstimatedCount { - .exactly(count) - } - @_lifetime(borrow self) @inlinable public func makeBorrowingIterator() -> Span { @@ -50,11 +40,6 @@ extension MutableSpan: BorrowingSequence where Element: ~Copyable { extension OutputSpan: BorrowingSequence where Element: ~Copyable { public typealias BorrowingIterator = Span.BorrowingIterator - @inlinable - public var estimatedCount: EstimatedCount { - .exactly(count) - } - @_lifetime(borrow self) @inlinable public func makeBorrowingIterator() -> Span { @@ -66,11 +51,6 @@ extension OutputSpan: BorrowingSequence where Element: ~Copyable { extension InputSpan: BorrowingSequence where Element: ~Copyable { public typealias BorrowingIterator = Span.BorrowingIterator - @inlinable - public var estimatedCount: EstimatedCount { - .exactly(count) - } - @_lifetime(borrow self) @inlinable public func makeBorrowingIterator() -> Span { @@ -83,9 +63,7 @@ extension Array: BorrowingSequence { public typealias BorrowingIterator = Span.BorrowingIterator @inlinable - public var estimatedCount: EstimatedCount { - .exactly(count) - } + public var underestimatedCount: Int { count } @_lifetime(borrow self) @inlinable @@ -132,14 +110,18 @@ where Bound: Strideable, Bound.Stride: SignedInteger } } - @inlinable - public var estimatedCount: EstimatedCount { - if let count = Int(exactly: lowerBound.distance(to: upperBound)) { - return .exactly(count) + @_alwaysEmitIntoClient + @_transparent + package var _borrowingUnderestimatedCount: Int { + guard let count = Int(exactly: lowerBound.distance(to: upperBound)) else { + return 0 } - return .unknown + return count } - + + @inlinable + public var underestimatedCount: Int { _borrowingUnderestimatedCount } + @inlinable public func makeBorrowingIterator() -> BorrowingIterator { BorrowingIterator(self) @@ -184,14 +166,18 @@ where Bound: Strideable, Bound.Stride: SignedInteger } } - @inlinable - public var estimatedCount: EstimatedCount { + @_alwaysEmitIntoClient + @_transparent + package var _borrowingUnderestimatedCount: Int { guard let distance = Int(exactly: lowerBound.distance(to: upperBound)) - else { return .unknown } + else { return 0 } let advanced = distance.addingReportingOverflow(1) - guard !advanced.overflow else { return .unknown } - return .exactly(advanced.partialValue) + guard !advanced.overflow else { return 0 } + return advanced.partialValue } + + @inlinable + public var underestimatedCount: Int { _borrowingUnderestimatedCount } @inlinable public func makeBorrowingIterator() -> BorrowingIterator { diff --git a/Sources/ContainersPreview/Protocols/BorrowingSequence+Utilities.swift b/Sources/ContainersPreview/Protocols/BorrowingSequence+Utilities.swift index 1bd60bfd..93782006 100644 --- a/Sources/ContainersPreview/Protocols/BorrowingSequence+Utilities.swift +++ b/Sources/ContainersPreview/Protocols/BorrowingSequence+Utilities.swift @@ -45,10 +45,6 @@ extension BorrowingSequence where Self: ~Copyable & ~Escapable { >( _ other: borrowing Other, ) -> Bool where Element: Equatable { - guard self.estimatedCount._mayBeEqual(to: other.estimatedCount) else { - return false - } - var it1 = self.makeBorrowingIterator() var it2 = other.makeBorrowingIterator() while true { @@ -106,12 +102,6 @@ extension BorrowingSequence where Self: ~Copyable & ~Escapable { _ other: borrowing Other, by areEquivalent: (borrowing Element, borrowing Other.Element) throws(E) -> Bool ) throws(E) -> Bool { - switch (self.estimatedCount, other.estimatedCount) { - case let (.exactly(a), .exactly(b)): - guard a == b else { return false } - default: - break - } var it1 = self.makeBorrowingIterator() var it2 = other.makeBorrowingIterator() while true { diff --git a/Sources/ContainersPreview/Protocols/BorrowingSequence.swift b/Sources/ContainersPreview/Protocols/BorrowingSequence.swift index 16678a83..f963de7a 100644 --- a/Sources/ContainersPreview/Protocols/BorrowingSequence.swift +++ b/Sources/ContainersPreview/Protocols/BorrowingSequence.swift @@ -11,32 +11,12 @@ #if compiler(>=6.2) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW -public enum EstimatedCount { - case infinite - case exactly(Int) - case unknown - - @inlinable - package func _mayBeEqual(to other: EstimatedCount) -> Bool { - switch (self, other) { - case let (.exactly(a), .exactly(b)): - return a == b - case (.exactly(_), .infinite): - return false - case (.infinite, .exactly(_)): - return false - default: - return true - } - } -} - @available(SwiftStdlib 5.0, *) public protocol BorrowingSequence: ~Copyable, ~Escapable { associatedtype Element: ~Copyable associatedtype BorrowingIterator: BorrowingIteratorProtocol & ~Copyable & ~Escapable - var estimatedCount: EstimatedCount { get } + var underestimatedCount: Int { get } @_lifetime(borrow self) borrowing func makeBorrowingIterator() -> BorrowingIterator @@ -49,16 +29,7 @@ public protocol BorrowingSequence: ~Copyable, ~Escapable { @available(SwiftStdlib 5.0, *) extension BorrowingSequence where Self: ~Copyable & ~Escapable { @inlinable - public var underestimatedCount: Int { - switch estimatedCount { - case .infinite: - Int.max - case .exactly(let c): - c - case .unknown: - 0 - } - } + public var underestimatedCount: Int { 0 } @inlinable public func _customContainsEquatableElement(_ element: borrowing Element) -> Bool? { diff --git a/Sources/ContainersPreview/Protocols/Container.swift b/Sources/ContainersPreview/Protocols/Container.swift index a413dbcf..1e0ca137 100644 --- a/Sources/ContainersPreview/Protocols/Container.swift +++ b/Sources/ContainersPreview/Protocols/Container.swift @@ -378,7 +378,7 @@ extension Container where Self: ~Copyable & ~Escapable { @available(SwiftStdlib 5.0, *) extension Container where Self: ~Copyable & ~Escapable { @_transparent - public var estimatedCount: EstimatedCount { .exactly(count) } + public var underestimatedCount: Int { count } } diff --git a/Tests/BasicContainersTests/RigidDictionaryTests.swift b/Tests/BasicContainersTests/RigidDictionaryTests.swift index efc9b4ef..827bbb0e 100644 --- a/Tests/BasicContainersTests/RigidDictionaryTests.swift +++ b/Tests/BasicContainersTests/RigidDictionaryTests.swift @@ -358,7 +358,7 @@ class RigidDictionaryTests: CollectionTestCase { } let oldValue = d.removeValue(forKey: tracker.instance(for: key)) - expectNotNil(oldValue) { + expectNotNil(oldValue, "\(key)") { expectEqual($0.payload, "\(key)") } expectEqual(d.count, count - 1) diff --git a/Tests/_CollectionsTestSupport/ConformanceCheckers/CheckContainer.swift b/Tests/_CollectionsTestSupport/ConformanceCheckers/CheckContainer.swift index e748975d..bd8dbcb5 100644 --- a/Tests/_CollectionsTestSupport/ConformanceCheckers/CheckContainer.swift +++ b/Tests/_CollectionsTestSupport/ConformanceCheckers/CheckContainer.swift @@ -52,15 +52,7 @@ public func checkIterable< let expectedContents = Array(expectedContents) - let estimatedCount = iterable.estimatedCount - switch estimatedCount { - case .exactly(let c): - expectEqual(c, expectedContents.count) - case .infinite: - expectFailure() - case .unknown: - break - } + expectLessThanOrEqual(iterable.underestimatedCount, expectedContents.count) // Check that the spans seem plausibly sized and that the indices are monotonic. let spanShapes: [Range] = { diff --git a/Tests/_CollectionsTestSupport/MinimalTypes/StaccatoContainer.swift b/Tests/_CollectionsTestSupport/MinimalTypes/StaccatoContainer.swift index bdb9168e..f50b2133 100644 --- a/Tests/_CollectionsTestSupport/MinimalTypes/StaccatoContainer.swift +++ b/Tests/_CollectionsTestSupport/MinimalTypes/StaccatoContainer.swift @@ -115,9 +115,7 @@ public struct _StaccatoIndex: Comparable { extension StaccatoContainer: BorrowingSequence where Element: ~Copyable { public typealias BorrowingIterator = _StaccatoBorrowingIterator // FIXME rdar://150240032 - public var estimatedCount: EstimatedCount { - .exactly(count) - } + public var underestimatedCount: Int { count } public func makeBorrowingIterator() -> BorrowingIterator { BorrowingIterator(contents: _contents.span, params: _params)