Skip to content

Commit d9b1489

Browse files
authored
Merge pull request #38867 from lorentey/no-swaps
[stdlib] Stop swapping self in Array.withUnsafeMutableBufferPointer
2 parents 5d6659c + ed287af commit d9b1489

File tree

1 file changed

+1
-16
lines changed

1 file changed

+1
-16
lines changed

stdlib/public/core/Array.swift

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,31 +1604,16 @@ extension Array {
16041604
_makeMutableAndUnique()
16051605
let count = _buffer.mutableCount
16061606

1607-
// Ensure that body can't invalidate the storage or its bounds by
1608-
// moving self into a temporary working array.
1609-
// NOTE: The stack promotion optimization that keys of the
1610-
// "array.withUnsafeMutableBufferPointer" semantics annotation relies on the
1611-
// array buffer not being able to escape in the closure. It can do this
1612-
// because we swap the array buffer in self with an empty buffer here. Any
1613-
// escape via the address of self in the closure will therefore escape the
1614-
// empty array.
1615-
1616-
var work = Array()
1617-
(work, self) = (self, work)
1618-
16191607
// Create an UnsafeBufferPointer over work that we can pass to body
1620-
let pointer = work._buffer.mutableFirstElementAddress
1608+
let pointer = _buffer.mutableFirstElementAddress
16211609
var inoutBufferPointer = UnsafeMutableBufferPointer(
16221610
start: pointer, count: count)
16231611

1624-
// Put the working array back before returning.
16251612
defer {
16261613
_precondition(
16271614
inoutBufferPointer.baseAddress == pointer &&
16281615
inoutBufferPointer.count == count,
16291616
"Array withUnsafeMutableBufferPointer: replacing the buffer is not allowed")
1630-
1631-
(work, self) = (self, work)
16321617
_endMutation()
16331618
}
16341619

0 commit comments

Comments
 (0)