Skip to content

Commit 36b41d4

Browse files
authored
Merge pull request #41869 from Catfish-Man/stack-promotion-and-ar-raise
Use withUnsafeTemporaryAllocation instead of a temporary Array
2 parents 552d4b0 + 9ad3c9a commit 36b41d4

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

stdlib/public/core/UnicodeHelpers.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -396,17 +396,15 @@ extension _StringGuts {
396396
).0))
397397
}
398398

399-
// TODO(String performance): Stack buffer if small enough
400-
let cus = Array<UInt16>(unsafeUninitializedCapacity: count) {
401-
buffer, initializedCapacity in
399+
return withUnsafeTemporaryAllocation(
400+
of: UInt16.self, capacity: count
401+
) { buffer in
402402
_cocoaStringCopyCharacters(
403403
from: self._object.cocoaObject,
404404
range: start..<end,
405-
into: buffer.baseAddress._unsafelyUnwrappedUnchecked)
406-
initializedCapacity = count
407-
}
408-
return cus.withUnsafeBufferPointer {
409-
return Character(String._uncheckedFromUTF16($0))
405+
into: buffer.baseAddress._unsafelyUnwrappedUnchecked
406+
)
407+
return Character(String._uncheckedFromUTF16(UnsafeBufferPointer(buffer)))
410408
}
411409
#else
412410
fatalError("No foreign strings on Linux in this version of Swift")

0 commit comments

Comments
 (0)