Skip to content

[Macros] Cache parsed syntax tree in compiler plugins #2682

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 2 commits into from
Jun 15, 2024

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Jun 12, 2024

The compiler may send the same syntax to the plugins multiple times.
For example, memberAttribute macro request contains parent nominal decl syntax, and the compiler sends a request for each members. Parsing it multiple times is a waste.

rdar://129624305

@rintaro rintaro requested review from ahoppen and bnbarham as code owners June 12, 2024 17:30
@rintaro
Copy link
Member Author

rintaro commented Jun 12, 2024

@swift-ci Please test

/// Syntax added by `add(_:)` method. Keyed by the `id` of the node.
private var knownSourceSyntax: [Syntax.ID: KnownSourceSyntax] = [:]

init(syntaxrRegistry: ParsedSyntaxRegistry) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
init(syntaxrRegistry: ParsedSyntaxRegistry) {
init(syntaxRegistry: ParsedSyntaxRegistry) {

The compiler may send the same syntax to the plugins multiple times.
For example, 'memberAttribute' macro request contains parent nominal
decl syntax, and the compiler sends a request for each members. Parsing
it multiple times is a waste.

rdar://129624305
@rintaro rintaro force-pushed the macros-parsed-syntax-registry branch from d0eb2f7 to 3c23a0e Compare June 13, 2024 17:23
@rintaro
Copy link
Member Author

rintaro commented Jun 13, 2024

@swift-ci Please test

@rintaro rintaro force-pushed the macros-parsed-syntax-registry branch from 3c23a0e to baff373 Compare June 13, 2024 17:24
@rintaro
Copy link
Member Author

rintaro commented Jun 13, 2024

@swift-ci Please test

@rintaro rintaro requested a review from hamishknight June 13, 2024 18:01
@rintaro
Copy link
Member Author

rintaro commented Jun 14, 2024

@swift-ci Please test Windows

1 similar comment
@rintaro
Copy link
Member Author

rintaro commented Jun 14, 2024

@swift-ci Please test Windows

@rintaro
Copy link
Member Author

rintaro commented Jun 14, 2024

@swift-ci Please test Windows

@rintaro rintaro merged commit b514509 into swiftlang:main Jun 15, 2024
3 checks passed
rintaro added a commit that referenced this pull request Jun 15, 2024
[6.0][Macros] Cache parsed syntax tree in compiler plugins #2682
@@ -24,6 +24,49 @@ import SwiftSyntax
import SwiftSyntaxMacros
#endif

/// Caching parser for PluginMessage.Syntax
class ParsedSyntaxRegistry {
Copy link
Member

Choose a reason for hiding this comment

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

Naming nitpick: A registry for me is something that stores long-lived values (possibly even eternal), which is quite the opposite of a cache. Would ParsedSyntaxCache be a better name?

Copy link
Contributor

Choose a reason for hiding this comment

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

Heh, in fairness it was originally the first. The LRU was added after 😅. But yeah, Cache is probably the better name now.

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.

4 participants