Skip to content

Commit c5bbfa2

Browse files
authored
Merge pull request #3435 from apple/stdlib-fix-type-of-copyToNativeArrayBuffer
stdlib: change Collection._copyToNativeArrayBuffer() to be defined in terms of public types
2 parents f8465fa + 455d139 commit c5bbfa2

16 files changed

+76
-88
lines changed

stdlib/private/StdlibCollectionUnittest/LoggingWrappers.swift.gyb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public class SequenceLog {
8989
public static var split = TypeIndexed(0)
9090
public static var _customContainsEquatableElement = TypeIndexed(0)
9191
public static var _preprocessingPass = TypeIndexed(0)
92-
public static var _copyToNativeArrayBuffer = TypeIndexed(0)
92+
public static var _copyToContiguousArray = TypeIndexed(0)
9393
public static var _copyContents = TypeIndexed(0)
9494
}
9595

@@ -302,10 +302,10 @@ public struct ${Self}<
302302

303303
/// Create a native array buffer containing the elements of `self`,
304304
/// in the same order.
305-
public func _copyToNativeArrayBuffer()
306-
-> _ContiguousArrayBuffer<Base.Iterator.Element> {
307-
Log._copyToNativeArrayBuffer[selfType] += 1
308-
return base._copyToNativeArrayBuffer()
305+
public func _copyToContiguousArray()
306+
-> ContiguousArray<Base.Iterator.Element> {
307+
Log._copyToContiguousArray[selfType] += 1
308+
return base._copyToContiguousArray()
309309
}
310310

311311
/// Copy a Sequence into an array.

stdlib/public/core/Arrays.swift.gyb

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ extension ${Self} : ArrayLiteralConvertible {
936936
///
937937
/// - Parameter elements: A variadic list of elements of the new array.
938938
public init(arrayLiteral elements: Element...) {
939-
self.init(_buffer: _extractOrCopyToNativeArrayBuffer(elements._buffer))
939+
self.init(_buffer: ContiguousArray(elements)._buffer)
940940
}
941941
%end
942942
}
@@ -1037,7 +1037,8 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
10371037
public init<S : Sequence>(_ s: S)
10381038
where S.Iterator.Element == Element {
10391039

1040-
self = ${Self}(_buffer: _Buffer(s._copyToNativeArrayBuffer(),
1040+
self = ${Self}(
1041+
_buffer: _Buffer(s._copyToContiguousArray()._buffer,
10411042
shiftedToStartIndex: 0))
10421043
}
10431044

@@ -1426,8 +1427,11 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
14261427
}
14271428
}
14281429

1429-
public func _copyToNativeArrayBuffer() -> _ContiguousArrayBuffer<Element> {
1430-
return _extractOrCopyToNativeArrayBuffer(self._buffer)
1430+
public func _copyToContiguousArray() -> ContiguousArray<Element> {
1431+
if let n = _buffer.requestNativeBuffer() {
1432+
return ContiguousArray(_buffer: n)
1433+
}
1434+
return _copyCollectionToContiguousArray(_buffer)
14311435
}
14321436
}
14331437

@@ -1487,10 +1491,9 @@ extension ${Self} {
14871491
if _fastPath(p != nil || isEmpty) {
14881492
return (_owner, UnsafePointer(p))
14891493
}
1490-
let n = _extractOrCopyToNativeArrayBuffer(self._buffer)
1494+
let n = ContiguousArray(self._buffer)._buffer
14911495
return (n.owner, UnsafePointer(n.firstElementAddress))
14921496
}
1493-
14941497
}
14951498

14961499
extension ${Self} {
@@ -1976,20 +1979,6 @@ internal func _arrayReserve<_Buffer>(
19761979
_arrayOutOfPlaceUpdate(&buffer, &newBuffer, count, 0, _IgnorePointer())
19771980
}
19781981

1979-
public // SPI(Foundation)
1980-
func _extractOrCopyToNativeArrayBuffer<Buffer>(
1981-
_ source: Buffer
1982-
) -> _ContiguousArrayBuffer<Buffer.Iterator.Element>
1983-
where
1984-
Buffer : _ArrayBufferProtocol,
1985-
Buffer.Iterator.Element == Buffer.Element {
1986-
1987-
if let n = source.requestNativeBuffer() {
1988-
return n
1989-
}
1990-
return _copyCollectionToNativeArrayBuffer(source)
1991-
}
1992-
19931982
/// Append items from `newItems` to `buffer`.
19941983
internal func _arrayAppendSequence<Buffer, S>(
19951984
_ buffer: inout Buffer, _ newItems: S

stdlib/public/core/ContiguousArrayBuffer.swift

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ final class _ContiguousArrayStorage<Element> : _ContiguousArrayStorage1 {
183183
}
184184

185185
@_fixed_layout
186-
public struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
186+
public // @testable
187+
struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
187188

188189
/// Make a buffer with uninitialized elements. After using this
189190
/// method, you must either initialize the `count` elements at the
@@ -539,15 +540,14 @@ extension _ContiguousArrayBuffer : RandomAccessCollection {
539540
}
540541

541542
extension Sequence {
542-
public func _copyToNativeArrayBuffer()
543-
-> _ContiguousArrayBuffer<Iterator.Element> {
544-
return _copySequenceToNativeArrayBuffer(self)
543+
public func _copyToContiguousArray() -> ContiguousArray<Iterator.Element> {
544+
return _copySequenceToContiguousArray(self)
545545
}
546546
}
547547

548-
internal func _copySequenceToNativeArrayBuffer<
548+
internal func _copySequenceToContiguousArray<
549549
S : Sequence
550-
>(_ source: S) -> _ContiguousArrayBuffer<S.Iterator.Element> {
550+
>(_ source: S) -> ContiguousArray<S.Iterator.Element> {
551551
let initialCapacity = source.underestimatedCount
552552
var builder =
553553
_UnsafePartiallyInitializedContiguousArrayBuffer<S.Iterator.Element>(
@@ -571,33 +571,32 @@ internal func _copySequenceToNativeArrayBuffer<
571571
}
572572

573573
extension Collection {
574-
public func _copyToNativeArrayBuffer(
575-
) -> _ContiguousArrayBuffer<Iterator.Element> {
576-
return _copyCollectionToNativeArrayBuffer(self)
574+
public func _copyToContiguousArray() -> ContiguousArray<Iterator.Element> {
575+
return _copyCollectionToContiguousArray(self)
577576
}
578577
}
579578

580579
extension _ContiguousArrayBuffer {
581-
public func _copyToNativeArrayBuffer() -> _ContiguousArrayBuffer<Element> {
582-
return self
580+
public func _copyToContiguousArray() -> ContiguousArray<Element> {
581+
return ContiguousArray(_buffer: self)
583582
}
584583
}
585584

586-
/// This is a fast implementation of _copyToNativeArrayBuffer() for collections.
585+
/// This is a fast implementation of _copyToContiguousArray() for collections.
587586
///
588587
/// It avoids the extra retain, release overhead from storing the
589588
/// ContiguousArrayBuffer into
590589
/// _UnsafePartiallyInitializedContiguousArrayBuffer. Since we do not support
591590
/// ARC loops, the extra retain, release overhead cannot be eliminated which
592591
/// makes assigning ranges very slow. Once this has been implemented, this code
593592
/// should be changed to use _UnsafePartiallyInitializedContiguousArrayBuffer.
594-
internal func _copyCollectionToNativeArrayBuffer<
593+
internal func _copyCollectionToContiguousArray<
595594
C : Collection
596-
>(_ source: C) -> _ContiguousArrayBuffer<C.Iterator.Element>
595+
>(_ source: C) -> ContiguousArray<C.Iterator.Element>
597596
{
598597
let count: Int = numericCast(source.count)
599598
if count == 0 {
600-
return _ContiguousArrayBuffer()
599+
return ContiguousArray()
601600
}
602601

603602
let result = _ContiguousArrayBuffer<C.Iterator.Element>(
@@ -613,7 +612,7 @@ internal func _copyCollectionToNativeArrayBuffer<
613612
p += 1
614613
}
615614
_expectEnd(i, source)
616-
return result
615+
return ContiguousArray(_buffer: result)
617616
}
618617

619618
/// A "builder" interface for initializing array buffers.
@@ -678,7 +677,7 @@ internal struct _UnsafePartiallyInitializedContiguousArrayBuffer<Element> {
678677
/// Returns the fully-initialized buffer. `self` is reset to contain an
679678
/// empty buffer and cannot be used afterward.
680679
@inline(__always) // For performance reasons.
681-
mutating func finish() -> _ContiguousArrayBuffer<Element> {
680+
mutating func finish() -> ContiguousArray<Element> {
682681
// Adjust the initialized count of the buffer.
683682
result.count = result.capacity - remainingCapacity
684683

@@ -692,12 +691,12 @@ internal struct _UnsafePartiallyInitializedContiguousArrayBuffer<Element> {
692691
/// Returns the fully-initialized buffer. `self` is reset to contain an
693692
/// empty buffer and cannot be used afterward.
694693
@inline(__always) // For performance reasons.
695-
mutating func finishWithOriginalCount() -> _ContiguousArrayBuffer<Element> {
694+
mutating func finishWithOriginalCount() -> ContiguousArray<Element> {
696695
_sanityCheck(remainingCapacity == result.capacity - result.count,
697696
"_UnsafePartiallyInitializedContiguousArrayBuffer has incorrect count")
698697
var finalResult = _ContiguousArrayBuffer<Element>()
699698
swap(&finalResult, &result)
700699
remainingCapacity = 0
701-
return finalResult
700+
return ContiguousArray(_buffer: finalResult)
702701
}
703702
}

stdlib/public/core/ExistentialCollection.swift.gyb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ internal class _AnyRandomAccessCollectionBox<Element>
174174
_abstract()
175175
}
176176

177-
internal func __copyToNativeArrayBuffer() -> _ContiguousArrayStorageBase {
177+
internal func __copyToContiguousArray() -> ContiguousArray<Element> {
178178
_abstract()
179179
}
180180

@@ -358,8 +358,8 @@ internal final class _${Kind}Box<S : ${Kind}> : _Any${Kind}Box<S.Iterator.Elemen
358358
) rethrows -> R? {
359359
return try _base._preprocessingPass(preprocess)
360360
}
361-
internal override func __copyToNativeArrayBuffer() -> _ContiguousArrayStorageBase {
362-
return _base._copyToNativeArrayBuffer()._storage
361+
internal override func __copyToContiguousArray() -> ContiguousArray<Element> {
362+
return _base._copyToContiguousArray()
363363
}
364364
internal override func __copyContents(initializing ptr: UnsafeMutablePointer<Element>)
365365
-> UnsafeMutablePointer<Element> {
@@ -627,8 +627,8 @@ extension Any${Kind} {
627627
return try _box.__preprocessingPass(preprocess)
628628
}
629629

630-
public func _copyToNativeArrayBuffer() -> _ContiguousArrayBuffer<Element> {
631-
return _ContiguousArrayBuffer(self._box.__copyToNativeArrayBuffer())
630+
public func _copyToContiguousArray() -> ContiguousArray<Element> {
631+
return self._box.__copyToContiguousArray()
632632
}
633633

634634
public func _copyContents(initializing ptr: UnsafeMutablePointer<Element>)

stdlib/public/core/Flatten.swift.gyb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,14 @@ public struct ${Collection}<Base> : ${collectionForTraversal(traversal)}
336336
// just return zero.
337337
public var underestimatedCount: Int { return 0 }
338338

339-
public func _copyToNativeArrayBuffer()
340-
-> _ContiguousArrayBuffer<Base.Iterator.Element.Iterator.Element> {
339+
public func _copyToContiguousArray()
340+
-> ContiguousArray<Base.Iterator.Element.Iterator.Element> {
341341

342-
// The default implementation of `_copyToNativeArrayBuffer` queries the
342+
// The default implementation of `_copyToContiguousArray` queries the
343343
// `count` property, which materializes every inner collection. This is a
344344
// bad default for `flatMap()`. So we treat `self` as a sequence and only
345345
// rely on underestimated count.
346-
return _copySequenceToNativeArrayBuffer(self)
346+
return _copySequenceToContiguousArray(self)
347347
}
348348

349349
// TODO: swift-3-indexing-model - add docs

stdlib/public/core/Join.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ public struct JoinedSequence<Base : Sequence> : Sequence
110110
separator: _separator)
111111
}
112112

113-
public func _copyToNativeArrayBuffer()
114-
-> _ContiguousArrayBuffer<Base.Iterator.Element.Iterator.Element> {
113+
public func _copyToContiguousArray()
114+
-> ContiguousArray<Base.Iterator.Element.Iterator.Element> {
115115
var result = ContiguousArray<Iterator.Element>()
116116
let separatorSize: Int = numericCast(_separator.count)
117117

@@ -132,7 +132,7 @@ public struct JoinedSequence<Base : Sequence> : Sequence
132132
for x in _base {
133133
result.append(contentsOf: x)
134134
}
135-
return result._buffer
135+
return result
136136
}
137137

138138
var iter = _base.makeIterator()
@@ -144,7 +144,7 @@ public struct JoinedSequence<Base : Sequence> : Sequence
144144
}
145145
}
146146

147-
return result._buffer
147+
return result
148148
}
149149

150150
internal var _base: Base

stdlib/public/core/LazyCollection.swift.gyb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ extension ${Self} : Sequence {
8989
/// - Complexity: O(N).
9090
public var underestimatedCount: Int { return _base.underestimatedCount }
9191

92-
public func _copyToNativeArrayBuffer()
93-
-> _ContiguousArrayBuffer<Base.Iterator.Element> {
94-
return _base._copyToNativeArrayBuffer()
92+
public func _copyToContiguousArray()
93+
-> ContiguousArray<Base.Iterator.Element> {
94+
return _base._copyToContiguousArray()
9595
}
9696

9797
@discardableResult

stdlib/public/core/Sequence.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ public protocol Sequence {
584584

585585
/// Create a native array buffer containing the elements of `self`,
586586
/// in the same order.
587-
func _copyToNativeArrayBuffer() -> _ContiguousArrayBuffer<Iterator.Element>
587+
func _copyToContiguousArray() -> ContiguousArray<Iterator.Element>
588588

589589
/// Copy a Sequence into an array, returning one past the last
590590
/// element initialized.

stdlib/public/core/SequenceWrapper.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ extension Sequence
110110

111111
/// Create a native array buffer containing the elements of `self`,
112112
/// in the same order.
113-
public func _copyToNativeArrayBuffer()
114-
-> _ContiguousArrayBuffer<Base.Iterator.Element> {
115-
return _base._copyToNativeArrayBuffer()
113+
public func _copyToContiguousArray()
114+
-> ContiguousArray<Base.Iterator.Element> {
115+
return _base._copyToContiguousArray()
116116
}
117117

118118
/// Copy a Sequence into an array, returning one past the last

stdlib/public/core/SliceBuffer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,11 @@ struct _SliceBuffer<Element> : _ArrayBufferProtocol, RandomAccessCollection {
327327
}
328328

329329
extension _SliceBuffer {
330-
public func _copyToNativeArrayBuffer() -> _ContiguousArrayBuffer<Element> {
330+
public func _copyToContiguousArray() -> ContiguousArray<Element> {
331331
if _hasNativeBuffer {
332332
let n = nativeBuffer
333333
if count == n.count {
334-
return n
334+
return ContiguousArray(_buffer: n)
335335
}
336336
}
337337

@@ -340,6 +340,6 @@ extension _SliceBuffer {
340340
minimumCapacity: 0)
341341
result.firstElementAddress.initializeFrom(
342342
firstElementAddress, count: count)
343-
return result
343+
return ContiguousArray(_buffer: result)
344344
}
345345
}

0 commit comments

Comments
 (0)