Skip to content

Commit a91cd6e

Browse files
authored
[5.9] Try using withContiguousStorageIfAvailable in RangeReplaceableCollection.append(contentsOf:) before falling back to a slow element-by-element loop (#66838)
* Try using withContiguousStorageIfAvailable in RangeReplaceableCollection.append(contentsOf:) before falling back to a slow element-by-element loop. Fixes rdar://109059874 (cherry picked from commit 465aa22) * _onFastPath, in inlineable code, will end up applying to the caller. We don't want that, so omit it (cherry picked from commit 5efc2da) * Don't reserveCapacity in append(contentsOf:), it breaks API guarantees of asymptotic complexity (cherry picked from commit 476c472)
1 parent 5722ca3 commit a91cd6e

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

stdlib/public/core/RangeReplaceableCollection.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,15 @@ 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+
for element in newElements {
463+
append(element)
464+
}
461465
}
462466
}
463467

0 commit comments

Comments
 (0)