Skip to content

Commit ed44e2f

Browse files
committed
Create interpolation strings with appropriate initial capacity
Should avoid relying so heavily on the optimizer realizing it can remove branches. This had somewhat complicated performance impacts in local benchmarking; we’ll see what it does on CI.
1 parent 4b31761 commit ed44e2f

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

stdlib/public/core/StringGuts.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -910,23 +910,31 @@ extension _StringGuts {
910910
_invariantCheck()
911911
}
912912

913-
@inlinable // @testable
914-
mutating func reserveCapacity(_ capacity: Int) {
915-
// Small strings can accomodate small capacities
913+
@inlinable internal
914+
init(_initialCapacity capacity: Int) {
916915
if capacity <= _SmallUTF8String.capacity {
917-
return
916+
self.init()
917+
} else {
918+
let storage = _SwiftStringStorage<UInt8>.create(
919+
capacity: capacity,
920+
count: 0)
921+
self.init(_large: storage)
918922
}
919-
reserveCapacitySlow(capacity)
920923
}
921924

922-
@usableFromInline
923-
mutating func reserveCapacitySlow(_ capacity: Int) {
925+
@usableFromInline // @testable
926+
mutating func reserveCapacity(_ capacity: Int) {
924927
if _fastPath(_isUniqueNative()) {
925928
if _fastPath(_object.nativeRawStorage.capacity >= capacity) {
926929
return
927930
}
928931
}
929932

933+
// Small strings can accomodate small capacities
934+
if capacity <= _SmallUTF8String.capacity {
935+
return
936+
}
937+
930938
let selfCount = self.count
931939
if isASCII {
932940
let storage = _copyToNativeStorage(

stdlib/public/core/StringInterpolation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
public struct DefaultStringInterpolation: StringInterpolationProtocol {
6464
/// The string contents accumulated by this instance.
6565
@usableFromInline
66-
internal var _storage: String = ""
66+
internal var _storage: String
6767

6868
/// Creates a string interpolation with storage pre-sized for a literal
6969
/// with the indicated attributes.
@@ -75,7 +75,7 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol {
7575
let capacityPerInterpolation = 2
7676
let initialCapacity = literalCapacity +
7777
interpolationCount * capacityPerInterpolation
78-
_storage.reserveCapacity(initialCapacity)
78+
_storage = String(_StringGuts(_initialCapacity: initialCapacity))
7979
}
8080

8181
/// Appends a literal segment of a string interpolation.

stdlib/public/core/StringRangeReplaceableCollection.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ extension String {
125125
/// to allocate.
126126
///
127127
/// - Complexity: O(*n*)
128-
@inlinable
129128
public mutating func reserveCapacity(_ n: Int) {
130129
_guts.reserveCapacity(n)
131130
}

0 commit comments

Comments
 (0)