Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stdlib] ContiguousArray & ArraySlice: Stop swapping self in wUMBP #38898

Merged
merged 1 commit into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion stdlib/public/core/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1604,7 +1604,7 @@ extension Array {
_makeMutableAndUnique()
let count = _buffer.mutableCount

// Create an UnsafeBufferPointer over work that we can pass to body
// Create an UnsafeBufferPointer that we can pass to body
let pointer = _buffer.mutableFirstElementAddress
var inoutBufferPointer = UnsafeMutableBufferPointer(
start: pointer, count: count)
Expand All @@ -1615,6 +1615,7 @@ extension Array {
inoutBufferPointer.count == count,
"Array withUnsafeMutableBufferPointer: replacing the buffer is not allowed")
_endMutation()
_fixLifetime(self)
}

// Invoke the body.
Expand Down
20 changes: 3 additions & 17 deletions stdlib/public/core/ArraySlice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1239,32 +1239,18 @@ extension ArraySlice {
// Ensure unique storage
_makeMutableAndUnique()

// Ensure that body can't invalidate the storage or its bounds by
// moving self into a temporary working array.
// NOTE: The stack promotion optimization that keys of the
// "array.withUnsafeMutableBufferPointer" semantics annotation relies on the
// array buffer not being able to escape in the closure. It can do this
// because we swap the array buffer in self with an empty buffer here. Any
// escape via the address of self in the closure will therefore escape the
// empty array.

var work = ArraySlice()
(work, self) = (self, work)

// Create an UnsafeBufferPointer over work that we can pass to body
let pointer = work._buffer.firstElementAddress
// Create an UnsafeBufferPointer that we can pass to body
let pointer = _buffer.firstElementAddress
var inoutBufferPointer = UnsafeMutableBufferPointer(
start: pointer, count: count)

// Put the working array back before returning.
defer {
_precondition(
inoutBufferPointer.baseAddress == pointer &&
inoutBufferPointer.count == count,
"ArraySlice withUnsafeMutableBufferPointer: replacing the buffer is not allowed")

(work, self) = (self, work)
_endMutation()
_fixLifetime(self)
}

// Invoke the body.
Expand Down
20 changes: 3 additions & 17 deletions stdlib/public/core/ContiguousArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1176,32 +1176,18 @@ extension ContiguousArray {
_makeMutableAndUnique()
let count = _buffer.mutableCount

// Ensure that body can't invalidate the storage or its bounds by
// moving self into a temporary working array.
// NOTE: The stack promotion optimization that keys of the
// "array.withUnsafeMutableBufferPointer" semantics annotation relies on the
// array buffer not being able to escape in the closure. It can do this
// because we swap the array buffer in self with an empty buffer here. Any
// escape via the address of self in the closure will therefore escape the
// empty array.

var work = ContiguousArray()
(work, self) = (self, work)

// Create an UnsafeBufferPointer over work that we can pass to body
let pointer = work._buffer.mutableFirstElementAddress
// Create an UnsafeBufferPointer that we can pass to body
let pointer = _buffer.mutableFirstElementAddress
var inoutBufferPointer = UnsafeMutableBufferPointer(
start: pointer, count: count)

// Put the working array back before returning.
defer {
_precondition(
inoutBufferPointer.baseAddress == pointer &&
inoutBufferPointer.count == count,
"ContiguousArray withUnsafeMutableBufferPointer: replacing the buffer is not allowed")

(work, self) = (self, work)
_endMutation()
_fixLifetime(self)
}

// Invoke the body.
Expand Down