Skip to content

Commit 8b6bd96

Browse files
committed
Remove stray comment and use a stack buffer for sequence appending
1 parent d7b0fac commit 8b6bd96

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

stdlib/public/SDK/Foundation/Data.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,6 @@ public final class _DataStorage {
444444

445445
}
446446

447-
////////////***** AUDITED UP TO HERE
448-
449447
// fast-path for appending directly from another data storage
450448
@inline(__always)
451449
public func append(_ otherData: _DataStorage, startingAt start: Int, endingAt end: Int) {
@@ -1469,13 +1467,19 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
14691467
@inline(__always)
14701468
public mutating func append<S : Sequence>(contentsOf newElements: S) where S.Iterator.Element == Iterator.Element {
14711469
let estimatedCount = newElements.underestimatedCount
1472-
var ptr = UnsafeMutablePointer<UInt8>.allocate(capacity: estimatedCount)
1473-
defer { ptr.deallocate(capacity: estimatedCount) }
1474-
let buffer = UnsafeMutableBufferPointer(start: ptr, count: estimatedCount)
1475-
var (iterator, endPoint) = newElements._copyContents(initializing: buffer)
1476-
append(ptr, count: endPoint - buffer.startIndex)
1477-
while let byte = iterator.next() {
1478-
append(byte)
1470+
guard estimatedCount > 0 else {
1471+
for byte in newElements {
1472+
append(byte)
1473+
}
1474+
return
1475+
}
1476+
_withStackOrHeapBuffer(estimatedCount) { allocation in
1477+
let buffer = UnsafeMutableBufferPointer(start: allocation.pointee.memory.assumingMemoryBound(to: UInt8.self), count: estimatedCount)
1478+
var (iterator, endPoint) = newElements._copyContents(initializing: buffer)
1479+
append(buffer.baseAddress!, count: endPoint - buffer.startIndex)
1480+
while let byte = iterator.next() {
1481+
append(byte)
1482+
}
14791483
}
14801484
}
14811485

0 commit comments

Comments
 (0)