Skip to content

Commit 60a0718

Browse files
committed
stdlib: simplify Array/ContiguousArray's withUnsafeMutableBufferPointer and withUnsafeMutableBufferPointer
Again, to reduce code size
1 parent bda8af0 commit 60a0718

File tree

2 files changed

+6
-22
lines changed

2 files changed

+6
-22
lines changed

stdlib/public/core/Array.swift

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,9 +1587,8 @@ extension Array {
15871587
public mutating func withUnsafeMutableBufferPointer<R>(
15881588
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
15891589
) rethrows -> R {
1590+
_makeMutableAndUnique()
15901591
let count = self.count
1591-
// Ensure unique storage
1592-
_buffer._outlinedMakeUniqueBuffer(bufferCount: count)
15931592

15941593
// Ensure that body can't invalidate the storage or its bounds by
15951594
// moving self into a temporary working array.
@@ -1701,19 +1700,12 @@ extension Array {
17011700
_precondition(subrange.upperBound <= _buffer.endIndex,
17021701
"Array replace: subrange extends past the end")
17031702

1704-
let oldCount = _buffer.count
17051703
let eraseCount = subrange.count
17061704
let insertCount = newElements.count
17071705
let growth = insertCount - eraseCount
17081706

1709-
if _buffer.requestUniqueMutableBackingBuffer(
1710-
minimumCapacity: oldCount + growth) != nil {
1711-
1712-
_buffer.replaceSubrange(
1713-
subrange, with: insertCount, elementsOf: newElements)
1714-
} else {
1715-
_buffer._arrayOutOfPlaceReplace(subrange, with: newElements, count: insertCount)
1716-
}
1707+
reserveCapacityForAppend(newElementsCount: growth)
1708+
_buffer.replaceSubrange(subrange, with: insertCount, elementsOf: newElements)
17171709
}
17181710
}
17191711

stdlib/public/core/ContiguousArray.swift

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,9 +1168,8 @@ extension ContiguousArray {
11681168
public mutating func withUnsafeMutableBufferPointer<R>(
11691169
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
11701170
) rethrows -> R {
1171+
_makeMutableAndUnique()
11711172
let count = self.count
1172-
// Ensure unique storage
1173-
_buffer._outlinedMakeUniqueBuffer(bufferCount: count)
11741173

11751174
// Ensure that body can't invalidate the storage or its bounds by
11761175
// moving self into a temporary working array.
@@ -1283,19 +1282,12 @@ extension ContiguousArray {
12831282
_precondition(subrange.upperBound <= _buffer.endIndex,
12841283
"ContiguousArray replace: subrange extends past the end")
12851284

1286-
let oldCount = _buffer.count
12871285
let eraseCount = subrange.count
12881286
let insertCount = newElements.count
12891287
let growth = insertCount - eraseCount
12901288

1291-
if _buffer.requestUniqueMutableBackingBuffer(
1292-
minimumCapacity: oldCount + growth) != nil {
1293-
1294-
_buffer.replaceSubrange(
1295-
subrange, with: insertCount, elementsOf: newElements)
1296-
} else {
1297-
_buffer._arrayOutOfPlaceReplace(subrange, with: newElements, count: insertCount)
1298-
}
1289+
reserveCapacityForAppend(newElementsCount: growth)
1290+
_buffer.replaceSubrange(subrange, with: insertCount, elementsOf: newElements)
12991291
}
13001292
}
13011293

0 commit comments

Comments
 (0)