From 1fe5fb717d88aebda9a834d02c6f8b45f3e0c246 Mon Sep 17 00:00:00 2001 From: Michael Ilseman Date: Thu, 17 May 2018 14:27:02 -0700 Subject: [PATCH] [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. --- stdlib/public/core/StringGuts.swift | 10 +++++++--- test/stdlib/NewStringAppending.swift | 14 ++++++-------- validation-test/stdlib/String.swift | 10 +++++----- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/stdlib/public/core/StringGuts.swift b/stdlib/public/core/StringGuts.swift index 7c7a02834db..203feceeee3 100644 --- a/stdlib/public/core/StringGuts.swift +++ b/stdlib/public/core/StringGuts.swift @@ -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.. \(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)