Skip to content

Model SyntaxVisitor as a class #158

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 6 commits into from
Oct 22, 2019

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Oct 16, 2019

Since all the methods on the SyntaxVisitor were mutating, it makes more sense to model the SyntaxVisitor as a class. This also increases visitation performance.

API Change

SyntaxVisitor

  • Structs that conformed to SyntaxVisitor need to become classes
  • Otherwise, the change should be source compatible

SyntaxAnyVisitor

  • Structs that conformed to SyntaxAnyVisitor need to become classes

Performance improvements

Performance measurements were done by running an empty SyntaxRewriter / SyntaxVisitor on a Swift file with ~5000 lines of code. The baseline is the current performance of SwiftSyntax before #155 is merged.

Empty SyntaxVisitor

Before After #155 After this PR
Debug 92.9ms 112.1ms (+21%) 107.9ms (+16%)
Release 12.3ms 12.6ms (+3%) 10.9ms (-12%)

Empty SyntaxAnyVisitor

Before After #155 After this PR
Debug 106.2ms 136.2ms (+28%) 115.3 (+9%)
Release 24.8ms 15.5ms (-38%) 11.4ms (-54%)

@ahoppen ahoppen requested a review from akyrtzi October 16, 2019 23:14
@ahoppen ahoppen force-pushed the syntax-visitor-class branch 2 times, most recently from 9ee9449 to 11a32cf Compare October 16, 2019 23:21
@ahoppen
Copy link
Member Author

ahoppen commented Oct 16, 2019

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 11a32cf

Avoid the overhead of existentials by modelling the entire syntax node
hierarchy using structs with custom casting functions.

This results in a major performance gain for SyntaxRewriter.
Improve the performance of SyntaxRewriter by avoiding unnecessary
casts, reusing the SyntaxBox and eliminating .enumerated()
Refactor the implementation to match SyntaxRewriter, thereby gaining
the same performance benefits.
Otherwise it requires a lot of boilerplate to return the Syntax node's
type in the lit-test-helper.
Since all the methods on the SyntaxVisitor were mutating, it makes more
sense to model the SyntaxVisitor as a class. This also increases
visitation performance.
@ahoppen ahoppen force-pushed the syntax-visitor-class branch from 11a32cf to bc5fef3 Compare October 17, 2019 21:08
@ahoppen
Copy link
Member Author

ahoppen commented Oct 17, 2019

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 11a32cf

Copy link
Member

@allevato allevato left a comment

Choose a reason for hiding this comment

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

The original change that turned SyntaxVisitor into a protocol caused an unfortunate (and large) amount of churn on swift-format's rule pipeline due to some tricky interactions between generics, protocols, and mutability. Even though it's a little sad to see all the work to fix that having to be reverted (we never migrated again to SyntaxVisitorBase), it's absolutely the right thing in the long run. 👍

@ahoppen ahoppen merged commit 903ac50 into swiftlang:master Oct 22, 2019
@ahoppen ahoppen deleted the syntax-visitor-class branch October 28, 2019 22:25
adevress pushed a commit to adevress/swift-syntax that referenced this pull request Jan 14, 2024
…ignores_discretionary

Restore some discretionary newlines from PR swiftlang#143
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.

5 participants