Skip to content

Commit b42bce4

Browse files
committed
stdlib: changes for the StringOptimization
To be able to constant fold string interpolation, the right semantic attributes must be in place. Also, the interpolation's write function must be inlinable. For the _typeName constant folding, a semantic attribute is required.
1 parent b2f8fa8 commit b42bce4

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

stdlib/public/core/Misc.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public func _getTypeName(_ type: Any.Type, qualified: Bool)
7171
-> (UnsafePointer<UInt8>, Int)
7272

7373
/// Returns the demangled qualified name of a metatype.
74+
@_semantics("typeName")
7475
public // @testable
7576
func _typeName(_ type: Any.Type, qualified: Bool = true) -> String {
7677
let (stringPtr, count) = _getTypeName(type, qualified: qualified)

stdlib/public/core/OutputStream.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ extension String: TextOutputStreamable {
557557
/// Writes the string into the given output stream.
558558
///
559559
/// - Parameter target: An output stream.
560+
@inlinable
560561
public func write<Target: TextOutputStream>(to target: inout Target) {
561562
target.write(self)
562563
}

stdlib/public/core/String.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,18 @@ public struct String {
359359
_invariantCheck()
360360
}
361361

362+
// This is intentionally a static function and not an initializer, because
363+
// an initializer would conflict with the Int-parsing initializer, when used
364+
// as function name, e.g.
365+
// [1, 2, 3].map(String.init)
366+
@_alwaysEmitIntoClient
367+
@_semantics("string.init_empty_with_capacity")
368+
@_semantics("inline_late")
369+
@inlinable
370+
internal static func _createEmpty(withInitialCapacity: Int) -> String {
371+
return String(_StringGuts(_initialCapacity: withInitialCapacity))
372+
}
373+
362374
/// Creates an empty string.
363375
///
364376
/// Using this initializer is equivalent to initializing a string with an

stdlib/public/core/StringInterpolation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public struct DefaultStringInterpolation: StringInterpolationProtocol {
7575
let capacityPerInterpolation = 2
7676
let initialCapacity = literalCapacity +
7777
interpolationCount * capacityPerInterpolation
78-
_storage = String(_StringGuts(_initialCapacity: initialCapacity))
78+
_storage = String._createEmpty(withInitialCapacity: initialCapacity)
7979
}
8080

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

0 commit comments

Comments
 (0)