Skip to content

Commit 6d6646f

Browse files
committed
Visit the current module first when looking up clang types (NFC)
1 parent 1d54081 commit 6d6646f

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,17 @@ static TypeSP LookupClangType(Module &m,
211211
}
212212

213213
TypeSP TypeSystemSwiftTypeRef::LookupClangType(
214-
StringRef name_ref, llvm::ArrayRef<CompilerContext> decl_context) {
214+
StringRef name_ref, llvm::ArrayRef<CompilerContext> decl_context,
215+
ExecutionContext *exe_ctx) {
215216
Module *m = GetModule();
216217
if (!m)
217218
return {};
218219
return ::LookupClangType(const_cast<Module &>(*m), decl_context);
219220
}
220221

221222
TypeSP TypeSystemSwiftTypeRefForExpressions::LookupClangType(
222-
StringRef name_ref, llvm::ArrayRef<CompilerContext> decl_context) {
223+
StringRef name_ref, llvm::ArrayRef<CompilerContext> decl_context,
224+
ExecutionContext *exe_ctx) {
223225
// Check the cache first. Negative results are also cached.
224226
TypeSP result;
225227
ConstString name(name_ref);
@@ -229,15 +231,33 @@ TypeSP TypeSystemSwiftTypeRefForExpressions::LookupClangType(
229231
TargetSP target_sp = GetTargetWP().lock();
230232
if (!target_sp)
231233
return {};
232-
target_sp->GetImages().ForEach([&](const ModuleSP &m) -> bool {
234+
235+
ModuleSP cur_module;
236+
if (exe_ctx)
237+
if (StackFrame *frame = exe_ctx->GetFramePtr())
238+
cur_module =
239+
frame->GetSymbolContext(lldb::eSymbolContextModule).module_sp;
240+
241+
auto lookup = [&](const ModuleSP &m) -> bool {
242+
// Already visited this.
243+
if (m == cur_module)
244+
return true;
245+
233246
// Don't recursively call into LookupClangTypes() to avoid filling
234247
// hundreds of image caches with negative results.
235248
result = ::LookupClangType(const_cast<Module &>(*m), decl_context);
236249
// Cache it in the expression context.
237250
if (result)
238251
m_clang_type_cache.Insert(name.AsCString(), result);
239252
return !result;
240-
});
253+
};
254+
255+
// Visit the current module first as a performance optimization heuristic.
256+
if (cur_module)
257+
if (!lookup(cur_module))
258+
return result;
259+
260+
target_sp->GetImages().ForEach(lookup);
241261
return result;
242262
}
243263

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@ class TypeSystemSwiftTypeRef : public TypeSystemSwift {
386386
/// context.
387387
virtual lldb::TypeSP
388388
LookupClangType(llvm::StringRef name_ref,
389-
llvm::ArrayRef<CompilerContext> decl_context);
389+
llvm::ArrayRef<CompilerContext> decl_context,
390+
ExecutionContext *exe_ctx = nullptr);
390391

391392
/// Attempts to convert a Clang type into a Swift type.
392393
/// For example, int is converted to Int32.
@@ -538,9 +539,11 @@ class TypeSystemSwiftTypeRefForExpressions : public TypeSystemSwiftTypeRef {
538539
/// Returns how often ModulesDidLoad was called.
539540
unsigned GetGeneration() const { return m_generation; }
540541
/// Performs a target-wide search.
542+
/// \param exe_ctx is a hint for where to look first.
541543
lldb::TypeSP
542544
LookupClangType(llvm::StringRef name_ref,
543-
llvm::ArrayRef<CompilerContext> decl_context) override;
545+
llvm::ArrayRef<CompilerContext> decl_context,
546+
ExecutionContext *exe_ctx) override;
544547

545548

546549
friend class SwiftASTContextForExpressions;

0 commit comments

Comments
 (0)