Skip to content

Commit e0a31a9

Browse files
committed
move _arrayOutOfPlaceUpdate into _ArrayBufferProtocol extension
1 parent d07ff7f commit e0a31a9

File tree

1 file changed

+81
-82
lines changed

1 file changed

+81
-82
lines changed

stdlib/public/core/Arrays.swift.gyb

Lines changed: 81 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,8 +1216,8 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
12161216
let newCount = oldCount + 1
12171217
var newBuffer = _forceCreateUniqueMutableBuffer(
12181218
&_buffer, countForNewBuffer: oldCount, minNewCapacity: newCount)
1219-
_arrayOutOfPlaceUpdate(
1220-
&_buffer, &newBuffer, oldCount, 0, _IgnorePointer())
1219+
_buffer._arrayOutOfPlaceUpdate(
1220+
&newBuffer, oldCount, 0, _IgnorePointer())
12211221
}
12221222

12231223
@_semantics("array.make_mutable")
@@ -1684,8 +1684,8 @@ internal func _arrayOutOfPlaceReplace<B, C>(
16841684
var newBuffer = _forceCreateUniqueMutableBuffer(
16851685
&source, newCount: newCount, requiredCapacity: newCount)
16861686

1687-
_arrayOutOfPlaceUpdate(
1688-
&source, &newBuffer,
1687+
source._arrayOutOfPlaceUpdate(
1688+
&newBuffer,
16891689
bounds.lowerBound - source.startIndex, insertCount,
16901690
_InitializeMemoryFromCollection(newValues)
16911691
)
@@ -1882,82 +1882,81 @@ internal protocol _PointerFunction {
18821882
func call(_: UnsafeMutablePointer<Element>, count: Int)
18831883
}
18841884

1885-
/// Initialize the elements of dest by copying the first headCount
1886-
/// items from source, calling initializeNewElements on the next
1887-
/// uninitialized element, and finally by copying the last N items
1888-
/// from source into the N remaining uninitialized elements of dest.
1889-
///
1890-
/// As an optimization, may move elements out of source rather than
1891-
/// copying when it isUniquelyReferenced.
1892-
@inline(never)
1893-
internal func _arrayOutOfPlaceUpdate<_Buffer, Initializer>(
1894-
_ source: inout _Buffer,
1895-
_ dest: inout _ContiguousArrayBuffer<_Buffer.Element>,
1896-
_ headCount: Int, // Count of initial source elements to copy/move
1897-
_ newCount: Int, // Number of new elements to insert
1898-
_ initializeNewElements: Initializer
1899-
) where
1900-
_Buffer : _ArrayBufferProtocol,
1901-
Initializer : _PointerFunction,
1902-
Initializer.Element == _Buffer.Element,
1903-
_Buffer.Index == Int {
1904-
1905-
_sanityCheck(headCount >= 0)
1906-
_sanityCheck(newCount >= 0)
1907-
1908-
// Count of trailing source elements to copy/move
1909-
let tailCount = dest.count - headCount - newCount
1910-
_sanityCheck(headCount + tailCount <= source.count)
1911-
1912-
let sourceCount = source.count
1913-
let oldCount = sourceCount - headCount - tailCount
1914-
let destStart = dest.firstElementAddress
1915-
let newStart = destStart + headCount
1916-
let newEnd = newStart + newCount
1917-
1918-
// Check to see if we have storage we can move from
1919-
if let backing = source.requestUniqueMutableBackingBuffer(
1920-
minimumCapacity: sourceCount) {
1921-
1922-
let sourceStart = source.firstElementAddress
1923-
let oldStart = sourceStart + headCount
1924-
1925-
// Destroy any items that may be lurking in a _SliceBuffer before
1926-
// its real first element
1927-
let backingStart = backing.firstElementAddress
1928-
let sourceOffset = sourceStart - backingStart
1929-
backingStart.deinitialize(count: sourceOffset)
1930-
1931-
// Move the head items
1932-
destStart.moveInitialize(from: sourceStart, count: headCount)
1933-
1934-
// Destroy unused source items
1935-
oldStart.deinitialize(count: oldCount)
1936-
1937-
initializeNewElements.call(newStart, count: newCount)
1938-
1939-
// Move the tail items
1940-
newEnd.moveInitialize(from: oldStart + oldCount, count: tailCount)
1941-
1942-
// Destroy any items that may be lurking in a _SliceBuffer after
1943-
// its real last element
1944-
let backingEnd = backingStart + backing.count
1945-
let sourceEnd = sourceStart + sourceCount
1946-
sourceEnd.deinitialize(count: backingEnd - sourceEnd)
1947-
backing.count = 0
1948-
}
1949-
else {
1950-
let headStart = source.startIndex
1951-
let headEnd = headStart + headCount
1952-
let newStart = source._copyContents(
1953-
subRange: headStart..<headEnd,
1954-
initializing: destStart)
1955-
initializeNewElements.call(newStart, count: newCount)
1956-
let tailStart = headEnd + oldCount
1957-
let tailEnd = source.endIndex
1958-
source._copyContents(subRange: tailStart..<tailEnd, initializing: newEnd)
1959-
}
1960-
source = _Buffer(_buffer: dest, shiftedToStartIndex: source.startIndex)
1885+
extension _ArrayBufferProtocol where Index == Int {
1886+
/// Initialize the elements of dest by copying the first headCount
1887+
/// items from source, calling initializeNewElements on the next
1888+
/// uninitialized element, and finally by copying the last N items
1889+
/// from source into the N remaining uninitialized elements of dest.
1890+
///
1891+
/// As an optimization, may move elements out of source rather than
1892+
/// copying when it isUniquelyReferenced.
1893+
@inline(never)
1894+
internal mutating func _arrayOutOfPlaceUpdate<Initializer>(
1895+
_ dest: inout _ContiguousArrayBuffer<Element>,
1896+
_ headCount: Int, // Count of initial source elements to copy/move
1897+
_ newCount: Int, // Number of new elements to insert
1898+
_ initializeNewElements: Initializer
1899+
) where
1900+
Initializer : _PointerFunction,
1901+
Initializer.Element == Element {
1902+
1903+
_sanityCheck(headCount >= 0)
1904+
_sanityCheck(newCount >= 0)
1905+
1906+
// Count of trailing source elements to copy/move
1907+
let sourceCount = self.count
1908+
let tailCount = dest.count - headCount - newCount
1909+
_sanityCheck(headCount + tailCount <= sourceCount)
1910+
1911+
let oldCount = sourceCount - headCount - tailCount
1912+
let destStart = dest.firstElementAddress
1913+
let newStart = destStart + headCount
1914+
let newEnd = newStart + newCount
1915+
1916+
// Check to see if we have storage we can move from
1917+
if let backing = requestUniqueMutableBackingBuffer(
1918+
minimumCapacity: sourceCount) {
1919+
1920+
let sourceStart = firstElementAddress
1921+
let oldStart = sourceStart + headCount
1922+
1923+
// Destroy any items that may be lurking in a _SliceBuffer before
1924+
// its real first element
1925+
let backingStart = backing.firstElementAddress
1926+
let sourceOffset = sourceStart - backingStart
1927+
backingStart.deinitialize(count: sourceOffset)
1928+
1929+
// Move the head items
1930+
destStart.moveInitialize(from: sourceStart, count: headCount)
1931+
1932+
// Destroy unused source items
1933+
oldStart.deinitialize(count: oldCount)
1934+
1935+
initializeNewElements.call(newStart, count: newCount)
1936+
1937+
// Move the tail items
1938+
newEnd.moveInitialize(from: oldStart + oldCount, count: tailCount)
1939+
1940+
// Destroy any items that may be lurking in a _SliceBuffer after
1941+
// its real last element
1942+
let backingEnd = backingStart + backing.count
1943+
let sourceEnd = sourceStart + sourceCount
1944+
sourceEnd.deinitialize(count: backingEnd - sourceEnd)
1945+
backing.count = 0
1946+
}
1947+
else {
1948+
let headStart = startIndex
1949+
let headEnd = headStart + headCount
1950+
let newStart = _copyContents(
1951+
subRange: headStart..<headEnd,
1952+
initializing: destStart)
1953+
initializeNewElements.call(newStart, count: newCount)
1954+
let tailStart = headEnd + oldCount
1955+
let tailEnd = endIndex
1956+
self._copyContents(subRange: tailStart..<tailEnd, initializing: newEnd)
1957+
}
1958+
self = Self(_buffer: dest, shiftedToStartIndex: startIndex)
1959+
}
19611960
}
19621961

19631962
internal struct _InitializePointer<T> : _PointerFunction {
@@ -1996,7 +1995,7 @@ internal func _outlinedMakeUniqueBuffer<_Buffer>(
19961995

19971996
var newBuffer = _forceCreateUniqueMutableBuffer(
19981997
&buffer, newCount: bufferCount, requiredCapacity: bufferCount)
1999-
_arrayOutOfPlaceUpdate(&buffer, &newBuffer, bufferCount, 0, _IgnorePointer())
1998+
buffer._arrayOutOfPlaceUpdate(&newBuffer, bufferCount, 0, _IgnorePointer())
20001999
}
20012000

20022001
internal func _arrayReserve<_Buffer>(
@@ -2017,7 +2016,7 @@ internal func _arrayReserve<_Buffer>(
20172016

20182017
var newBuffer = _forceCreateUniqueMutableBuffer(
20192018
&buffer, newCount: count, requiredCapacity: requiredCapacity)
2020-
_arrayOutOfPlaceUpdate(&buffer, &newBuffer, count, 0, _IgnorePointer())
2019+
buffer._arrayOutOfPlaceUpdate(&newBuffer, count, 0, _IgnorePointer())
20212020
}
20222021

20232022
/// Append items from `newItems` to `buffer`.

0 commit comments

Comments
 (0)