mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[string] Skip allocation in reserveCapacity if smol
If the requested capacity is small enough to fit in our small string representation, don't allocate a UTF-16 buffer, instead just return early.
This commit is contained in:
committed by
Michael Ilseman
parent
440c5b34ba
commit
1fe5fb717d
@@ -966,18 +966,22 @@ extension _StringGuts {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO (TODO: JIRA): check if we're small and still within capacity
|
||||
// Small strings can accomodate small capacities
|
||||
if capacity <= _SmallUTF8String.capacity {
|
||||
return
|
||||
}
|
||||
|
||||
let selfCount = self.count
|
||||
if isASCII {
|
||||
let storage = _copyToNativeStorage(
|
||||
of: UInt8.self,
|
||||
from: 0..<self.count,
|
||||
from: 0..<selfCount,
|
||||
unusedCapacity: Swift.max(capacity - count, 0))
|
||||
self = _StringGuts(_large: storage)
|
||||
} else {
|
||||
let storage = _copyToNativeStorage(
|
||||
of: UTF16.CodeUnit.self,
|
||||
from: 0..<self.count,
|
||||
from: 0..<selfCount,
|
||||
unusedCapacity: Swift.max(capacity - count, 0))
|
||||
self = _StringGuts(_large: storage)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user