Skip to content

Commit 3820393

Browse files
lorenteymilseman
authored andcommitted
[stdlib] Ensure that reserved capacity survives CoW copies (#46)
This shouldn’t really be necessary, but it makes sense to wait until we can reclaim memory like this consistently across the entire stdlib.
1 parent d112e5f commit 3820393

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

stdlib/public/core/StringGutsRangeReplaceable.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,10 @@ extension _StringGuts {
233233

234234
// TODO(cleanup): Add append on guts taking range, use that
235235
var result = String()
236-
result.reserveCapacity(self.count &- (upperOffset &- lowerOffset))
236+
// FIXME: It should be okay to get rid of excess capacity
237+
// here. rdar://problem/45635432
238+
result.reserveCapacity(
239+
nativeCapacity ?? (count &- (upperOffset &- lowerOffset)))
237240
result.append(contentsOf: String(self)[..<lower])
238241
result.append(contentsOf: String(self)[upper...])
239242
self = result._guts
@@ -258,6 +261,11 @@ extension _StringGuts {
258261
}
259262

260263
var result = String()
264+
// FIXME: It should be okay to get rid of excess capacity
265+
// here. rdar://problem/45635432
266+
if let capacity = self.nativeCapacity {
267+
result.reserveCapacity(capacity)
268+
}
261269
let selfStr = String(self)
262270
result.append(contentsOf: selfStr[..<bounds.lowerBound])
263271
result.append(contentsOf: newElements)

0 commit comments

Comments
 (0)