Skip to content

Commit ba3c403

Browse files
committed
[stdlib] Deprecate MutableCollection._withUnsafeMutableBufferPointerIfSupported
In Swift 5.0, [SE-0237] introduced the public `MutableCollection.withContiguousMutableStorageIfAvailable` method. It’s time we migrated off the old, underscored variant and deprecated it. The default `MutableCollection.sort` and `.partition(by:)` implementations are currently calling this hidden method rather than the documented interface, preventing custom Collection implementations from achieving good performance, even if they have contiguous storage. [SE-0237]: https://github.com/apple/swift-evolution/blob/master/proposals/0237-contiguous-collection.md
1 parent 65a9052 commit ba3c403

File tree

7 files changed

+10
-4
lines changed

7 files changed

+10
-4
lines changed

stdlib/public/core/Array.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,6 +1356,7 @@ extension Array: RangeReplaceableCollection {
13561356
//===--- algorithms -----------------------------------------------------===//
13571357

13581358
@inlinable
1359+
@available(*, deprecated, renamed: "withContiguousMutableStorageIfAvailable")
13591360
public mutating func _withUnsafeMutableBufferPointerIfSupported<R>(
13601361
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
13611362
) rethrows -> R? {

stdlib/public/core/ArraySlice.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,7 @@ extension ArraySlice: RangeReplaceableCollection {
10791079
//===--- algorithms -----------------------------------------------------===//
10801080

10811081
@inlinable
1082+
@available(*, deprecated, renamed: "withContiguousMutableStorageIfAvailable")
10821083
public mutating func _withUnsafeMutableBufferPointerIfSupported<R>(
10831084
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
10841085
) rethrows -> R? {

stdlib/public/core/CollectionAlgorithms.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ extension MutableCollection where Self: BidirectionalCollection {
318318
public mutating func partition(
319319
by belongsInSecondPartition: (Element) throws -> Bool
320320
) rethrows -> Index {
321-
let maybeOffset = try _withUnsafeMutableBufferPointerIfSupported {
321+
let maybeOffset = try withContiguousMutableStorageIfAvailable {
322322
(bufferPointer) -> Int in
323323
let unsafeBufferPivot = try bufferPointer._partitionImpl(
324324
by: belongsInSecondPartition)

stdlib/public/core/ContiguousArray.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,7 @@ extension ContiguousArray: RangeReplaceableCollection {
980980
//===--- algorithms -----------------------------------------------------===//
981981

982982
@inlinable
983+
@available(*, deprecated, renamed: "withContiguousMutableStorageIfAvailable")
983984
public mutating func _withUnsafeMutableBufferPointerIfSupported<R>(
984985
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
985986
) rethrows -> R? {

stdlib/public/core/MutableCollection.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ where SubSequence: MutableCollection
177177
/// within an algorithm, but when that fails, invoking the
178178
/// same algorithm on `body`\ 's argument lets you trade safety for
179179
/// speed.
180+
@available(*, deprecated, renamed: "withContiguousMutableStorageIfAvailable")
180181
mutating func _withUnsafeMutableBufferPointerIfSupported<R>(
181182
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
182183
) rethrows -> R?
@@ -199,6 +200,7 @@ where SubSequence: MutableCollection
199200
// TODO: swift-3-indexing-model - review the following
200201
extension MutableCollection {
201202
@inlinable
203+
@available(*, deprecated, renamed: "withContiguousMutableStorageIfAvailable")
202204
public mutating func _withUnsafeMutableBufferPointerIfSupported<R>(
203205
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
204206
) rethrows -> R? {

stdlib/public/core/Sort.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,10 @@ extension MutableCollection where Self: RandomAccessCollection {
246246
public mutating func sort(
247247
by areInIncreasingOrder: (Element, Element) throws -> Bool
248248
) rethrows {
249-
let didSortUnsafeBuffer: Void? = try _withUnsafeMutableBufferPointerIfSupported {
250-
buffer -> Void in
249+
let didSortUnsafeBuffer =
250+
try withContiguousMutableStorageIfAvailable { buffer in
251251
try buffer._stableSortImpl(by: areInIncreasingOrder)
252-
}
252+
}
253253
if didSortUnsafeBuffer == nil {
254254
// Fallback since we can't use an unsafe buffer: sort into an outside
255255
// array, then copy elements back in.

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ extension Unsafe${Mutable}BufferPointer {
446446
}
447447

448448
@inlinable
449+
@available(*, deprecated, renamed: "withContiguousMutableStorageIfAvailable")
449450
public mutating func _withUnsafeMutableBufferPointerIfSupported<R>(
450451
_ body: (inout UnsafeMutableBufferPointer<Element>) throws -> R
451452
) rethrows -> R? {

0 commit comments

Comments
 (0)