Skip to content

Commit aa22d6f

Browse files
committed
Use a SetVector when looking up the SPI attributes on imports
Using a SetVector fixes an issue where many source files imported the same SPI group from the same module, the emitted private textual interfaces superfluously repeated the `@_spi` attribute on the import. rdar://problem/63681845
1 parent ed51731 commit aa22d6f

File tree

11 files changed

+35
-30
lines changed

11 files changed

+35
-30
lines changed

include/swift/AST/FileUnit.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ class FileUnit : public DeclContext {
107107
/// Find all SPI names imported from \p importedModule by this module,
108108
/// collecting the identifiers in \p spiGroups.
109109
virtual void lookupImportedSPIGroups(
110-
const ModuleDecl *importedModule,
111-
SmallVectorImpl<Identifier> &spiGroups) const {};
110+
const ModuleDecl *importedModule,
111+
SmallSetVector<Identifier, 4> &spiGroups) const {};
112112

113113
protected:
114114
/// Look up an operator declaration. Do not call directly, use

include/swift/AST/Module.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,8 +575,9 @@ class ModuleDecl : public DeclContext, public TypeDecl {
575575

576576
/// Find all SPI names imported from \p importedModule by this module,
577577
/// collecting the identifiers in \p spiGroups.
578-
void lookupImportedSPIGroups(const ModuleDecl *importedModule,
579-
SmallVectorImpl<Identifier> &spiGroups) const;
578+
void lookupImportedSPIGroups(
579+
const ModuleDecl *importedModule,
580+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const;
580581

581582
/// \sa getImportedModules
582583
enum class ImportFilterKind {

include/swift/AST/SourceFile.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,9 @@ class SourceFile final : public FileUnit {
365365
/// Find all SPI names imported from \p importedModule by this file,
366366
/// collecting the identifiers in \p spiGroups.
367367
virtual void
368-
lookupImportedSPIGroups(const ModuleDecl *importedModule,
369-
SmallVectorImpl<Identifier> &spiGroups) const override;
368+
lookupImportedSPIGroups(
369+
const ModuleDecl *importedModule,
370+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const override;
370371

371372
// Is \p targetDecl accessible as an explictly imported SPI from this file?
372373
bool isImportedAsSPI(const ValueDecl *targetDecl) const;

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,9 @@ class SerializedASTFile final : public LoadedFile {
359359
SmallVectorImpl<AbstractFunctionDecl *> &results) const override;
360360

361361
virtual void
362-
lookupImportedSPIGroups(const ModuleDecl *importedModule,
363-
SmallVectorImpl<Identifier> &spiGroups) const override;
362+
lookupImportedSPIGroups(
363+
const ModuleDecl *importedModule,
364+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const override;
364365

365366
Optional<CommentInfo> getCommentForDecl(const Decl *D) const override;
366367

lib/AST/Module.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,9 @@ void ModuleDecl::lookupObjCMethods(
639639
FORWARD(lookupObjCMethods, (selector, results));
640640
}
641641

642-
void ModuleDecl::lookupImportedSPIGroups(const ModuleDecl *importedModule,
643-
SmallVectorImpl<Identifier> &spiGroups) const {
642+
void ModuleDecl::lookupImportedSPIGroups(
643+
const ModuleDecl *importedModule,
644+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const {
644645
FORWARD(lookupImportedSPIGroups, (importedModule, spiGroups));
645646
}
646647

@@ -2190,31 +2191,29 @@ bool SourceFile::isImportedImplementationOnly(const ModuleDecl *module) const {
21902191
return !imports.isImportedBy(module, getParentModule());
21912192
}
21922193

2193-
void SourceFile::lookupImportedSPIGroups(const ModuleDecl *importedModule,
2194-
SmallVectorImpl<Identifier> &spiGroups) const {
2194+
void SourceFile::lookupImportedSPIGroups(
2195+
const ModuleDecl *importedModule,
2196+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const {
21952197
for (auto &import : Imports) {
21962198
if (import.importOptions.contains(ImportFlags::SPIAccessControl) &&
21972199
importedModule == std::get<ModuleDecl*>(import.module)) {
21982200
auto importedSpis = import.spiGroups;
2199-
spiGroups.append(importedSpis.begin(), importedSpis.end());
2201+
spiGroups.insert(importedSpis.begin(), importedSpis.end());
22002202
}
22012203
}
22022204
}
22032205

22042206
bool SourceFile::isImportedAsSPI(const ValueDecl *targetDecl) const {
22052207
auto targetModule = targetDecl->getModuleContext();
2206-
SmallVector<Identifier, 4> importedSPIGroups;
2208+
llvm::SmallSetVector<Identifier, 4> importedSPIGroups;
22072209
lookupImportedSPIGroups(targetModule, importedSPIGroups);
22082210
if (importedSPIGroups.empty()) return false;
22092211

22102212
auto declSPIGroups = targetDecl->getSPIGroups();
22112213

2212-
// Note: If we reach a point where there are many SPI imports or SPI groups
2213-
// on decls we could optimize this further by using a set.
2214-
for (auto importedSPI : importedSPIGroups)
2215-
for (auto declSPI : declSPIGroups)
2216-
if (importedSPI == declSPI)
2217-
return true;
2214+
for (auto declSPI : declSPIGroups)
2215+
if (importedSPIGroups.count(declSPI))
2216+
return true;
22182217

22192218
return false;
22202219
}

lib/Frontend/ModuleInterfaceSupport.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ static void printImports(raw_ostream &out,
139139
continue;
140140
}
141141

142-
llvm::SmallVector<Identifier, 4> spis;
142+
llvm::SmallSetVector<Identifier, 4> spis;
143143
M->lookupImportedSPIGroups(importedModule, spis);
144144

145145
// Only print implementation-only imports which have an SPI import.

lib/Serialization/ModuleFile.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,13 +2665,14 @@ void ModuleFile::lookupObjCMethods(
26652665
}
26662666
}
26672667

2668-
void ModuleFile::lookupImportedSPIGroups(const ModuleDecl *importedModule,
2669-
SmallVectorImpl<Identifier> &spiGroups) const {
2668+
void ModuleFile::lookupImportedSPIGroups(
2669+
const ModuleDecl *importedModule,
2670+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const {
26702671
for (auto &dep : Dependencies) {
26712672
auto depSpis = dep.spiGroups;
26722673
if (dep.Import.second == importedModule &&
26732674
!depSpis.empty()) {
2674-
spiGroups.append(depSpis.begin(), depSpis.end());
2675+
spiGroups.insert(depSpis.begin(), depSpis.end());
26752676
}
26762677
}
26772678
}

lib/Serialization/ModuleFile.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,8 +816,9 @@ class ModuleFile
816816

817817
/// Find all SPI names imported from \p importedModule by this module,
818818
/// collecting the identifiers in \p spiGroups.
819-
void lookupImportedSPIGroups(const ModuleDecl *importedModule,
820-
SmallVectorImpl<Identifier> &spiGroups) const;
819+
void lookupImportedSPIGroups(
820+
const ModuleDecl *importedModule,
821+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const;
821822

822823
/// Reports all link-time dependencies.
823824
void collectLinkLibraries(ModuleDecl::LinkLibraryCallback callback) const;

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ void Serializer::writeInputBlock(const SerializationOptions &options) {
10851085
else
10861086
stableImportControl = ImportControl::ImplementationOnly;
10871087

1088-
SmallVector<Identifier, 4> spis;
1088+
llvm::SmallSetVector<Identifier, 4> spis;
10891089
M->lookupImportedSPIGroups(import.second, spis);
10901090

10911091
ImportedModule.emit(ScratchRecord,

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,8 +1149,9 @@ void SerializedASTFile::lookupObjCMethods(
11491149
File.lookupObjCMethods(selector, results);
11501150
}
11511151

1152-
void SerializedASTFile::lookupImportedSPIGroups(const ModuleDecl *importedModule,
1153-
SmallVectorImpl<Identifier> &spiGroups) const {
1152+
void SerializedASTFile::lookupImportedSPIGroups(
1153+
const ModuleDecl *importedModule,
1154+
llvm::SmallSetVector<Identifier, 4> &spiGroups) const {
11541155
File.lookupImportedSPIGroups(importedModule, spiGroups);
11551156
}
11561157

0 commit comments

Comments
 (0)