[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:
Michael Ilseman
2018-05-17 14:27:02 -07:00
committed by Michael Ilseman
parent 440c5b34ba
commit 1fe5fb717d
3 changed files with 18 additions and 16 deletions

View File

@@ -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)
}