[Deque] Force-inline the subscript’s _modify accessor to work around a performance issue

Resolves #164.

(cherry picked from commit 98cd753866)
This commit is contained in:
Karoy Lorentey committed 2022-08-31 17:13:53 -07:00
1 parent 6cbecdb467
commit 87c78de6b4
1 file changed
+22 -11
+22 -11
View File
@@ -359,25 +359,36 @@ extension Deque: RandomAccessCollection {
handle.ptr(at: slot).pointee = newValue
}
}
@inline(__always) // https://github.com/apple/swift-collections/issues/164
_modify {
precondition(index >= 0 && index < count, "Index out of bounds")
_storage.ensureUnique()
// We technically aren't supposed to escape storage pointers out of a
// managed buffer, so we escape a `(slot, value)` pair instead, leaving
// the corresponding slot temporarily uninitialized.
var (slot, value) = _storage.update { handle -> (_Slot, Element) in
let slot = handle.slot(forOffset: index)
return (slot, handle.ptr(at: slot).move())
}
var (slot, value) = _prepareForModify(at: index)
defer {
_storage.update { handle in
handle.ptr(at: slot).initialize(to: value)
}
_finalizeModify(slot, value)
}
yield &value
}
}
@inlinable
internal mutating func _prepareForModify(at index: Int) -> (_Slot, Element) {
_storage.ensureUnique()
// We technically aren't supposed to escape storage pointers out of a
// managed buffer, so we escape a `(slot, value)` pair instead, leaving
// the corresponding slot temporarily uninitialized.
return _storage.update { handle in
let slot = handle.slot(forOffset: index)
return (slot, handle.ptr(at: slot).move())
}
}
@inlinable
internal mutating func _finalizeModify(_ slot: _Slot, _ value: Element) {
_storage.update { handle in
handle.ptr(at: slot).initialize(to: value)
}
}
/// Accesses a contiguous subrange of the deque's elements.
///
/// - Parameters: