Skip to content

Commit da5e498

Browse files
author
Gabor Horvath
committed
[cxx-interop] Remove some duplicated lookups
Repeatedly lookup up a key from a dictionary can be justified whenever the content of the dictionary might change between the lookups (so any references into the dictionary might get invalidated). We had a couple of instances where as far as I can tell no such modifications should happen between two lookups with identical keys. This PR simplifies the code to remove the extra lookups. It also removes a dictionary that was completely unused.
1 parent a3fac71 commit da5e498

File tree

6 files changed

+48
-49
lines changed

6 files changed

+48
-49
lines changed

lib/ClangImporter/ClangImporter.cpp

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

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

@@ -6846,8 +6847,9 @@ void ClangImporter::Implementation::dumpSwiftLookupTables() {
68466847
// Print out the lookup tables for the various modules.
68476848
for (auto moduleName : moduleNames) {
68486849
llvm::errs() << "<<" << moduleName << " lookup table>>\n";
6849-
LookupTables[moduleName]->deserializeAll();
6850-
LookupTables[moduleName]->dump(llvm::errs());
6850+
auto &lookupTable = LookupTables[moduleName];
6851+
lookupTable->deserializeAll();
6852+
lookupTable->dump(llvm::errs());
68516853
}
68526854

68536855
llvm::errs() << "<<Bridging header lookup table>>\n";
@@ -7410,8 +7412,10 @@ ClangImporter::getCXXFunctionTemplateSpecialization(SubstitutionMap subst,
74107412
if (!newFn)
74117413
return ConcreteDeclRef(decl);
74127414

7413-
if (Impl.specializedFunctionTemplates.count(newFn))
7414-
return ConcreteDeclRef(Impl.specializedFunctionTemplates[newFn]);
7415+
auto [fnIt, inserted] =
7416+
Impl.specializedFunctionTemplates.try_emplace(newFn, nullptr);
7417+
if (!inserted)
7418+
return ConcreteDeclRef(fnIt->second);
74157419

74167420
auto newDecl = cast_or_null<ValueDecl>(
74177421
decl->getASTContext().getClangModuleLoader()->importDeclDirectly(
@@ -7434,7 +7438,7 @@ ClangImporter::getCXXFunctionTemplateSpecialization(SubstitutionMap subst,
74347438
}
74357439
}
74367440

7437-
Impl.specializedFunctionTemplates[newFn] = newDecl;
7441+
fnIt->getSecond() = newDecl;
74387442
return ConcreteDeclRef(newDecl);
74397443
}
74407444

@@ -7539,11 +7543,9 @@ ValueDecl *ClangImporter::Implementation::importBaseMemberDecl(
75397543
// Make sure we don't clone the decl again for this class, as that would
75407544
// result in multiple definitions of the same symbol.
75417545
std::pair<ValueDecl *, DeclContext *> key = {decl, newContext};
7542-
auto known = clonedBaseMembers.find(key);
7543-
if (known == clonedBaseMembers.end()) {
7544-
ValueDecl *cloned = cloneBaseMemberDecl(decl, newContext);
7545-
known = clonedBaseMembers.insert({key, cloned}).first;
7546-
}
7546+
auto [known, inserted] = clonedBaseMembers.try_emplace(key, nullptr);
7547+
if (inserted)
7548+
known->getSecond() = cloneBaseMemberDecl(decl, newContext);
75477549

75487550
return known->second;
75497551
}
@@ -8006,7 +8008,7 @@ CxxRecordAsSwiftType::evaluate(Evaluator &evaluator,
80068008
mod->lookupValue(desc.ctx.getIdentifier(swiftName), NLKind::UnqualifiedLookup,
80078009
results);
80088010
if (results.size() == 1) {
8009-
if (dyn_cast<ClassDecl>(results[0]))
8011+
if (isa<ClassDecl>(results[0]))
80108012
return results[0];
80118013
}
80128014
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>
@@ -2578,11 +2579,11 @@ namespace {
25782579
Impl.addAlternateDecl(subscriptImpl, subscript);
25792580
}
25802581

2581-
if (Impl.cxxDereferenceOperators.find(result) !=
2582-
Impl.cxxDereferenceOperators.end()) {
2582+
auto getterAndSetterIt = Impl.cxxDereferenceOperators.find(result);
2583+
if (getterAndSetterIt != Impl.cxxDereferenceOperators.end()) {
25832584
// If this type has a dereference operator, synthesize a computed
25842585
// property called `pointee` for it.
2585-
auto getterAndSetter = Impl.cxxDereferenceOperators[result];
2586+
auto getterAndSetter = getterAndSetterIt->second;
25862587

25872588
VarDecl *pointeeProperty =
25882589
synthesizer.makeDereferencedPointeeProperty(
@@ -5076,10 +5077,9 @@ namespace {
50765077

50775078
// Check whether there's some special method to import.
50785079
if (!forceClassMethod) {
5079-
if (dc == Impl.importDeclContextOf(decl, decl->getDeclContext()) &&
5080-
!Impl.ImportedDecls[{decl->getCanonicalDecl(), getVersion()}])
5081-
Impl.ImportedDecls[{decl->getCanonicalDecl(), getVersion()}]
5082-
= result;
5080+
if (dc == Impl.importDeclContextOf(decl, decl->getDeclContext()))
5081+
Impl.ImportedDecls.try_emplace(
5082+
{decl->getCanonicalDecl(), getVersion()}, result);
50835083

50845084
if (importedName.isSubscriptAccessor()) {
50855085
// If this was a subscript accessor, try to create a
@@ -7696,8 +7696,8 @@ SwiftDeclConverter::importSubscript(Decl *decl,
76967696

76977697
// Note that we've created this subscript.
76987698
Impl.Subscripts[{getter, setter}] = subscript;
7699-
if (setter && !Impl.Subscripts[{getter, nullptr}])
7700-
Impl.Subscripts[{getter, nullptr}] = subscript;
7699+
if (setter)
7700+
Impl.Subscripts.try_emplace({getter, nullptr}, subscript);
77017701

77027702
// Make the getter/setter methods unavailable.
77037703
if (!getter->isUnavailable())
@@ -8413,11 +8413,12 @@ SourceFile &ClangImporter::Implementation::getClangSwiftAttrSourceFile(
84138413
StringRef attributeText,
84148414
bool cached
84158415
) {
8416+
::TinyPtrVector<SourceFile *> *sourceFiles = nullptr;
84168417
if (cached) {
8417-
auto &sourceFiles = ClangSwiftAttrSourceFiles[attributeText];
8418+
sourceFiles = &ClangSwiftAttrSourceFiles[attributeText];
84188419

84198420
// Check whether we've already created a source file.
8420-
for (auto sourceFile : sourceFiles) {
8421+
for (auto sourceFile : *sourceFiles) {
84218422
if (sourceFile->getParentModule() == &module)
84228423
return *sourceFile;
84238424
}
@@ -8443,10 +8444,8 @@ SourceFile &ClangImporter::Implementation::getClangSwiftAttrSourceFile(
84438444
auto sourceFile = new (SwiftContext)
84448445
SourceFile(module, SourceFileKind::Library, bufferID);
84458446

8446-
if (cached) {
8447-
auto &sourceFiles = ClangSwiftAttrSourceFiles[attributeText];
8448-
sourceFiles.push_back(sourceFile);
8449-
}
8447+
if (cached)
8448+
sourceFiles->push_back(sourceFile);
84508449

84518450
return *sourceFile;
84528451
}
@@ -9839,9 +9838,9 @@ ClangImporter::Implementation::importDeclContextOf(
98399838
// Clang submodule of the declaration.
98409839
const clang::Module *declSubmodule = *getClangSubmoduleForDecl(decl);
98419840
auto extensionKey = std::make_pair(nominal, declSubmodule);
9842-
auto knownExtension = extensionPoints.find(extensionKey);
9843-
if (knownExtension != extensionPoints.end())
9844-
return knownExtension->second;
9841+
auto [it, inserted] = extensionPoints.try_emplace(extensionKey, nullptr);
9842+
if (!inserted)
9843+
return it->getSecond();
98459844

98469845
// Create a new extension for this nominal type/Clang submodule pair.
98479846
auto ext = ExtensionDecl::create(SwiftContext, SourceLoc(), nullptr, {},
@@ -9854,7 +9853,7 @@ ClangImporter::Implementation::importDeclContextOf(
98549853
// Record this extension so we can find it later. We do this early because
98559854
// once we've set the member loader, we don't know when the compiler will use
98569855
// it and end up back in this method.
9857-
extensionPoints[extensionKey] = ext;
9856+
it->getSecond() = ext;
98589857
ext->setMemberLoader(this, reinterpret_cast<uintptr_t>(declSubmodule));
98599858

98609859
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
@@ -801,10 +801,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
801801
llvm::DenseMap<const Decl *, ArrayRef<ProtocolDecl *>>
802802
ImportedProtocols;
803803

804-
/// The set of declaration context for which we've already ruled out the
805-
/// presence of globals-as-members.
806-
llvm::DenseSet<const IterableDeclContext *> checkedGlobalsAsMembers;
807-
808804
void startedImportingEntity();
809805

810806
public:

0 commit comments

Comments
 (0)