Skip to content

Commit bbad294

Browse files
Merge pull request #4104 from adrian-prantl/90666627
Cache the result of TypeSystemSwiftTypeRef::LookupClangType(). …
2 parents 900c3b6 + cf4f938 commit bbad294

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp

+20-5
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ GetTypeAlias(swift::Demangle::Demangler &dem,
151151
}
152152

153153
/// Find a Clang type by name in the modules in \p module_holder.
154-
TypeSP TypeSystemSwiftTypeRef::LookupClangType(StringRef name) {
155-
auto lookup = [](Module &M, StringRef name) -> TypeSP {
154+
TypeSP TypeSystemSwiftTypeRef::LookupClangType(StringRef name_ref) {
155+
auto lookup = [](Module &M, ConstString name) -> TypeSP {
156156
llvm::SmallVector<CompilerContext, 2> decl_context;
157157
decl_context.push_back({CompilerContextKind::AnyModule, ConstString()});
158158
decl_context.push_back({CompilerContextKind::AnyType, ConstString(name)});
@@ -164,18 +164,33 @@ TypeSP TypeSystemSwiftTypeRef::LookupClangType(StringRef name) {
164164
return {};
165165
return clang_types.GetTypeAtIndex(0);
166166
};
167-
if (auto *M = GetModule())
168-
return lookup(*M, name);
167+
168+
// Check the cache first. Negative results are also cached.
169+
TypeSP result;
170+
ConstString name(name_ref);
171+
if (m_clang_type_cache.Lookup(name.AsCString(), result))
172+
return result;
173+
174+
if (auto *M = GetModule()) {
175+
TypeSP result = lookup(*M, name);
176+
// Cache it.
177+
m_clang_type_cache.Insert(name.AsCString(), result);
178+
return result;
179+
}
169180

170181
SwiftASTContext *target_holder = GetSwiftASTContext();
171182
if (!target_holder)
172183
return {};
173184
TargetSP target_sp = target_holder->GetTarget().lock();
174185
if (!target_sp)
175186
return {};
176-
TypeSP result;
177187
target_sp->GetImages().ForEach([&](const ModuleSP &module) -> bool {
188+
// Don't recursively call into LookupClangTypes() to avoid filling
189+
// hundreds of image caches with negative results.
178190
result = lookup(const_cast<Module &>(*module), name);
191+
// Cache it in the expression context.
192+
if (result)
193+
m_clang_type_cache.Insert(name.AsCString(), result);
179194
return !result;
180195
});
181196
return result;

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h

+2
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
382382

383383
/// All lldb::Type pointers produced by DWARFASTParser Swift go here.
384384
ThreadSafeDenseMap<const char *, lldb::TypeSP> m_swift_type_map;
385+
/// Map ConstString Clang type identifiers to Clang types.
386+
ThreadSafeDenseMap<const char *, lldb::TypeSP> m_clang_type_cache;
385387
};
386388

387389
} // namespace lldb_private

0 commit comments

Comments
 (0)