Skip to content

Commit 44e92dd

Browse files
committed
[stdlib] only move/erase/add if required in Array.replaceSubrange
1 parent 531fafb commit 44e92dd

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

stdlib/public/core/ArrayBufferProtocol.swift

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,28 @@ extension _ArrayBufferProtocol {
159159
// erase all the elements we're replacing to create a hole
160160
let holeStart = elements + subrange.lowerBound
161161
let holeEnd = holeStart + newCount
162-
holeStart.deinitialize(count: eraseCount)
162+
163+
if eraseCount > 0 {
164+
holeStart.deinitialize(count: eraseCount)
165+
}
163166

164167
// resize the hole to make it the correct size
165-
let tailStart = elements + subrange.upperBound
166-
let tailCount = oldCount - subrange.upperBound
167-
holeEnd.moveInitialize(from: tailStart, count: tailCount)
168+
if growth != 0 {
169+
let tailStart = elements + subrange.upperBound
170+
let tailCount = oldCount - subrange.upperBound
171+
holeEnd.moveInitialize(from: tailStart, count: tailCount)
172+
}
168173

169174
// place the values into the hole we created
170-
var place = holeStart
171-
var i = newValues.startIndex
172-
while place < holeEnd {
173-
place.initialize(to: newValues[i])
174-
place += 1
175-
newValues.formIndex(after: &i)
175+
if newCount > 0 {
176+
var place = holeStart
177+
var i = newValues.startIndex
178+
while place < holeEnd {
179+
place.initialize(to: newValues[i])
180+
place += 1
181+
newValues.formIndex(after: &i)
182+
}
183+
_expectEnd(of: newValues, is: i)
176184
}
177-
_expectEnd(of: newValues, is: i)
178185
}
179186
}

0 commit comments

Comments
 (0)