@@ -703,7 +703,15 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
703
703
where
704
704
S. Iterator. Element == Element
705
705
> ( 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)
707
715
}
708
716
709
717
// An overload of `append(contentsOf:)` that uses the += that is optimized for
@@ -718,7 +726,21 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
718
726
where
719
727
C. Iterator. Element == Element
720
728
> ( 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
722
744
}
723
745
724
746
/// Remove and return the last element in O(1).
@@ -1018,15 +1040,7 @@ extension ${Self} {
1018
1040
public func += <
1019
1041
S : Sequence
1020
1042
> ( 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)
1030
1044
}
1031
1045
1032
1046
// FIXME(ABI): remove this entrypoint. The functionality should be provided by
@@ -1035,21 +1049,7 @@ public func += <
1035
1049
public func += <
1036
1050
C : Collection
1037
1051
> ( 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)
1053
1053
}
1054
1054
% end
1055
1055
0 commit comments