@@ -225,13 +225,12 @@ extension _StringGuts {
225
225
226
226
// TODO(String performance): Local small stack first, before making large
227
227
// array. Also, make a smaller initial array and grow over time.
228
- var codeUnits = Array < UInt16 > ( repeating: 0 , count: count)
229
-
230
- codeUnits. withUnsafeMutableBufferPointer {
231
- _cocoaStringCopyCharacters (
232
- from: cocoa,
233
- range: 0 ..< count,
234
- into: $0. baseAddress. _unsafelyUnwrappedUnchecked)
228
+ let codeUnits = Array < UInt16 > ( unsafeUninitializedCapacity: count) { buf, initializedCount in
229
+ _cocoaStringCopyCharacters (
230
+ from: cocoa,
231
+ range: 0 ..< count,
232
+ into: buf. baseAddress. _unsafelyUnwrappedUnchecked)
233
+ initializedCount = count
235
234
}
236
235
return codeUnits. withUnsafeBufferPointer {
237
236
_measureCharacterStrideICU ( of: $0, startingAt: i)
@@ -291,13 +290,12 @@ extension _StringGuts {
291
290
292
291
// TODO(String performance): Local small stack first, before making large
293
292
// array. Also, make a smaller initial array and grow over time.
294
- var codeUnits = Array < UInt16 > ( repeating: 0 , count: count)
295
-
296
- codeUnits. withUnsafeMutableBufferPointer {
297
- _cocoaStringCopyCharacters (
298
- from: cocoa,
299
- range: 0 ..< count,
300
- into: $0. baseAddress. _unsafelyUnwrappedUnchecked)
293
+ let codeUnits = Array < UInt16 > ( unsafeUninitializedCapacity: count) { buf, initializedCount in
294
+ _cocoaStringCopyCharacters (
295
+ from: cocoa,
296
+ range: 0 ..< count,
297
+ into: buf. baseAddress. _unsafelyUnwrappedUnchecked)
298
+ initializedCount = count
301
299
}
302
300
return codeUnits. withUnsafeBufferPointer {
303
301
_measureCharacterStrideICU ( of: $0, endingAt: i)
0 commit comments