Skip to content

Commit 2a49d82

Browse files
committed
Merge pull request #2296 from apple/stdlib-array-move-code-to-append-contents-of
stdlib: move logic from '+=' to 'Array.append(contentsOf:)'
2 parents 44d422e + 919d049 commit 2a49d82

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

stdlib/public/core/Arrays.swift.gyb

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,15 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
703703
where
704704
S.Iterator.Element == Element
705705
>(contentsOf newElements: S) {
706-
self += newElements
706+
let oldCount = self.count
707+
let capacity = self.capacity
708+
let newCount = oldCount + newElements.underestimatedCount
709+
710+
if newCount > capacity {
711+
self.reserveCapacity(
712+
Swift.max(newCount, _growArrayCapacity(capacity)))
713+
}
714+
_arrayAppendSequence(&self._buffer, newElements)
707715
}
708716

709717
// An overload of `append(contentsOf:)` that uses the += that is optimized for
@@ -718,7 +726,21 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
718726
where
719727
C.Iterator.Element == Element
720728
>(contentsOf newElements: C) {
721-
self += newElements
729+
let newElementsCount = numericCast(newElements.count) as Int
730+
731+
let oldCount = self.count
732+
let capacity = self.capacity
733+
let newCount = oldCount + newElementsCount
734+
735+
// Ensure uniqueness, mutability, and sufficient storage. Note that
736+
// for consistency, we need unique self even if newElements is empty.
737+
self.reserveCapacity(
738+
newCount > capacity ?
739+
Swift.max(newCount, _growArrayCapacity(capacity))
740+
: newCount)
741+
742+
(self._buffer.firstElementAddress + oldCount).initializeFrom(newElements)
743+
self._buffer.count = newCount
722744
}
723745

724746
/// Remove and return the last element in O(1).
@@ -1018,15 +1040,7 @@ extension ${Self} {
10181040
public func += <
10191041
S : Sequence
10201042
>(lhs: inout ${Self}<S.Iterator.Element>, rhs: S) {
1021-
let oldCount = lhs.count
1022-
let capacity = lhs.capacity
1023-
let newCount = oldCount + rhs.underestimatedCount
1024-
1025-
if newCount > capacity {
1026-
lhs.reserveCapacity(
1027-
max(newCount, _growArrayCapacity(capacity)))
1028-
}
1029-
_arrayAppendSequence(&lhs._buffer, rhs)
1043+
lhs.append(contentsOf: rhs)
10301044
}
10311045

10321046
// FIXME(ABI): remove this entrypoint. The functionality should be provided by
@@ -1035,21 +1049,7 @@ public func += <
10351049
public func += <
10361050
C : Collection
10371051
>(lhs: inout ${Self}<C.Iterator.Element>, rhs: C) {
1038-
let rhsCount = numericCast(rhs.count) as Int
1039-
1040-
let oldCount = lhs.count
1041-
let capacity = lhs.capacity
1042-
let newCount = oldCount + rhsCount
1043-
1044-
// Ensure uniqueness, mutability, and sufficient storage. Note that
1045-
// for consistency, we need unique lhs even if rhs is empty.
1046-
lhs.reserveCapacity(
1047-
newCount > capacity ?
1048-
max(newCount, _growArrayCapacity(capacity))
1049-
: newCount)
1050-
1051-
(lhs._buffer.firstElementAddress + oldCount).initializeFrom(rhs)
1052-
lhs._buffer.count = newCount
1052+
lhs.append(contentsOf: rhs)
10531053
}
10541054
% end
10551055

0 commit comments

Comments
 (0)