Skip to content

Commit bedda7e

Browse files
committed
[stdlib] only move/erase/add if required in Array.replaceSubrange
1 parent 869bbe9 commit bedda7e

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
@@ -163,21 +163,28 @@ extension _ArrayBufferProtocol {
163163
// erase all the elements we're replacing to create a hole
164164
let holeStart = elements + subrange.lowerBound
165165
let holeEnd = holeStart + newCount
166-
holeStart.deinitialize(count: eraseCount)
166+
167+
if eraseCount > 0 {
168+
holeStart.deinitialize(count: eraseCount)
169+
}
167170

168171
// resize the hole to make it the correct size
169-
let tailStart = elements + subrange.upperBound
170-
let tailCount = oldCount - subrange.upperBound
171-
holeEnd.moveInitialize(from: tailStart, count: tailCount)
172+
if growth != 0 {
173+
let tailStart = elements + subrange.upperBound
174+
let tailCount = oldCount - subrange.upperBound
175+
holeEnd.moveInitialize(from: tailStart, count: tailCount)
176+
}
172177

173178
// place the values into the hole we created
174-
var place = holeStart
175-
var i = newValues.startIndex
176-
while place < holeEnd {
177-
place.initialize(to: newValues[i])
178-
place += 1
179-
newValues.formIndex(after: &i)
179+
if newCount > 0 {
180+
var place = holeStart
181+
var i = newValues.startIndex
182+
while place < holeEnd {
183+
place.initialize(to: newValues[i])
184+
place += 1
185+
newValues.formIndex(after: &i)
186+
}
187+
_expectEnd(of: newValues, is: i)
180188
}
181-
_expectEnd(of: newValues, is: i)
182189
}
183190
}

0 commit comments

Comments
 (0)