[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

@@ -948,7 +948,7 @@ StringTests.test("stringGutsReserve")
base._guts._objectIdentifier != nil &&
isUnique
base.reserveCapacity(0)
base.reserveCapacity(16)
// Now it's unique
// If it was already native and unique, no reallocation
@@ -1065,11 +1065,11 @@ StringTests.test("reserveCapacity") {
expectNotEqual(id0, s.bufferID)
s = ""
print("empty capacity \(s.capacity)")
s.reserveCapacity(oldCap + 2)
print("reserving \(oldCap + 2) -> \(s.capacity), width = \(s._guts.byteWidth)")
s.reserveCapacity(oldCap + 18)
print("reserving \(oldCap + 18) -> \(s.capacity), width = \(s._guts.byteWidth)")
let id1 = s.bufferID
s.insert(contentsOf: repeatElement(x, count: oldCap + 2), at: s.endIndex)
print("extending by \(oldCap + 2) -> \(s.capacity), width = \(s._guts.byteWidth)")
s.insert(contentsOf: repeatElement(x, count: oldCap + 18), at: s.endIndex)
print("extending by \(oldCap + 18) -> \(s.capacity), width = \(s._guts.byteWidth)")
expectEqual(id1, s.bufferID)
s.insert(contentsOf: repeatElement(x, count: s.capacity + 100), at: s.endIndex)
expectNotEqual(id1, s.bufferID)