Skip to content

[Macros] Cache SourceLocationConverter in ExportedSourceFile #70415

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

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Dec 12, 2023

We need a SourceLocationConverter every time we create a PluginMessage.Syntax to know the source location of that syntax node within the source file. This means that we needed to re-build the line table of the entire source file multiple times for every macro that we expand. Cache it to improve performance.

rdar://119047550

@ahoppen
Copy link
Member Author

ahoppen commented Dec 12, 2023

@swift-ci Please smoke test

@ahoppen ahoppen force-pushed the ahoppen/cache-source-loc-converter branch from 67f04d9 to cc378e4 Compare December 13, 2023 21:59
@ahoppen ahoppen force-pushed the ahoppen/cache-source-loc-converter branch from cc378e4 to ede70fa Compare December 13, 2023 21:59
@ahoppen
Copy link
Member Author

ahoppen commented Dec 13, 2023

@bnbarham Running test uncovered that I wasn’t handling the file path mode properly in MacroExpansionContext.location. I would appreciate another review.

@ahoppen
Copy link
Member Author

ahoppen commented Dec 13, 2023

@swift-ci Please smoke test

if adjustedFile == exportedSourceFile.fileName {
adjustedFile = fileID
}
var adjustePresumedFile = location.presumedFile
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
var adjustePresumedFile = location.presumedFile
var adjustedPresumedFile = location.presumedFile

let fileID = "\(exportedSourceFile.moduleName)/\(exportedSourceFile.fileName.basename)"
var adjustedFile = location.file
if adjustedFile == exportedSourceFile.fileName {
adjustedFile = fileID
Copy link
Contributor

Choose a reason for hiding this comment

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

What does it mean if they aren't equal? I thought that would be the presumed file case, but apparently not?

Copy link
Member Author

@ahoppen ahoppen Dec 14, 2023

Choose a reason for hiding this comment

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

AFAIK it can only happen for the presumed file. I can change it to an assertion that adjustedFile == exportedSourceFile.fileName. Don’t have strong opinions here. Just thought that it would be better to recover instead of crashing/asserting.

We need a `SourceLocationConverter` every time we create a `PluginMessage.Syntax` to know the source location of that syntax node within the source file. This means that we needed to re-build the line table of the entire source file multiple times for every macro that we expand. Cache it to improve performance.

rdar://119047550
@ahoppen ahoppen force-pushed the ahoppen/cache-source-loc-converter branch from ede70fa to 8da0754 Compare December 14, 2023 16:14
@ahoppen
Copy link
Member Author

ahoppen commented Dec 19, 2023

@swift-ci Please test

@bnbarham
Copy link
Contributor

bnbarham commented Jan 5, 2024

@swift-ci please test

@bnbarham bnbarham merged commit a159604 into swiftlang:main Jan 8, 2024
@ahoppen ahoppen deleted the ahoppen/cache-source-loc-converter branch January 8, 2024 22:50
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