Skip to content

Commit d272fed

Browse files
committed
Disable a few assertions with non-trivial cost in release builds
This reduces the cost of enabling assertions in release builds to ~2%.
1 parent 3faac59 commit d272fed

File tree

4 files changed

+1657
-581
lines changed

4 files changed

+1657
-581
lines changed

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/RawSyntaxNodesFile.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,19 @@ let rawSyntaxNodesFile = SourceFileSyntax(leadingTrivia: "\(generateCopyrightHea
135135
"""
136136
)
137137

138+
DeclSyntax(
139+
"""
140+
init(unchecked raw: RawSyntax) {
141+
self.raw = raw
142+
}
143+
"""
144+
)
145+
138146
DeclSyntax(
139147
"""
140148
public init?<Node: RawSyntaxNodeProtocol>(_ other: Node) {
141149
guard Self.isKindOf(other.raw) else { return nil }
142-
self.init(raw: other.raw)
150+
self.init(unchecked: other.raw)
143151
}
144152
"""
145153
)
@@ -167,7 +175,7 @@ let rawSyntaxNodesFile = SourceFileSyntax(leadingTrivia: "\(generateCopyrightHea
167175
ptr += 1
168176
}
169177
}
170-
self.init(raw: raw)
178+
self.init(unchecked: raw)
171179
}
172180
"""
173181
)

Sources/SwiftSyntax/Assert.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ public func syntaxAssert(_ condition: @autoclosure () -> Bool, _ message: @autoc
2222
}
2323
}
2424

25+
/// An assertion that is only active in DEBUG builds, just like Swift.assert.
26+
/// Use this instead of `syntaxAssert` in places where the assertion has a
27+
/// non-trivial cost but provides little value in release builds.
28+
@_transparent
29+
public func debugAssert(_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) {
30+
Swift.assert(condition(), message(), file: file, line: line)
31+
}
32+
2533
@available(*, deprecated, message: "Use syntaxAssertionFailure instead, which will also trap in release builds")
2634
public func assertionFailure(_ message: @autoclosure () -> String = String(), file: StaticString = #file, line: UInt = #line) {
2735
Swift.assertionFailure(message(), file: file, line: line)

Sources/SwiftSyntax/Raw/RawSyntax.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,11 +536,11 @@ extension RawSyntax {
536536
tokenDiagnostic: TokenDiagnostic?,
537537
arena: __shared SyntaxArena
538538
) -> RawSyntax {
539-
syntaxAssert(
539+
debugAssert(
540540
arena.contains(text: wholeText),
541541
"token text must be managed by the arena"
542542
)
543-
syntaxAssert(
543+
debugAssert(
544544
arena is ParsingSyntaxArena || textRange == wholeText.indices,
545545
"arena must be able to parse trivia"
546546
)

0 commit comments

Comments
 (0)