diff --git a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp index 4843ae771e72d..fe9ae1f719071 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp +++ b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp @@ -151,8 +151,8 @@ GetTypeAlias(swift::Demangle::Demangler &dem, } /// Find a Clang type by name in the modules in \p module_holder. -TypeSP TypeSystemSwiftTypeRef::LookupClangType(StringRef name) { - auto lookup = [](Module &M, StringRef name) -> TypeSP { +TypeSP TypeSystemSwiftTypeRef::LookupClangType(StringRef name_ref) { + auto lookup = [](Module &M, ConstString name) -> TypeSP { llvm::SmallVector decl_context; decl_context.push_back({CompilerContextKind::AnyModule, ConstString()}); decl_context.push_back({CompilerContextKind::AnyType, ConstString(name)}); @@ -164,8 +164,19 @@ TypeSP TypeSystemSwiftTypeRef::LookupClangType(StringRef name) { return {}; return clang_types.GetTypeAtIndex(0); }; - if (auto *M = GetModule()) - return lookup(*M, name); + + // Check the cache first. Negative results are also cached. + TypeSP result; + ConstString name(name_ref); + if (m_clang_type_cache.Lookup(name.AsCString(), result)) + return result; + + if (auto *M = GetModule()) { + TypeSP result = lookup(*M, name); + // Cache it. + m_clang_type_cache.Insert(name.AsCString(), result); + return result; + } SwiftASTContext *target_holder = GetSwiftASTContext(); if (!target_holder) @@ -173,9 +184,13 @@ TypeSP TypeSystemSwiftTypeRef::LookupClangType(StringRef name) { TargetSP target_sp = target_holder->GetTarget().lock(); if (!target_sp) return {}; - TypeSP result; target_sp->GetImages().ForEach([&](const ModuleSP &module) -> bool { + // Don't recursively call into LookupClangTypes() to avoid filling + // hundreds of image caches with negative results. result = lookup(const_cast(*module), name); + // Cache it in the expression context. + if (result) + m_clang_type_cache.Insert(name.AsCString(), result); return !result; }); return result; diff --git a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h index e79ef26efc59e..a5dcc4a6e0021 100644 --- a/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h +++ b/lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.h @@ -382,6 +382,8 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift { /// All lldb::Type pointers produced by DWARFASTParser Swift go here. ThreadSafeDenseMap m_swift_type_map; + /// Map ConstString Clang type identifiers to Clang types. + ThreadSafeDenseMap m_clang_type_cache; }; } // namespace lldb_private