Skip to content

Commit 34ccba1

Browse files
authored
Merge pull request #198 from dylansturg/sets_are_faster_for_searching
Use a `Set` to track visited exprs.
2 parents 308418f + a49dfce commit 34ccba1

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
4343

4444
/// Lists the expressions that have been visited, from the outermost expression, where contextual
4545
/// breaks and start/end contextual breaking tokens have been inserted.
46-
private var preVisitedExprs = [ExprSyntax]()
46+
private var preVisitedExprs = Set<ExprSyntax>()
4747

4848
/// Lists the tokens that are the closing or final delimiter of a node that shouldn't be split
4949
/// from the preceding token. When breaks are inserted around compound expressions, the breaks are
@@ -3181,20 +3181,13 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
31813181
/// necessary for contextual breaking throughout the expression. Records the nodes that were
31823182
/// visited so that they can be skipped later.
31833183
private func preVisitInsertingContextualBreaks<T: ExprSyntaxProtocol & Equatable>(_ expr: T) {
3184-
if !hasPreVisited(expr) {
3185-
let (visited, _, _) = insertContextualBreaks(ExprSyntax(expr), isTopLevel: true)
3186-
preVisitedExprs.append(contentsOf: visited)
3184+
let exprSyntax = ExprSyntax(expr)
3185+
if !preVisitedExprs.contains(exprSyntax) {
3186+
let (visited, _, _) = insertContextualBreaks(exprSyntax, isTopLevel: true)
3187+
preVisitedExprs.formUnion(visited)
31873188
}
31883189
}
31893190

3190-
/// Returns whether the given expression node has been pre-visited, from a parent expression.
3191-
private func hasPreVisited<T: ExprSyntaxProtocol & Equatable>(_ expr: T) -> Bool {
3192-
for item in preVisitedExprs {
3193-
if item == ExprSyntax(expr) { return true }
3194-
}
3195-
return false
3196-
}
3197-
31983191
/// Recursively visits nested expressions from the given expression inserting contextual breaking
31993192
/// tokens. When visiting an expression node, `preVisitInsertingContextualBreaks(_:)` should be
32003193
/// called instead of this helper.

0 commit comments

Comments
 (0)