@@ -867,11 +867,16 @@ extension ${Self} {
867
867
/// `body`: it may not appear to have its correct value. Instead,
868
868
/// use only the `UnsafeMutableBufferPointer` argument to `body`.
869
869
@_semantics( " array.withUnsafeMutableBufferPointer " )
870
+ @inline ( __always) // Performance: This method should get inlined into the
871
+ // caller such that we can combine the partial apply with the apply in this
872
+ // function saving on allocating a closure context. This becomes unnecessary
873
+ // once we allocate noescape closures on the stack.
870
874
public mutating func withUnsafeMutableBufferPointer< R> (
871
875
@noescape body: ( inout UnsafeMutableBufferPointer< Element > ) throws -> R
872
876
) rethrows -> R {
877
+ let count = self . count
873
878
// Ensure unique storage
874
- _arrayReserve ( & _buffer, 0 )
879
+ _outlinedMakeUniqueBuffer ( & _buffer, bufferCount : count )
875
880
876
881
// Ensure that body can't invalidate the storage or its bounds by
877
882
// moving self into a temporary working array.
@@ -887,7 +892,6 @@ extension ${Self} {
887
892
888
893
// Create an UnsafeBufferPointer over work that we can pass to body
889
894
let pointer = work. _buffer. firstElementAddress
890
- let count = work. count
891
895
var inoutBufferPointer = UnsafeMutableBufferPointer (
892
896
start: pointer, count: count)
893
897
@@ -1190,6 +1194,23 @@ internal struct _IgnorePointer<T> : _PointerFunctionType {
1190
1194
}
1191
1195
}
1192
1196
1197
+ @inline ( never)
1198
+ internal func _outlinedMakeUniqueBuffer<
1199
+ _Buffer : _ArrayBufferType where _Buffer. Index == Int
1200
+ > (
1201
+ buffer: inout _Buffer , bufferCount: Int
1202
+ ) {
1203
+ if _fastPath (
1204
+ buffer. requestUniqueMutableBackingBuffer ( bufferCount) != nil ) {
1205
+ return
1206
+ }
1207
+
1208
+ var newBuffer = Optional (
1209
+ _forceCreateUniqueMutableBuffer (
1210
+ & buffer, newCount: bufferCount, requiredCapacity: bufferCount) )
1211
+ _arrayOutOfPlaceUpdate ( & buffer, & newBuffer, bufferCount, 0 , _IgnorePointer ( ) )
1212
+ }
1213
+
1193
1214
internal func _arrayReserve<
1194
1215
_Buffer : _ArrayBufferType where _Buffer. Index == Int
1195
1216
> (
0 commit comments