Skip to content

Commit 2642eab

Browse files
authored
Merge pull request #3143 from benlangmuir/stdlib-array-buffer-init
[stdlib] Stop copying the _ContiguousArrayBuffer when initializing an ArraySlice from a literal
2 parents ec15e34 + 90b3625 commit 2642eab

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

stdlib/public/core/ArrayCast.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public func _arrayForceCast<SourceElement, TargetElement>(
5656
if _fastPath(native!.storesOnlyElementsOfType(TargetElement.self)) {
5757
// A native buffer that is known to store only elements of the
5858
// TargetElement can be used directly
59-
return Array(source._buffer.cast(toBufferOf: TargetElement.self))
59+
return Array(_buffer: source._buffer.cast(toBufferOf: TargetElement.self))
6060
}
6161
// Other native buffers must use deferred element type checking
62-
return Array(
62+
return Array(_buffer:
6363
source._buffer.downcast(
6464
toBufferWithDeferredTypeCheckOf: TargetElement.self))
6565
}
@@ -90,7 +90,7 @@ public func _arrayForceCast<SourceElement, TargetElement>(
9090
p += 1
9191
}
9292
}
93-
return Array(_ArrayBuffer(buf, shiftedToStartIndex: 0))
93+
return Array(_buffer: _ArrayBuffer(buf, shiftedToStartIndex: 0))
9494

9595
case (.value, .explicit):
9696
_sanityCheckFailure(
@@ -121,7 +121,7 @@ internal func _arrayConditionalDownCastElements<SourceElement, TargetElement>(
121121

122122
if _fastPath(native != nil) {
123123
if native!.storesOnlyElementsOfType(TargetElement.self) {
124-
return Array(a._buffer.cast(toBufferOf: TargetElement.self))
124+
return Array(_buffer: a._buffer.cast(toBufferOf: TargetElement.self))
125125
}
126126
return nil
127127
}
@@ -136,7 +136,7 @@ internal func _arrayConditionalDownCastElements<SourceElement, TargetElement>(
136136
}
137137
}
138138
}
139-
return Array(a._buffer.cast(toBufferOf: TargetElement.self))
139+
return Array(_buffer: a._buffer.cast(toBufferOf: TargetElement.self))
140140
}
141141
return []
142142
}
@@ -169,7 +169,7 @@ ElementwiseBridging:
169169
p.initialize(with: value!)
170170
p += 1
171171
}
172-
return Array(_ArrayBuffer(buf, shiftedToStartIndex: 0))
172+
return Array(_buffer: _ArrayBuffer(buf, shiftedToStartIndex: 0))
173173
}
174174
while false
175175

stdlib/public/core/Arrays.swift.gyb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ public struct ${Self}<Element>
732732
get {
733733
_checkIndex(bounds.lowerBound)
734734
_checkIndex(bounds.upperBound)
735-
return ArraySlice(_buffer[bounds])
735+
return ArraySlice(_buffer: _buffer[bounds])
736736
}
737737
set(rhs) {
738738
_checkIndex(bounds.lowerBound)
@@ -884,10 +884,20 @@ public struct ${Self}<Element>
884884

885885
/// Initialization from an existing buffer does not have "array.init"
886886
/// semantics because the caller may retain an alias to buffer.
887-
public init(_ _buffer: _Buffer) {
887+
public // @testable
888+
init(_buffer: _Buffer) {
888889
self._buffer = _buffer
889890
}
890891

892+
%if Self == 'ArraySlice':
893+
/// Initialization from an existing buffer does not have "array.init"
894+
/// semantics because the caller may retain an alias to buffer.
895+
public // @testable
896+
init(_buffer: _ContiguousArrayBuffer<Element>) {
897+
self.init(_buffer: _Buffer(_buffer, shiftedToStartIndex: 0))
898+
}
899+
%end
900+
891901
public var _buffer: _Buffer
892902
}
893903

@@ -926,7 +936,7 @@ extension ${Self} : ArrayLiteralConvertible {
926936
///
927937
/// - Parameter elements: A variadic list of elements of the new array.
928938
public init(arrayLiteral elements: Element...) {
929-
self.init(_extractOrCopyToNativeArrayBuffer(elements._buffer))
939+
self.init(_buffer: _extractOrCopyToNativeArrayBuffer(elements._buffer))
930940
}
931941
%end
932942
}
@@ -1027,7 +1037,7 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
10271037
public init<S : Sequence>(_ s: S)
10281038
where S.Iterator.Element == Element {
10291039

1030-
self = ${Self}(_Buffer(s._copyToNativeArrayBuffer(),
1040+
self = ${Self}(_buffer: _Buffer(s._copyToNativeArrayBuffer(),
10311041
shiftedToStartIndex: 0))
10321042
}
10331043

@@ -1114,7 +1124,7 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
11141124
storage, to: _ContiguousArrayStorage<Element>.self))
11151125

11161126
return (
1117-
Array(_Buffer(innerBuffer, shiftedToStartIndex: 0)),
1127+
Array(_buffer: _Buffer(innerBuffer, shiftedToStartIndex: 0)),
11181128
innerBuffer.firstElementAddress)
11191129
}
11201130

@@ -2082,7 +2092,7 @@ public func != <Element : Equatable>(
20822092
public func _arrayUpCast<Derived, Base>(_ a: Array<Derived>) -> Array<Base> {
20832093
// FIXME: Dynamic casting is currently not possible without the objc runtime:
20842094
// rdar://problem/18801510
2085-
return Array(a._buffer.cast(toBufferOf: Base.self))
2095+
return Array(_buffer: a._buffer.cast(toBufferOf: Base.self))
20862096
}
20872097
#endif
20882098

@@ -2113,7 +2123,7 @@ extension Array {
21132123
/// * `Element` is bridged verbatim to Objective-C (i.e.,
21142124
/// is a reference type).
21152125
public init(_immutableCocoaArray: _NSArrayCore) {
2116-
self = Array(_ArrayBuffer(nsArray: _immutableCocoaArray))
2126+
self = Array(_buffer: _ArrayBuffer(nsArray: _immutableCocoaArray))
21172127
}
21182128
}
21192129
#endif

0 commit comments

Comments
 (0)