@@ -444,8 +444,6 @@ public final class _DataStorage {
444
444
445
445
}
446
446
447
- ////////////***** AUDITED UP TO HERE
448
-
449
447
// fast-path for appending directly from another data storage
450
448
@inline ( __always)
451
449
public func append( _ otherData: _DataStorage , startingAt start: Int , endingAt end: Int ) {
@@ -1469,13 +1467,19 @@ public struct Data : ReferenceConvertible, Equatable, Hashable, RandomAccessColl
1469
1467
@inline ( __always)
1470
1468
public mutating func append< S : Sequence > ( contentsOf newElements: S ) where S. Iterator. Element == Iterator . Element {
1471
1469
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
+ }
1479
1483
}
1480
1484
}
1481
1485
0 commit comments