Skip to content

Sema: Move the availability macros cache to the ASTContext #76794

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
Oct 2, 2024

Conversation

xymus
Copy link
Contributor

@xymus xymus commented Oct 1, 2024

The availability macros definitions are parsed from the command line and stored in a cache. The cache was in the Parser, which would have it be computed for each file using availability attributes. Let's move it to the ASTContext instead where it can generally be computed once per invocation and used across the module, speeding up the compilation process.

Also optimize how we register the buffer holding the macro definitions. Registering all of them before starting to parse their content avoids sorting them repeatedly which caused a noticeable slowdown on high usage of availability macros.

rdar://134797088

The availability macros definitions are parsed from the command line and
stored in a cache. The cache was in the Parser, which would have it be
computed for each file using availability macros. Let's move it to the
ASTContext instead where it can generally be computed once per invocation
and used across the module.

rdar://134797088
@xymus
Copy link
Contributor Author

xymus commented Oct 1, 2024

@swift-ci Please smoke test

SmallVector<AvailabilitySpec *, 4>> VersionEntry;

bool WasParsed = false;
llvm::DenseMap<StringRef, VersionEntry> Impl;
Copy link
Member

Choose a reason for hiding this comment

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

Should this be a StringMap rather than depend on the StringRefs living forever?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that's safe currently. The StringRefs should point to a buffer attached to the SourceManager of the ASTContext, so both the cache and the sources should be freed together. Does this sound right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently one of these buffers is allocated for each -define-availability argument. I suspect this doesn't help with the general performances of the findBuffer services on wide uses of availability macros, we could look into deregistering these small buffers after parsing and copy all these strings.

@@ -426,6 +426,9 @@ struct ASTContext::Implementation {
/// Singleton used to cache the import graph.
swift::namelookup::ImportCache TheImportCache;

/// Cache of availability macros parsed from the command line.
AvailabilityMacroMap TheAvailabilityMacroCache;
Copy link
Member

Choose a reason for hiding this comment

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

Could we use the request-evaluator here rather than add the cache directly to the ASTContext?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The request input here would be the compiler arguments. Since it's used at the parser level we can't rely on them being attached to the module as I would usually do. Would you know what to define as inputs for a request in this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Other requests seem to use the ASTContext itself. I'll see to use that.

…sing

The availability macro definitions from the command line are copied into
source buffers for parsing. Let's register all of these buffers in one go,
before starting to parse any of them. Parsing triggers using `findBuffer`
services which sorts all buffers. The new logic should ensure this
sorting happens only once for availability macros.
@xymus
Copy link
Contributor Author

xymus commented Oct 1, 2024

@swift-ci Please smoke test

@xymus
Copy link
Contributor Author

xymus commented Oct 2, 2024

Merging with 2 different optimizations. I'll put up a PR to move the cache to a request next as this optimization is time sensitive.

@xymus xymus merged commit 3442fba into swiftlang:main Oct 2, 2024
3 checks passed
@xymus xymus deleted the move-avail-macro-cache branch October 2, 2024 17:53
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.

3 participants