Skip to content

Commit dabfcb2

Browse files
authored
Merge pull request #65778 from Catfish-Man/contiguous-conundrum
Try using withContiguousStorageIfAvailable in RangeReplaceableCollection.append(contentsOf:) before falling back to a slow element-by-element loop.
2 parents f97d1f2 + 5efc2da commit dabfcb2

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

stdlib/public/core/RangeReplaceableCollection.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,17 @@ extension RangeReplaceableCollection {
453453
@inlinable
454454
public mutating func append<S: Sequence>(contentsOf newElements: __owned S)
455455
where S.Element == Element {
456-
457-
let approximateCapacity = self.count + newElements.underestimatedCount
458-
self.reserveCapacity(approximateCapacity)
459-
for element in newElements {
460-
append(element)
456+
457+
let done:Void? = newElements.withContiguousStorageIfAvailable {
458+
replaceSubrange(endIndex..<endIndex, with: $0)
459+
}
460+
461+
if done == nil {
462+
let approximateCapacity = self.count + newElements.underestimatedCount
463+
self.reserveCapacity(approximateCapacity)
464+
for element in newElements {
465+
append(element)
466+
}
461467
}
462468
}
463469

0 commit comments

Comments
 (0)