Skip to content

Commit 54d2ea0

Browse files
committed
[Member name lookup] Only look to expand macros in types/extensions that matter
Keep track of those types and extensions that have any macro expansions, so that when we need to check whether to expand macros to satisfy name lookup, we only look through that list---rather than looking at every extension again. This improves compile times significantly for a source file with a large number of extensions on a single type. However, there is still much to do here.
1 parent af3ea4d commit 54d2ea0

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

include/swift/AST/PotentialMacroExpansions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class PotentialMacroExpansions {
9090
delete getIntroducedNamesIfAvailable();
9191
}
9292

93+
/// Whether there are any expanded macros in this context.
94+
explicit operator bool() const { return hasAnyExpandedMacro(); }
95+
9396
/// Whether there are any expanded macros in this context.
9497
bool hasAnyExpandedMacro() const {
9598
return Storage.getInt() & AnyExpandedMacros;

lib/AST/NameLookup.cpp

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,10 +1223,19 @@ class swift::MemberLookupTable : public ASTAllocated<swift::MemberLookupTable> {
12231223
/// parent nominal type.
12241224
llvm::DenseSet<DeclBaseName> LazilyCompleteNames;
12251225

1226-
/// The set of names for which we have expanded relevant macros for in the
1227-
/// parent nominal type.
1228-
llvm::DenseSet<DeclName> LazilyCompleteNamesForMacroExpansion;
1229-
1226+
struct {
1227+
/// Whether we have computed the `containersWithMacroExpansions`.
1228+
bool ComputedContainersWithMacroExpansions = false;
1229+
1230+
/// The nominal type and any extensions that have macro expansions, which
1231+
/// is used to restrict the set of places one will lookup for a member
1232+
/// produced by a macro expansion.
1233+
llvm::SmallVector<TypeOrExtensionDecl, 2> ContainersWithMacroExpansions;
1234+
1235+
/// The set of names for which we have expanded relevant macros for in the
1236+
/// parent nominal type.
1237+
llvm::DenseSet<DeclName> LazilyCompleteNames;
1238+
} LazyMacroExpansionState;
12301239
public:
12311240
/// Create a new member lookup table.
12321241
explicit MemberLookupTable(ASTContext &ctx);
@@ -1255,6 +1264,32 @@ class swift::MemberLookupTable : public ASTAllocated<swift::MemberLookupTable> {
12551264
LazilyCompleteNames.clear();
12561265
}
12571266

1267+
/// Retrieve an array containing the set of containers for this type (
1268+
/// i.e., the nominal type and any extensions) that can produce members via
1269+
/// macro expansion.
1270+
ArrayRef<TypeOrExtensionDecl> getContainersWithMacroExpansions(
1271+
NominalTypeDecl *nominal) {
1272+
if (LazyMacroExpansionState.ComputedContainersWithMacroExpansions)
1273+
return LazyMacroExpansionState.ContainersWithMacroExpansions;
1274+
1275+
Evaluator &evaluator = nominal->getASTContext().evaluator;
1276+
1277+
// Does the type have macro expansions?
1278+
if (evaluateOrDefault(
1279+
evaluator, PotentialMacroExpansionsInContextRequest{nominal}, {}))
1280+
LazyMacroExpansionState.ContainersWithMacroExpansions.push_back(nominal);
1281+
1282+
// Check each extension for macro expansions.
1283+
for (auto ext : nominal->getExtensions()) {
1284+
if (evaluateOrDefault(
1285+
evaluator, PotentialMacroExpansionsInContextRequest{ext}, {}))
1286+
LazyMacroExpansionState.ContainersWithMacroExpansions.push_back(ext);
1287+
}
1288+
1289+
LazyMacroExpansionState.ComputedContainersWithMacroExpansions = true;
1290+
return LazyMacroExpansionState.ContainersWithMacroExpansions;
1291+
}
1292+
12581293
/// Determine whether the given container has any macro-introduced names that
12591294
/// match the given declaration.
12601295
bool hasAnyMacroNamesMatching(TypeOrExtensionDecl container, DeclName name);
@@ -1266,16 +1301,16 @@ class swift::MemberLookupTable : public ASTAllocated<swift::MemberLookupTable> {
12661301
bool isBaseNameComplete = name.isCompoundName() &&
12671302
isLazilyCompleteForMacroExpansion(DeclName(name.getBaseName()));
12681303
return isBaseNameComplete ||
1269-
LazilyCompleteNamesForMacroExpansion.contains(name);
1304+
LazyMacroExpansionState.LazilyCompleteNames.contains(name);
12701305
}
12711306

12721307
void markLazilyCompleteForMacroExpansion(DeclName name) {
12731308
assert(!MacroDecl::isUniqueMacroName(name.getBaseName()));
1274-
LazilyCompleteNamesForMacroExpansion.insert(name);
1309+
LazyMacroExpansionState.LazilyCompleteNames.insert(name);
12751310
}
12761311

12771312
void clearLazilyCompleteForMacroExpansionCache() {
1278-
LazilyCompleteNamesForMacroExpansion.clear();
1313+
LazyMacroExpansionState.LazilyCompleteNames.clear();
12791314
}
12801315

12811316
/// Iterator into the lookup table.
@@ -1895,11 +1930,9 @@ DirectLookupRequest::evaluate(Evaluator &evaluator,
18951930
DeclName macroExpansionKey = adjustLazyMacroExpansionNameKey(ctx, name);
18961931
if (!excludeMacroExpansions &&
18971932
!Table.isLazilyCompleteForMacroExpansion(macroExpansionKey)) {
1898-
populateLookupTableEntryFromMacroExpansions(
1899-
ctx, Table, macroExpansionKey, decl);
1900-
for (auto ext : decl->getExtensions()) {
1933+
for (auto container : Table.getContainersWithMacroExpansions(decl)) {
19011934
populateLookupTableEntryFromMacroExpansions(
1902-
ctx, Table, macroExpansionKey, ext);
1935+
ctx, Table, macroExpansionKey, container);
19031936
}
19041937
Table.markLazilyCompleteForMacroExpansion(macroExpansionKey);
19051938
}

0 commit comments

Comments
 (0)