From f2914d4fb498b33ef9e00d4efb3b6177b1eb3a52 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 12 Aug 2026 12:25:12 -0700 Subject: [PATCH 1/6] Eliminate some duplicate bounds checks for UniqueArray --- .../RigidArray/RigidArray+Append.swift | 78 ++++++++++++++----- .../UniqueArray/UniqueArray+Append.swift | 17 ++-- 2 files changed, 66 insertions(+), 29 deletions(-) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift index 7db3487f..23b43076 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift @@ -20,6 +20,13 @@ import ContainersPreview @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { + @inlinable + @unsafe + internal mutating func _appendUnchecked(_ item: consuming Element) { + unsafe _storage.initializeElement(at: _count, to: item) + _count &+= 1 + } + /// Adds an element to the end of the array. /// /// If the array does not have sufficient capacity to hold any more elements, @@ -31,8 +38,7 @@ extension RigidArray where Element: ~Copyable { @inlinable public mutating func append(_ item: consuming Element) { precondition(!isFull, "RigidArray capacity overflow") - unsafe _storage.initializeElement(at: _count, to: item) - _count &+= 1 + unsafe _appendUnchecked(item) } /// Adds an element to the end of the array, if possible. @@ -48,7 +54,7 @@ extension RigidArray where Element: ~Copyable { @inlinable public mutating func pushLast(_ item: consuming Element) -> Element? { if isFull { return item } - append(item) + unsafe _appendUnchecked(item) return nil } } @@ -93,6 +99,17 @@ extension RigidArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { + @inlinable + @unsafe + internal mutating func _appendUnchecked( + moving items: UnsafeMutableBufferPointer + ) { + guard items.count > 0 else { return } + let c = unsafe _freeSpace._moveInitializePrefix(from: items) + assert(c == items.count) + _count &+= items.count + } + /// Moves the elements of a buffer to the end of this array, leaving the /// buffer uninitialized. /// @@ -109,10 +126,7 @@ extension RigidArray where Element: ~Copyable { moving items: UnsafeMutableBufferPointer ) { precondition(items.count <= freeCapacity, "RigidArray capacity overflow") - guard items.count > 0 else { return } - let c = unsafe _freeSpace._moveInitializePrefix(from: items) - assert(c == items.count) - _count &+= items.count + unsafe _appendUnchecked(moving: items) } #if UnstableContainersPreview @@ -139,6 +153,19 @@ extension RigidArray where Element: ~Copyable { } #endif + @_alwaysEmitIntoClient + @unsafe + internal mutating func _appendUnchecked( + moving items: inout OutputSpan + ) { + // FIXME: Remove this when `OutputSpan` starts conforming to RangeReplaceableContainer + items.withUnsafeMutableBufferPointer { buffer, count in + let source = buffer._extracting(first: count) + unsafe _appendUnchecked(moving: source) + count = 0 + } + } + /// Moves the elements of an output span to the end of this array, leaving the /// span empty. /// @@ -153,12 +180,8 @@ extension RigidArray where Element: ~Copyable { public mutating func append( moving items: inout OutputSpan ) { - // FIXME: Remove this when `OutputSpan` starts conforming to RangeReplaceableContainer - items.withUnsafeMutableBufferPointer { buffer, count in - let source = buffer._extracting(first: count) - unsafe self.append(moving: source) - count = 0 - } + precondition(items.count <= freeCapacity, "RigidArray capacity overflow") + _appendUnchecked(moving: &items) } #if !UnstableContainersPreview @@ -186,6 +209,17 @@ extension RigidArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension RigidArray { + @_alwaysEmitIntoClient + @unsafe + internal mutating func _appendUnchecked( + copying newElements: UnsafeBufferPointer + ) { + guard newElements.count > 0 else { return } + unsafe _freeSpace.baseAddress.unsafelyUnwrapped.initialize( + from: newElements.baseAddress.unsafelyUnwrapped, count: newElements.count) + _count &+= newElements.count + } + /// Copies the elements of a buffer to the end of this array. /// /// If the array does not have sufficient capacity to hold all items in the @@ -203,10 +237,7 @@ extension RigidArray { precondition( newElements.count <= freeCapacity, "RigidArray capacity overflow") - guard newElements.count > 0 else { return } - unsafe _freeSpace.baseAddress.unsafelyUnwrapped.initialize( - from: newElements.baseAddress.unsafelyUnwrapped, count: newElements.count) - _count &+= newElements.count + unsafe _appendUnchecked(copying: newElements) } /// Copies the elements of a buffer to the end of this array. @@ -226,6 +257,14 @@ extension RigidArray { unsafe self.append(copying: UnsafeBufferPointer(items)) } + @_alwaysEmitIntoClient + @unsafe + internal mutating func _appendUnchecked(copying items: Span) { + items.withUnsafeBufferPointer { source in + unsafe _appendUnchecked(copying: source) + } + } + /// Copies the elements of a span to the end of this array. /// /// If the array does not have sufficient capacity to hold all items in the @@ -237,9 +276,8 @@ extension RigidArray { /// - Complexity: O(`newElements.count`) @_alwaysEmitIntoClient public mutating func append(copying items: Span) { - items.withUnsafeBufferPointer { source in - unsafe self.append(copying: source) - } + precondition(items.count <= freeCapacity, "RigidArray capacity overflow") + _appendUnchecked(copying: items) } @_alwaysEmitIntoClient diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift index defdf685..7afc0961 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift @@ -32,7 +32,7 @@ extension UniqueArray where Element: ~Copyable { @inlinable public mutating func append(_ item: consuming Element) { _ensureFreeCapacity(1) - _storage.append(item) + unsafe _storage._appendUnchecked(item) } } @@ -89,7 +89,7 @@ extension UniqueArray where Element: ~Copyable { moving items: UnsafeMutableBufferPointer ) { _ensureFreeCapacity(items.count) - _storage.append(moving: items) + _storage._appendUnchecked(moving: items) } #if UnstableContainersPreview @@ -129,7 +129,7 @@ extension UniqueArray where Element: ~Copyable { moving items: inout OutputSpan ) { _ensureFreeCapacity(items.count) - _storage.append(moving: &items) + unsafe _storage._appendUnchecked(moving: &items) } #if !UnstableContainersPreview @@ -218,7 +218,7 @@ extension UniqueArray { copying newElements: UnsafeBufferPointer ) { _ensureFreeCapacity(newElements.count) - unsafe _storage.append(copying: newElements) + unsafe _storage._appendUnchecked(copying: newElements) } /// Copies the elements of a buffer to the end of this array. @@ -254,7 +254,7 @@ extension UniqueArray { @_alwaysEmitIntoClient public mutating func append(copying newElements: Span) { _ensureFreeCapacity(newElements.count) - _storage.append(copying: newElements) + unsafe _storage._appendUnchecked(copying: newElements) } #if compiler(>=6.4) && UnstableContainersPreview @@ -272,7 +272,7 @@ extension UniqueArray { let span = try it.nextSpan() if span.isEmpty { break } _ensureFreeCapacity(span.count) - _storage.append(copying: span) + unsafe _storage._appendUnchecked(copying: span) } } // FIXME: Add _append(copyingContainer:), forwarding to the same method on RigidArray @@ -323,7 +323,7 @@ extension UniqueArray { public mutating func append(copying newElements: some Sequence) { let done: Void? = newElements.withContiguousStorageIfAvailable { buffer in _ensureFreeCapacity(buffer.count) - unsafe _storage.append(copying: buffer) + unsafe _storage._appendUnchecked(copying: buffer) return } if done != nil { return } @@ -331,8 +331,7 @@ extension UniqueArray { _ensureFreeCapacity(newElements.underestimatedCount) var it = _storage._append(prefixOf: newElements) while let item = it.next() { - _ensureFreeCapacity(1) - _storage.append(item) + self.append(item) } } From 4fd692f00bd4b0162e54d874d617efe7f46e8d2e Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 12 Aug 2026 13:29:24 -0700 Subject: [PATCH 2/6] Apply review suggestions --- .../RigidArray/RigidArray+Append.swift | 32 ++++++------------- .../UniqueArray/UniqueArray+Append.swift | 10 ++++-- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift index 23b43076..8a0744d8 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift @@ -153,19 +153,6 @@ extension RigidArray where Element: ~Copyable { } #endif - @_alwaysEmitIntoClient - @unsafe - internal mutating func _appendUnchecked( - moving items: inout OutputSpan - ) { - // FIXME: Remove this when `OutputSpan` starts conforming to RangeReplaceableContainer - items.withUnsafeMutableBufferPointer { buffer, count in - let source = buffer._extracting(first: count) - unsafe _appendUnchecked(moving: source) - count = 0 - } - } - /// Moves the elements of an output span to the end of this array, leaving the /// span empty. /// @@ -180,8 +167,13 @@ extension RigidArray where Element: ~Copyable { public mutating func append( moving items: inout OutputSpan ) { + // FIXME: Remove this when `OutputSpan` starts conforming to RangeReplaceableContainer precondition(items.count <= freeCapacity, "RigidArray capacity overflow") - _appendUnchecked(moving: &items) + items.withUnsafeMutableBufferPointer { buffer, count in + let source = buffer._extracting(first: count) + unsafe _appendUnchecked(moving: source) + count = 0 + } } #if !UnstableContainersPreview @@ -257,14 +249,6 @@ extension RigidArray { unsafe self.append(copying: UnsafeBufferPointer(items)) } - @_alwaysEmitIntoClient - @unsafe - internal mutating func _appendUnchecked(copying items: Span) { - items.withUnsafeBufferPointer { source in - unsafe _appendUnchecked(copying: source) - } - } - /// Copies the elements of a span to the end of this array. /// /// If the array does not have sufficient capacity to hold all items in the @@ -277,7 +261,9 @@ extension RigidArray { @_alwaysEmitIntoClient public mutating func append(copying items: Span) { precondition(items.count <= freeCapacity, "RigidArray capacity overflow") - _appendUnchecked(copying: items) + items.withUnsafeBufferPointer { source in + unsafe _appendUnchecked(copying: source) + } } @_alwaysEmitIntoClient diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift index 7afc0961..9962a377 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift @@ -129,7 +129,11 @@ extension UniqueArray where Element: ~Copyable { moving items: inout OutputSpan ) { _ensureFreeCapacity(items.count) - unsafe _storage._appendUnchecked(moving: &items) + items.withUnsafeMutableBufferPointer { buffer, count in + let source = buffer._extracting(first: count) + unsafe _appendUnchecked(moving: source) + count = 0 + } } #if !UnstableContainersPreview @@ -254,7 +258,9 @@ extension UniqueArray { @_alwaysEmitIntoClient public mutating func append(copying newElements: Span) { _ensureFreeCapacity(newElements.count) - unsafe _storage._appendUnchecked(copying: newElements) + items.withUnsafeBufferPointer { source in + unsafe _storage._appendUnchecked(copying: source) + } } #if compiler(>=6.4) && UnstableContainersPreview From dad98a58ca1ca6670741cdbcd4e0ed618952c725 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 12 Aug 2026 13:31:02 -0700 Subject: [PATCH 3/6] Replace inlinable with aEIC --- Sources/BasicContainers/RigidArray/RigidArray+Append.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift index 8a0744d8..3e3cc765 100644 --- a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift +++ b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift @@ -20,7 +20,7 @@ import ContainersPreview @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { - @inlinable + @_alwaysEmitIntoClient @unsafe internal mutating func _appendUnchecked(_ item: consuming Element) { unsafe _storage.initializeElement(at: _count, to: item) @@ -99,7 +99,7 @@ extension RigidArray where Element: ~Copyable { @available(SwiftStdlib 5.0, *) extension RigidArray where Element: ~Copyable { - @inlinable + @_alwaysEmitIntoClient @unsafe internal mutating func _appendUnchecked( moving items: UnsafeMutableBufferPointer From 6c7ebb3cb94aeef12bb5fd157ae9239ad0d12a87 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 12 Aug 2026 13:32:38 -0700 Subject: [PATCH 4/6] Update UniqueArray+Append.swift --- Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift index 9962a377..3053a797 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift @@ -258,7 +258,7 @@ extension UniqueArray { @_alwaysEmitIntoClient public mutating func append(copying newElements: Span) { _ensureFreeCapacity(newElements.count) - items.withUnsafeBufferPointer { source in + newElements.withUnsafeBufferPointer { source in unsafe _storage._appendUnchecked(copying: source) } } From 1b511f76529b85928c4e118f4e8cadaa6edab386 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 12 Aug 2026 14:07:15 -0700 Subject: [PATCH 5/6] Update UniqueArray+Append.swift --- Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift index 3053a797..870ac266 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift @@ -131,7 +131,7 @@ extension UniqueArray where Element: ~Copyable { _ensureFreeCapacity(items.count) items.withUnsafeMutableBufferPointer { buffer, count in let source = buffer._extracting(first: count) - unsafe _appendUnchecked(moving: source) + unsafe _storage._appendUnchecked(moving: source) count = 0 } } From d44d6a3a26c6e73771f5bff30cc99330f085d633 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 12 Aug 2026 15:04:38 -0700 Subject: [PATCH 6/6] Update UniqueArray+Append.swift --- Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift index 870ac266..ad28daf8 100644 --- a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift +++ b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift @@ -278,7 +278,9 @@ extension UniqueArray { let span = try it.nextSpan() if span.isEmpty { break } _ensureFreeCapacity(span.count) - unsafe _storage._appendUnchecked(copying: span) + span.withUnsafeBufferPointer { source in + unsafe _storage._appendUnchecked(copying: source) + } } } // FIXME: Add _append(copyingContainer:), forwarding to the same method on RigidArray