Skip to content

AST: Sort TypeRefinementContexts for better lookup performance #76912

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 4 commits into from
Oct 9, 2024

Conversation

tshortli
Copy link
Contributor

@tshortli tshortli commented Oct 8, 2024

Improve the performance of querying the TypeRefinementContext tree to look up the most refined context that contains a source location. By storing the child nodes of a parent context in sorted order, we can search for the child with a source range that contains the target location using a binary search.

To ensure the new sorted children invariant is preserved, the ASTVerifier has been extended to also verify the TypeRefinementContext hierarchy.

The existing `rangeContainsTokenLocWithGeneratedSource()` utility pre-dated
`SourceManager::containsTokenLoc()`.
Improve the performance of looking up the most refined `TypeRefinementContext`
for a given source location by storing children in sorted order and using a
binary search to query each layer of the tree. This slightly pessimizes
insertion (primarily when macros are expanded out of source location order) but
lookup is a far more common operation.
Verify the integrity of the tree formed by `TypeRefinementContext` nodes. In
general, parent nodes should "contain" their children and children always be in
sorted order.
When printing the source ranges for nodes that represent an expansion within a
source file, print the range from the original source file and include a
description of the kind of expansion.
@tshortli
Copy link
Contributor Author

tshortli commented Oct 8, 2024

@swift-ci please test

@tshortli
Copy link
Contributor Author

tshortli commented Oct 8, 2024

@swift-ci please test macOS

@tshortli tshortli merged commit cee810a into swiftlang:main Oct 9, 2024
5 checks passed
@tshortli tshortli deleted the sorted-type-refinement-contexts branch October 9, 2024 03:48
Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

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

I'm late to the party here, but this looks fantastic

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