diff --git a/Sources/BasicContainers/RigidArray/RigidArray+Append.swift b/Sources/BasicContainers/RigidArray/RigidArray+Append.swift index 7db3487f..3e3cc765 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 { + @_alwaysEmitIntoClient + @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 { + @_alwaysEmitIntoClient + @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 @@ -154,9 +168,10 @@ extension RigidArray where Element: ~Copyable { moving items: inout OutputSpan ) { // FIXME: Remove this when `OutputSpan` starts conforming to RangeReplaceableContainer + precondition(items.count <= freeCapacity, "RigidArray capacity overflow") items.withUnsafeMutableBufferPointer { buffer, count in let source = buffer._extracting(first: count) - unsafe self.append(moving: source) + unsafe _appendUnchecked(moving: source) count = 0 } } @@ -186,6 +201,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 +229,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. @@ -237,8 +260,9 @@ extension RigidArray { /// - Complexity: O(`newElements.count`) @_alwaysEmitIntoClient public mutating func append(copying items: Span) { + precondition(items.count <= freeCapacity, "RigidArray capacity overflow") items.withUnsafeBufferPointer { source in - unsafe self.append(copying: source) + unsafe _appendUnchecked(copying: source) } } diff --git a/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift b/Sources/BasicContainers/UniqueArray/UniqueArray+Append.swift index defdf685..ad28daf8 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,11 @@ extension UniqueArray where Element: ~Copyable { moving items: inout OutputSpan ) { _ensureFreeCapacity(items.count) - _storage.append(moving: &items) + items.withUnsafeMutableBufferPointer { buffer, count in + let source = buffer._extracting(first: count) + unsafe _storage._appendUnchecked(moving: source) + count = 0 + } } #if !UnstableContainersPreview @@ -218,7 +222,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 +258,9 @@ extension UniqueArray { @_alwaysEmitIntoClient public mutating func append(copying newElements: Span) { _ensureFreeCapacity(newElements.count) - _storage.append(copying: newElements) + newElements.withUnsafeBufferPointer { source in + unsafe _storage._appendUnchecked(copying: source) + } } #if compiler(>=6.4) && UnstableContainersPreview @@ -272,7 +278,9 @@ extension UniqueArray { let span = try it.nextSpan() if span.isEmpty { break } _ensureFreeCapacity(span.count) - _storage.append(copying: span) + span.withUnsafeBufferPointer { source in + unsafe _storage._appendUnchecked(copying: source) + } } } // FIXME: Add _append(copyingContainer:), forwarding to the same method on RigidArray @@ -323,7 +331,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 +339,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) } }