Skip to content

Commit bda8af0

Browse files
committed
stdlib: simplify Array/ContiguousArray's append(contentsOf:)
Instead of calling ArrayBuffer's _arrayAppendSequence, inline the code. This saves some code size.
1 parent 88a9ebb commit bda8af0

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

stdlib/public/core/Array.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ extension Array: RangeReplaceableCollection {
12131213
start: startNewElements,
12141214
count: self.capacity - oldCount)
12151215

1216-
let (remainder,writtenUpTo) = buf.initialize(from: newElements)
1216+
var (remainder,writtenUpTo) = buf.initialize(from: newElements)
12171217

12181218
// trap on underflow from the sequence's underestimate:
12191219
let writtenCount = buf.distance(from: buf.startIndex, to: writtenUpTo)
@@ -1229,7 +1229,22 @@ extension Array: RangeReplaceableCollection {
12291229
if writtenUpTo == buf.endIndex {
12301230
// there may be elements that didn't fit in the existing buffer,
12311231
// append them in slow sequence-only mode
1232-
_buffer._arrayAppendSequence(IteratorSequence(remainder))
1232+
var newCount = _getCount()
1233+
var nextItem = remainder.next()
1234+
while nextItem != nil {
1235+
reserveCapacityForAppend(newElementsCount: 1)
1236+
1237+
let currentCapacity = _getCapacity()
1238+
let base = _buffer.firstElementAddress
1239+
1240+
// fill while there is another item and spare capacity
1241+
while let next = nextItem, newCount < currentCapacity {
1242+
(base + newCount).initialize(to: next)
1243+
newCount += 1
1244+
nextItem = remainder.next()
1245+
}
1246+
_buffer.count = newCount
1247+
}
12331248
}
12341249
}
12351250

stdlib/public/core/ContiguousArray.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ extension ContiguousArray: RangeReplaceableCollection {
845845
start: startNewElements,
846846
count: self.capacity - oldCount)
847847

848-
let (remainder,writtenUpTo) = buf.initialize(from: newElements)
848+
var (remainder,writtenUpTo) = buf.initialize(from: newElements)
849849

850850
// trap on underflow from the sequence's underestimate:
851851
let writtenCount = buf.distance(from: buf.startIndex, to: writtenUpTo)
@@ -861,7 +861,22 @@ extension ContiguousArray: RangeReplaceableCollection {
861861
if writtenUpTo == buf.endIndex {
862862
// there may be elements that didn't fit in the existing buffer,
863863
// append them in slow sequence-only mode
864-
_buffer._arrayAppendSequence(IteratorSequence(remainder))
864+
var newCount = _getCount()
865+
var nextItem = remainder.next()
866+
while nextItem != nil {
867+
reserveCapacityForAppend(newElementsCount: 1)
868+
869+
let currentCapacity = _getCapacity()
870+
let base = _buffer.firstElementAddress
871+
872+
// fill while there is another item and spare capacity
873+
while let next = nextItem, newCount < currentCapacity {
874+
(base + newCount).initialize(to: next)
875+
newCount += 1
876+
nextItem = remainder.next()
877+
}
878+
_buffer.count = newCount
879+
}
865880
}
866881
}
867882

0 commit comments

Comments
 (0)