Skip to content

AST: Optimize the layout of AvailabilityRange #79784

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
Mar 5, 2025

Conversation

tshortli
Copy link
Contributor

@tshortli tshortli commented Mar 5, 2025

AvailabilityRange is now being used as a currency type in more of the compiler, and some of those uses are in permanent ASTContext allocations. The class wraps the VersionRange utility, which is itself a wrapper around llvm::VersionTuple with some additional storage for representing sentinel values. Even though the two sentinel values can be be represented with just a single bit of additional storage on top of the 16 bytes required to represent VersionTuple, because of alignment requirements the sentinel values end up bloating the layout of VersionRange by many bytes.

To make AvailabilityRange and VersionRange more efficient to store, we can instead reserve two unlikely llvm::VersionTuple bit patterns as the sentinel values instead. The values chosen are the same ones LLVM uses to represent version tuple tombstones and empty keys in a DenseMap.

@tshortli
Copy link
Contributor Author

tshortli commented Mar 5, 2025

@swift-ci please test

`AvailabilityRange` is now being used as a currency type in more of the
compiler, and some of those uses are in permanent `ASTContext` allocations. The
class wraps the `VersionRange` utility, which is itself a wrapper around
`llvm::VersionTuple` with some additional storage for representing sentinel
values. Even though the two sentinel values can be be represented with just a
single bit of additional storage on top of the 16 bytes required to represent
`VersionTuple`, because of alignment requirements the sentinel values end up
bloating the layout of `VersionRange` by many bytes.

To make `AvailabilityRange` and `VersionRange` more efficient to store, we can
instead reserve two unlikely `llvm::VersionTuple` bit patterns as the sentinel
values instead. The values chosen are the same ones LLVM uses to represent
version tuple tombstones and empty keys in a `DenseMap`.
@tshortli tshortli force-pushed the optimize-version-range-layout branch from be17f8a to cad1ee7 Compare March 5, 2025 15:15
@tshortli
Copy link
Contributor Author

tshortli commented Mar 5, 2025

@swift-ci please smoke test

@tshortli tshortli marked this pull request as ready for review March 5, 2025 15:16
@tshortli tshortli enabled auto-merge March 5, 2025 15:16
@tshortli tshortli merged commit 77d18a8 into swiftlang:main Mar 5, 2025
3 checks passed
@tshortli tshortli deleted the optimize-version-range-layout branch March 5, 2025 20:28
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.

1 participant