Skip to content

Skip leading trivia generation if empty in BuildableNodes #639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2022

Conversation

fwcd
Copy link
Member

@fwcd fwcd commented Aug 25, 2022

Although we discussed this earlier, I would like to revisit the optimization to skip leading trivia generation on buildable nodes in SwiftSyntaxBuilder when empty, since it actually does have a noticeable performance impact on generate-swift-syntax-builder: The optimization cuts down generation time from ~13 seconds to ~7 seconds on my machine, nearly a 50% improvement in performance! Profiling the executable with Instruments reveals that this indeed seems to be the issue, the heaviest stack trace points to withLeadingTrivia.

The improvement in performance with this patch becomes especially noticeable during development of generated files where shorter feedback cycles greatly improve the workflow.

(This change should be mostly orthogonal to the upcoming formatting-related changes)

@fwcd fwcd requested a review from ahoppen as a code owner August 25, 2022 14:44
Comment on lines +280 to +291
IfStmt(
conditions: ExprList([MemberAccessExpr(base: "combinedLeadingTrivia", name: "isEmpty")])
) {
ReturnStmt(expression: "result")
} elseBody: {
ReturnStmt(expression: FunctionCallExpr(MemberAccessExpr(base: "result", name: "withLeadingTrivia")) {
TupleExprElement(expression: FunctionCallExpr(MemberAccessExpr(
base: "combinedLeadingTrivia",
name: "addingSpacingAfterNewlinesIfNeeded"
)))
})
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One idea might be to move this logic into withLeadingTrivia itself (and similarly into withTrailingTrivia). However since we cannot assume that the syntax doesn't already have trivia (like in this case) the check would likely get more expensive, so I am not sure whether the more general solution would be worth it there.

@fwcd
Copy link
Member Author

fwcd commented Aug 25, 2022

@swift-ci please test

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, that performance difference is way bigger than I expected. Thanks for catching this 👍

@fwcd
Copy link
Member Author

fwcd commented Aug 25, 2022

@swift-ci please test macOS

@fwcd fwcd merged commit 29f9cc1 into swiftlang:main Aug 25, 2022
@fwcd fwcd deleted the fwcd/optimize-leading-trivia branch August 25, 2022 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants