Skip to content

Commit ed9eef2

Browse files
authored
Merge pull request #79324 from swiftlang/gaborh/redundant-lookups
[cxx-interop] Remove some duplicated lookups
2 parents 8e87541 + 068815c commit ed9eef2

File tree

6 files changed

+45
-44
lines changed

6 files changed

+45
-44
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2477,8 +2477,9 @@ ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
24772477
// well, and may do unnecessary work.
24782478
ClangModuleUnit *wrapperUnit = getWrapperForModule(clangModule, importLoc);
24792479
ModuleDecl *result = wrapperUnit->getParentModule();
2480-
if (!ModuleWrappers[clangModule].getInt()) {
2481-
ModuleWrappers[clangModule].setInt(true);
2480+
auto &moduleWrapper = ModuleWrappers[clangModule];
2481+
if (!moduleWrapper.getInt()) {
2482+
moduleWrapper.setInt(true);
24822483
(void) namelookup::getAllImports(result);
24832484
}
24842485

@@ -2888,11 +2889,11 @@ void ClangImporter::Implementation::addImportDiagnostic(
28882889
ImportDiagnosticTarget target, Diagnostic &&diag,
28892890
clang::SourceLocation loc) {
28902891
ImportDiagnostic importDiag = ImportDiagnostic(target, diag, loc);
2891-
if (SwiftContext.LangOpts.DisableExperimentalClangImporterDiagnostics ||
2892-
CollectedDiagnostics.count(importDiag))
2892+
if (SwiftContext.LangOpts.DisableExperimentalClangImporterDiagnostics)
2893+
return;
2894+
auto [_, inserted] = CollectedDiagnostics.insert(importDiag);
2895+
if (!inserted)
28932896
return;
2894-
2895-
CollectedDiagnostics.insert(importDiag);
28962897
ImportDiagnostics[target].push_back(importDiag);
28972898
}
28982899

@@ -6866,8 +6867,9 @@ void ClangImporter::Implementation::dumpSwiftLookupTables() {
68666867
// Print out the lookup tables for the various modules.
68676868
for (auto moduleName : moduleNames) {
68686869
llvm::errs() << "<<" << moduleName << " lookup table>>\n";
6869-
LookupTables[moduleName]->deserializeAll();
6870-
LookupTables[moduleName]->dump(llvm::errs());
6870+
auto &lookupTable = LookupTables[moduleName];
6871+
lookupTable->deserializeAll();
6872+
lookupTable->dump(llvm::errs());
68716873
}
68726874

68736875
llvm::errs() << "<<Bridging header lookup table>>\n";
@@ -7430,8 +7432,10 @@ ClangImporter::getCXXFunctionTemplateSpecialization(SubstitutionMap subst,
74307432
if (!newFn)
74317433
return ConcreteDeclRef(decl);
74327434

7433-
if (Impl.specializedFunctionTemplates.count(newFn))
7434-
return ConcreteDeclRef(Impl.specializedFunctionTemplates[newFn]);
7435+
auto [fnIt, inserted] =
7436+
Impl.specializedFunctionTemplates.try_emplace(newFn, nullptr);
7437+
if (!inserted)
7438+
return ConcreteDeclRef(fnIt->second);
74357439

74367440
auto newDecl = cast_or_null<ValueDecl>(
74377441
decl->getASTContext().getClangModuleLoader()->importDeclDirectly(
@@ -7454,7 +7458,7 @@ ClangImporter::getCXXFunctionTemplateSpecialization(SubstitutionMap subst,
74547458
}
74557459
}
74567460

7457-
Impl.specializedFunctionTemplates[newFn] = newDecl;
7461+
fnIt->getSecond() = newDecl;
74587462
return ConcreteDeclRef(newDecl);
74597463
}
74607464

@@ -8165,7 +8169,7 @@ CxxRecordAsSwiftType::evaluate(Evaluator &evaluator,
81658169
mod->lookupValue(desc.ctx.getIdentifier(swiftName), NLKind::UnqualifiedLookup,
81668170
results);
81678171
if (results.size() == 1) {
8168-
if (dyn_cast<ClassDecl>(results[0]))
8172+
if (isa<ClassDecl>(results[0]))
81698173
return results[0];
81708174
}
81718175
return nullptr;

lib/ClangImporter/DWARFImporter.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,15 @@ ModuleDecl *ClangImporter::Implementation::loadModuleDWARF(
112112

113113
// FIXME: Implement submodule support!
114114
Identifier name = path[0].Item;
115-
auto it = DWARFModuleUnits.find(name);
116-
if (it != DWARFModuleUnits.end())
115+
auto [it, inserted] = DWARFModuleUnits.try_emplace(name, nullptr);
116+
if (!inserted)
117117
return it->second->getParentModule();
118118

119+
auto itCopy = it; // Capturing structured bindings is a C++20 feature.
119120
auto *M = ModuleDecl::create(name, SwiftContext,
120121
[&](ModuleDecl *M, auto addFile) {
121122
auto *wrapperUnit = new (SwiftContext) DWARFModuleUnit(*M, *this);
122-
DWARFModuleUnits.insert({name, wrapperUnit});
123+
itCopy->second = wrapperUnit;
123124
addFile(wrapperUnit);
124125
});
125126
M->setIsNonSwiftModule();

lib/ClangImporter/ImportDecl.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include "llvm/ADT/StringExtras.h"
7575
#include "llvm/ADT/StringMap.h"
7676
#include "llvm/ADT/StringSwitch.h"
77+
#include "llvm/ADT/TinyPtrVector.h"
7778
#include "llvm/Support/Path.h"
7879

7980
#include <algorithm>
@@ -2574,11 +2575,11 @@ namespace {
25742575
Impl.addAlternateDecl(subscriptImpl, subscript);
25752576
}
25762577

2577-
if (Impl.cxxDereferenceOperators.find(result) !=
2578-
Impl.cxxDereferenceOperators.end()) {
2578+
auto getterAndSetterIt = Impl.cxxDereferenceOperators.find(result);
2579+
if (getterAndSetterIt != Impl.cxxDereferenceOperators.end()) {
25792580
// If this type has a dereference operator, synthesize a computed
25802581
// property called `pointee` for it.
2581-
auto getterAndSetter = Impl.cxxDereferenceOperators[result];
2582+
auto getterAndSetter = getterAndSetterIt->second;
25822583

25832584
VarDecl *pointeeProperty =
25842585
synthesizer.makeDereferencedPointeeProperty(
@@ -5119,10 +5120,9 @@ namespace {
51195120

51205121
// Check whether there's some special method to import.
51215122
if (!forceClassMethod) {
5122-
if (dc == Impl.importDeclContextOf(decl, decl->getDeclContext()) &&
5123-
!Impl.ImportedDecls[{decl->getCanonicalDecl(), getVersion()}])
5124-
Impl.ImportedDecls[{decl->getCanonicalDecl(), getVersion()}]
5125-
= result;
5123+
if (dc == Impl.importDeclContextOf(decl, decl->getDeclContext()))
5124+
Impl.ImportedDecls.try_emplace(
5125+
{decl->getCanonicalDecl(), getVersion()}, result);
51265126

51275127
if (importedName.isSubscriptAccessor()) {
51285128
// If this was a subscript accessor, try to create a
@@ -7780,8 +7780,8 @@ SwiftDeclConverter::importSubscript(Decl *decl,
77807780

77817781
// Note that we've created this subscript.
77827782
Impl.Subscripts[{getter, setter}] = subscript;
7783-
if (setter && !Impl.Subscripts[{getter, nullptr}])
7784-
Impl.Subscripts[{getter, nullptr}] = subscript;
7783+
if (setter)
7784+
Impl.Subscripts.try_emplace({getter, nullptr}, subscript);
77857785

77867786
// Make the getter/setter methods unavailable.
77877787
if (!getter->isUnavailable())
@@ -8497,11 +8497,12 @@ SourceFile &ClangImporter::Implementation::getClangSwiftAttrSourceFile(
84978497
StringRef attributeText,
84988498
bool cached
84998499
) {
8500+
::TinyPtrVector<SourceFile *> *sourceFiles = nullptr;
85008501
if (cached) {
8501-
auto &sourceFiles = ClangSwiftAttrSourceFiles[attributeText];
8502+
sourceFiles = &ClangSwiftAttrSourceFiles[attributeText];
85028503

85038504
// Check whether we've already created a source file.
8504-
for (auto sourceFile : sourceFiles) {
8505+
for (auto sourceFile : *sourceFiles) {
85058506
if (sourceFile->getParentModule() == &module)
85068507
return *sourceFile;
85078508
}
@@ -8527,10 +8528,8 @@ SourceFile &ClangImporter::Implementation::getClangSwiftAttrSourceFile(
85278528
auto sourceFile = new (SwiftContext)
85288529
SourceFile(module, SourceFileKind::Library, bufferID);
85298530

8530-
if (cached) {
8531-
auto &sourceFiles = ClangSwiftAttrSourceFiles[attributeText];
8532-
sourceFiles.push_back(sourceFile);
8533-
}
8531+
if (cached)
8532+
sourceFiles->push_back(sourceFile);
85348533

85358534
return *sourceFile;
85368535
}
@@ -9880,9 +9879,9 @@ ClangImporter::Implementation::importDeclContextOf(
98809879
// Clang submodule of the declaration.
98819880
const clang::Module *declSubmodule = *getClangSubmoduleForDecl(decl);
98829881
auto extensionKey = std::make_pair(nominal, declSubmodule);
9883-
auto knownExtension = extensionPoints.find(extensionKey);
9884-
if (knownExtension != extensionPoints.end())
9885-
return knownExtension->second;
9882+
auto [it, inserted] = extensionPoints.try_emplace(extensionKey, nullptr);
9883+
if (!inserted)
9884+
return it->getSecond();
98869885

98879886
// Create a new extension for this nominal type/Clang submodule pair.
98889887
auto ext = ExtensionDecl::create(SwiftContext, SourceLoc(), nullptr, {},
@@ -9895,7 +9894,7 @@ ClangImporter::Implementation::importDeclContextOf(
98959894
// Record this extension so we can find it later. We do this early because
98969895
// once we've set the member loader, we don't know when the compiler will use
98979896
// it and end up back in this method.
9898-
extensionPoints[extensionKey] = ext;
9897+
it->getSecond() = ext;
98999898
ext->setMemberLoader(this, reinterpret_cast<uintptr_t>(declSubmodule));
99009899

99019900
if (auto protoDecl = ext->getExtendedProtocolDecl()) {

lib/ClangImporter/ImportMacro.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,10 +779,11 @@ ValueDecl *ClangImporter::Implementation::importMacro(Identifier name,
779779
PrettyStackTraceStringAction stackRAII{"importing macro", name.str()};
780780

781781
// Look for macros imported with the same name.
782-
auto known = ImportedMacros.find(name);
783-
if (known == ImportedMacros.end()) {
782+
auto [known, inserted] = ImportedMacros.try_emplace(
783+
name, SmallVector<std::pair<const clang::MacroInfo *, ValueDecl *>, 2>{});
784+
if (inserted) {
784785
// Push in a placeholder to break circularity.
785-
ImportedMacros[name].push_back({macro, nullptr});
786+
known->getSecond().push_back({macro, nullptr});
786787
} else {
787788
// Check whether this macro has already been imported.
788789
for (const auto &entry : known->second) {

lib/ClangImporter/ImportType.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3574,8 +3574,8 @@ static ModuleDecl *tryLoadModule(ASTContext &C,
35743574
llvm::DenseMap<Identifier, ModuleDecl *>
35753575
&checkedModules) {
35763576
// If we've already done this check, return the cached result.
3577-
auto known = checkedModules.find(moduleName);
3578-
if (known != checkedModules.end())
3577+
auto [known, inserted] = checkedModules.try_emplace(moduleName, nullptr);
3578+
if (!inserted)
35793579
return known->second;
35803580

35813581
ModuleDecl *module;
@@ -3587,7 +3587,7 @@ static ModuleDecl *tryLoadModule(ASTContext &C,
35873587
else
35883588
module = C.getModuleByIdentifier(moduleName);
35893589

3590-
checkedModules[moduleName] = module;
3590+
known->getSecond() = module;
35913591
return module;
35923592
}
35933593

lib/ClangImporter/ImporterImpl.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -807,10 +807,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
807807
llvm::DenseMap<const Decl *, ArrayRef<ProtocolDecl *>>
808808
ImportedProtocols;
809809

810-
/// The set of declaration context for which we've already ruled out the
811-
/// presence of globals-as-members.
812-
llvm::DenseSet<const IterableDeclContext *> checkedGlobalsAsMembers;
813-
814810
void startedImportingEntity();
815811

816812
public:

0 commit comments

Comments
 (0)