Skip to content

Commit c098b8e

Browse files
Merge pull request #4053 from adrian-prantl/89927216
Cache the result of TypeSystemSwiftTypeRef::LookupClangType().
2 parents c84e3bf + c0279cb commit c098b8e

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ GetTypeAlias(swift::Demangle::Demangler &dem,
190190
}
191191

192192
/// Find a Clang type by name in the modules in \p module_holder.
193-
TypeSP TypeSystemSwiftTypeRef::LookupClangType(StringRef name) {
194-
auto lookup = [](Module &M, StringRef name) -> TypeSP {
193+
TypeSP TypeSystemSwiftTypeRef::LookupClangType(StringRef name_ref) {
194+
auto lookup = [](Module &M, ConstString name) -> TypeSP {
195195
llvm::SmallVector<CompilerContext, 2> decl_context;
196196
decl_context.push_back({CompilerContextKind::AnyModule, ConstString()});
197197
decl_context.push_back({CompilerContextKind::AnyType, ConstString(name)});
@@ -203,18 +203,33 @@ TypeSP TypeSystemSwiftTypeRef::LookupClangType(StringRef name) {
203203
return {};
204204
return clang_types.GetTypeAtIndex(0);
205205
};
206-
if (auto *M = GetModule())
207-
return lookup(*M, name);
206+
207+
// Check the cache first. Negative results are also cached.
208+
TypeSP result;
209+
ConstString name(name_ref);
210+
if (m_clang_type_cache.Lookup(name.AsCString(), result))
211+
return result;
212+
213+
if (auto *M = GetModule()) {
214+
TypeSP result = lookup(*M, name);
215+
// Cache it.
216+
m_clang_type_cache.Insert(name.AsCString(), result);
217+
return result;
218+
}
208219

209220
SwiftASTContext *target_holder = GetSwiftASTContext();
210221
if (!target_holder)
211222
return {};
212223
TargetSP target_sp = target_holder->GetTargetWP().lock();
213224
if (!target_sp)
214225
return {};
215-
TypeSP result;
216226
target_sp->GetImages().ForEach([&](const ModuleSP &module) -> bool {
227+
// Don't recursively call into LookupClangTypes() to avoid filling
228+
// hundreds of image caches with negative results.
217229
result = lookup(const_cast<Module &>(*module), name);
230+
// Cache it in the expression context.
231+
if (result)
232+
m_clang_type_cache.Insert(name.AsCString(), result);
218233
return !result;
219234
});
220235
return result;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
396396

397397
/// All lldb::Type pointers produced by DWARFASTParser Swift go here.
398398
ThreadSafeDenseMap<const char *, lldb::TypeSP> m_swift_type_map;
399+
/// Map ConstString Clang type identifiers to Clang types.
400+
ThreadSafeDenseMap<const char *, lldb::TypeSP> m_clang_type_cache;
399401
};
400402

401403
/// This one owns a SwiftASTContextForExpressions.

0 commit comments

Comments
 (0)