Skip to content

Commit e427a5b

Browse files
committed
Move _copyBuffer to be an init on _ArrayBufferProtocol
1 parent e43ff71 commit e427a5b

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

stdlib/public/core/ArrayBufferProtocol.swift

Lines changed: 15 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,26 @@ 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+
@inline(never)
139+
@_versioned
140+
internal init(copying buffer: Self) {
141+
let newBuffer = _ContiguousArrayBuffer<Element>(
142+
_uninitializedCount: buffer.count, minimumCapacity: buffer.count)
143+
buffer._copyContents(
144+
subRange: Range(buffer.indices),
145+
initializing: newBuffer.firstElementAddress)
146+
self = Self( _buffer: newBuffer, shiftedToStartIndex: buffer.startIndex)
147+
}
148+
135149
@_inlineable
136150
@_versioned
137151
internal mutating func replaceSubrange<C>(

stdlib/public/core/Arrays.swift.gyb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ extension ${Self} {
855855
@_semantics("array.make_mutable")
856856
internal mutating func _makeMutableAndUnique() {
857857
if _slowPath(!_buffer.isMutableAndUniquelyReferenced()) {
858-
${Self}._copyBuffer(&_buffer)
858+
_buffer = _Buffer(copying: _buffer)
859859
}
860860
}
861861

@@ -864,7 +864,7 @@ extension ${Self} {
864864
@_semantics("array.make_mutable")
865865
internal mutating func _makeMutableAndUniqueOrPinned() {
866866
if _slowPath(!_buffer.isMutableAndUniquelyReferencedOrPinned()) {
867-
${Self}._copyBuffer(&_buffer)
867+
_buffer = _Buffer(copying: _buffer)
868868
}
869869
}
870870

@@ -1543,8 +1543,8 @@ extension ${Self} : RangeReplaceableCollection, ArrayProtocol {
15431543
@_inlineable
15441544
public mutating func popLast() -> Element? {
15451545
let newCount = _getCount() - 1
1546-
guard newCount >= 0 else { return nil }
1547-
_makeUniqueAndReserveCapacityIfNotUnique()
1546+
guard !isEmpty else { return nil }
1547+
return _customRemoveLast().unsafelyUnwrapped
15481548
let pointer = (_buffer.firstElementAddress + newCount)
15491549
let element = pointer.move()
15501550
_buffer.count = newCount

0 commit comments

Comments
 (0)