Skip to content

Removes redundant buffer zeroing in StringGraphemeBreaking.swift by using `init(unsafeUninitializedCapacity:initializingWith:) #30132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions stdlib/public/core/StringGraphemeBreaking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,12 @@ extension _StringGuts {

// TODO(String performance): Local small stack first, before making large
// array. Also, make a smaller initial array and grow over time.
var codeUnits = Array<UInt16>(repeating: 0, count: count)

codeUnits.withUnsafeMutableBufferPointer {
_cocoaStringCopyCharacters(
from: cocoa,
range: 0..<count,
into: $0.baseAddress._unsafelyUnwrappedUnchecked)
let codeUnits = Array<UInt16>(unsafeUninitializedCapacity: count) { buf, initializedCount in
_cocoaStringCopyCharacters(
from: cocoa,
range: 0..<count,
into: buf.baseAddress._unsafelyUnwrappedUnchecked)
initializedCount = count
}
return codeUnits.withUnsafeBufferPointer {
_measureCharacterStrideICU(of: $0, startingAt: i)
Expand Down Expand Up @@ -291,13 +290,12 @@ extension _StringGuts {

// TODO(String performance): Local small stack first, before making large
// array. Also, make a smaller initial array and grow over time.
var codeUnits = Array<UInt16>(repeating: 0, count: count)

codeUnits.withUnsafeMutableBufferPointer {
_cocoaStringCopyCharacters(
from: cocoa,
range: 0..<count,
into: $0.baseAddress._unsafelyUnwrappedUnchecked)
let codeUnits = Array<UInt16>(unsafeUninitializedCapacity: count) { buf, initializedCount in
_cocoaStringCopyCharacters(
from: cocoa,
range: 0..<count,
into: buf.baseAddress._unsafelyUnwrappedUnchecked)
initializedCount = count
}
return codeUnits.withUnsafeBufferPointer {
_measureCharacterStrideICU(of: $0, endingAt: i)
Expand Down