Skip to content

Commit cfaff1d

Browse files
[stdlib] Move _copyBuffer to be an init on _ArrayBufferProtocol (#14222)
* Move _copyBuffer to be an init on _ArrayBufferProtocol * Make init(copying:) inlineable, even though its inline(never), to allow specialization. * Delete old array swap test, as swap on arrays is no longer valid. * Delete the old static method
1 parent 79b0437 commit cfaff1d

File tree

3 files changed

+20
-32
lines changed

3 files changed

+20
-32
lines changed

stdlib/public/core/ArrayBufferProtocol.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ internal protocol _ArrayBufferProtocol
2424
/// Adopt the entire buffer, presenting it at the provided `startIndex`.
2525
init(_buffer: _ContiguousArrayBuffer<Element>, shiftedToStartIndex: Int)
2626

27+
init(copying buffer: Self)
28+
2729
/// Copy the elements in `bounds` from this buffer into uninitialized
2830
/// memory starting at `target`. Return a pointer "past the end" of the
2931
/// just-initialized memory.
@@ -124,14 +126,27 @@ internal protocol _ArrayBufferProtocol
124126
var endIndex: Int { get }
125127
}
126128

127-
extension _ArrayBufferProtocol {
129+
extension _ArrayBufferProtocol where Indices == CountableRange<Int>{
128130

129131
@_inlineable
130132
@_versioned
131133
internal var subscriptBaseAddress: UnsafeMutablePointer<Element> {
132134
return firstElementAddress
133135
}
134136

137+
// Make sure the compiler does not inline _copyBuffer to reduce code size.
138+
@_inlineable
139+
@inline(never)
140+
@_versioned
141+
internal init(copying buffer: Self) {
142+
let newBuffer = _ContiguousArrayBuffer<Element>(
143+
_uninitializedCount: buffer.count, minimumCapacity: buffer.count)
144+
buffer._copyContents(
145+
subRange: Range(buffer.indices),
146+
initializing: newBuffer.firstElementAddress)
147+
self = Self( _buffer: newBuffer, shiftedToStartIndex: buffer.startIndex)
148+
}
149+
135150
@_inlineable
136151
@_versioned
137152
internal mutating func replaceSubrange<C>(

stdlib/public/core/Arrays.swift.gyb

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -835,27 +835,12 @@ extension ${Self} {
835835
return Builtin.unsafeCastToNativeObject(_buffer.owner)
836836
}
837837

838-
// FIXME(ABI)#12 : move to an initializer on _Buffer.
839-
// Make sure the compiler does not inline _copyBuffer to reduce code size.
840-
@inline(never)
841-
@_inlineable
842-
@_versioned
843-
static internal func _copyBuffer(_ buffer: inout _Buffer) {
844-
let newBuffer = _ContiguousArrayBuffer<Element>(
845-
_uninitializedCount: buffer.count, minimumCapacity: buffer.count)
846-
buffer._copyContents(
847-
subRange: buffer.indices,
848-
initializing: newBuffer.firstElementAddress)
849-
buffer = _Buffer(
850-
_buffer: newBuffer, shiftedToStartIndex: buffer.startIndex)
851-
}
852-
853838
@_inlineable
854839
@_versioned
855840
@_semantics("array.make_mutable")
856841
internal mutating func _makeMutableAndUnique() {
857842
if _slowPath(!_buffer.isMutableAndUniquelyReferenced()) {
858-
${Self}._copyBuffer(&_buffer)
843+
_buffer = _Buffer(copying: _buffer)
859844
}
860845
}
861846

@@ -864,7 +849,7 @@ extension ${Self} {
864849
@_semantics("array.make_mutable")
865850
internal mutating func _makeMutableAndUniqueOrPinned() {
866851
if _slowPath(!_buffer.isMutableAndUniquelyReferencedOrPinned()) {
867-
${Self}._copyBuffer(&_buffer)
852+
_buffer = _Buffer(copying: _buffer)
868853
}
869854
}
870855

@@ -1543,8 +1528,8 @@ extension ${Self} : RangeReplaceableCollection, ArrayProtocol {
15431528
@_inlineable
15441529
public mutating func popLast() -> Element? {
15451530
let newCount = _getCount() - 1
1546-
guard newCount >= 0 else { return nil }
1547-
_makeUniqueAndReserveCapacityIfNotUnique()
1531+
guard !isEmpty else { return nil }
1532+
return _customRemoveLast().unsafelyUnwrapped
15481533
let pointer = (_buffer.firstElementAddress + newCount)
15491534
let element = pointer.move()
15501535
_buffer.count = newCount

test/SILOptimizer/swap_refcnt.swift

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)