Skip to content

Commit f67e771

Browse files
committed
Improve SyntaxVisitor performance
Refactor the implementation to match SyntaxRewriter, thereby gaining the same performance benefits.
1 parent f108c26 commit f67e771

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

Sources/SwiftSyntax/SyntaxRewriter.swift.gyb

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ open class SyntaxRewriter {
151151

152152
// Incrementing i manually is faster than using .enumerated()
153153
var childIndex = 0
154-
for (raw, info) in RawSyntaxChildren(Syntax(node)) {
154+
for (raw, info) in RawSyntaxChildren(syntaxNode) {
155155
defer { childIndex += 1 }
156156
guard let child = raw else {
157157
// Node does not exist. If we are collecting rewritten nodes, we need to
@@ -310,26 +310,26 @@ public extension SyntaxAnyVisitor {
310310
% for node in SYNTAX_NODES:
311311
% if is_visitable(node):
312312
mutating func visit(_ node: ${node.name}) -> SyntaxVisitorContinueKind {
313-
return visitAny(Syntax(node))
313+
return visitAny(node._syntaxNode)
314314
}
315315
mutating func visitPost(_ node: ${node.name}) {
316-
return visitAnyPost(Syntax(node))
316+
return visitAnyPost(node._syntaxNode)
317317
}
318318
% end
319319
% end
320320

321321
mutating func visit(_ token: TokenSyntax) -> SyntaxVisitorContinueKind {
322-
return visitAny(Syntax(token))
322+
return visitAny(token._syntaxNode)
323323
}
324324
mutating func visitPost(_ node: TokenSyntax) {
325-
return visitAnyPost(Syntax(node))
325+
return visitAnyPost(node._syntaxNode)
326326
}
327327

328328
mutating func visit(_ node: UnknownSyntax) -> SyntaxVisitorContinueKind {
329-
return visitAny(Syntax(node))
329+
return visitAny(node._syntaxNode)
330330
}
331331
mutating func visitPost(_ node: UnknownSyntax) {
332-
return visitAnyPost(Syntax(node))
332+
return visitAnyPost(node._syntaxNode)
333333
}
334334
}
335335

@@ -384,16 +384,16 @@ private func _doVisitImpl${node.name}<Visitor>(
384384
let node = Unknown${node.name}(data)
385385
let needsChildren = (visitor.visit(node) == .visitChildren)
386386
// Avoid calling into visitChildren if possible.
387-
if needsChildren && data.raw.numberOfChildren > 0 {
388-
visitChildren(data, parent: Syntax(node), &visitor)
387+
if needsChildren && node.raw.numberOfChildren > 0 {
388+
visitChildren(node, &visitor)
389389
}
390390
visitor.visitPost(node)
391391
% else:
392392
let node = ${node.name}(data)
393393
let needsChildren = (visitor.visit(node) == .visitChildren)
394394
// Avoid calling into visitChildren if possible.
395-
if needsChildren && data.raw.numberOfChildren > 0 {
396-
visitChildren(data, parent: Syntax(node), &visitor)
395+
if needsChildren && node.raw.numberOfChildren > 0 {
396+
visitChildren(node, &visitor)
397397
}
398398
visitor.visitPost(node)
399399
% end
@@ -414,8 +414,8 @@ fileprivate func doVisit<Visitor>(
414414
let node = UnknownSyntax(data)
415415
let needsChildren = (visitor.visit(node) == .visitChildren)
416416
// Avoid calling into visitChildren if possible.
417-
if needsChildren && data.raw.numberOfChildren > 0 {
418-
visitChildren(data, parent: Syntax(node), &visitor)
417+
if needsChildren && node.raw.numberOfChildren > 0 {
418+
visitChildren(node, &visitor)
419419
}
420420
visitor.visitPost(node)
421421
// The implementation of every generated case goes into its own function. This
@@ -429,11 +429,13 @@ fileprivate func doVisit<Visitor>(
429429
}
430430
}
431431

432-
fileprivate func visitChildren<Visitor>(
433-
_ data: SyntaxData, parent: Syntax, _ visitor: inout Visitor
432+
fileprivate func visitChildren<SyntaxType: SyntaxProtocol, Visitor>(
433+
_ syntax: SyntaxType, _ visitor: inout Visitor
434434
) where Visitor : SyntaxVisitor {
435-
for childRaw in PresentRawSyntaxChildren(data.absoluteRaw) {
436-
let childData = SyntaxData(childRaw, parent: parent)
435+
let syntaxNode = Syntax(syntax)
436+
let parentBox = SyntaxBox(syntaxNode)
437+
for childRaw in PresentRawSyntaxChildren(syntaxNode) {
438+
let childData = SyntaxData(childRaw, parentBox: parentBox)
437439
doVisit(childData, &visitor)
438440
}
439441
}

0 commit comments

Comments
 (0)