Skip to content

Commit 7b1707a

Browse files
committed
Cache the results of SwiftASTContext::GetCompileUnitImports()
Every time a scratch context is requested, a call to GetCompileUnitImports() is made, which is both expensive and redundant. This patch adds a cache that keeps track of compile units for which the imports were already performed. This results in a measurable performance improvement on the LLDB test suite. rdar://75381959
1 parent 9c34c22 commit 7b1707a

File tree

3 files changed

+60
-17
lines changed

3 files changed

+60
-17
lines changed

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

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8314,8 +8314,8 @@ bool SwiftASTContext::GetImplicitImports(
83148314
llvm::SmallVectorImpl<swift::AttributedImport<swift::ImportedModule>>
83158315
&modules,
83168316
Status &error) {
8317-
if (!GetCompileUnitImports(swift_ast_context, sc, stack_frame_wp, modules,
8318-
error)) {
8317+
if (!swift_ast_context.GetCompileUnitImports(sc, stack_frame_wp, modules,
8318+
error)) {
83198319
return false;
83208320
}
83218321

@@ -8387,22 +8387,54 @@ bool SwiftASTContext::CacheUserImports(SwiftASTContext &swift_ast_context,
83878387
}
83888388

83898389
bool SwiftASTContext::GetCompileUnitImports(
8390-
SwiftASTContext &swift_ast_context, SymbolContext &sc,
8391-
lldb::StackFrameWP &stack_frame_wp,
8390+
SymbolContext &sc, lldb::StackFrameWP &stack_frame_wp,
83928391
llvm::SmallVectorImpl<swift::AttributedImport<swift::ImportedModule>>
83938392
&modules,
83948393
Status &error) {
8394+
return GetCompileUnitImportsImpl(sc, stack_frame_wp, &modules, error);
8395+
}
8396+
8397+
void SwiftASTContext::PerformCompileUnitImports(
8398+
SymbolContext &sc, lldb::StackFrameWP &stack_frame_wp, Status &error) {
8399+
GetCompileUnitImportsImpl(sc, stack_frame_wp, nullptr, error);
8400+
}
8401+
8402+
static std::pair<Module *, lldb::user_id_t>
8403+
GetCUSignature(CompileUnit &compile_unit) {
8404+
return {compile_unit.GetModule().get(), compile_unit.GetID()};
8405+
}
8406+
8407+
bool SwiftASTContext::GetCompileUnitImportsImpl(
8408+
SymbolContext &sc, lldb::StackFrameWP &stack_frame_wp,
8409+
llvm::SmallVectorImpl<swift::AttributedImport<swift::ImportedModule>>
8410+
*modules,
8411+
Status &error) {
8412+
CompileUnit *compile_unit = sc.comp_unit;
8413+
if (compile_unit)
8414+
// Check the cache if this compile unit's imports were previously
8415+
// requested. If the caller didn't request the list of imported
8416+
// modules then there is nothing left to do for subsequent
8417+
// GetCompileUnitImportsImpl() calls as the previously loaded
8418+
// modules should still be loaded. The fact the we
8419+
// unconditionally return true does not matter because the only
8420+
// way to get here is through void PerformCompileUnitImports(),
8421+
// which discards the return value.
8422+
if (!m_cu_imports.insert(GetCUSignature(*compile_unit)).second)
8423+
// List of imports isn't requested and we already processed this CU?
8424+
if (!modules)
8425+
return true;
8426+
83958427
// Import the Swift standard library and its dependencies.
83968428
SourceModule swift_module;
83978429
swift_module.path.emplace_back("Swift");
83988430
auto *stdlib =
8399-
LoadOneModule(swift_module, swift_ast_context, stack_frame_wp, error);
8431+
LoadOneModule(swift_module, *this, stack_frame_wp, error);
84008432
if (!stdlib)
84018433
return false;
84028434

8403-
modules.emplace_back(swift::ImportedModule(stdlib));
8435+
if (modules)
8436+
modules->emplace_back(swift::ImportedModule(stdlib));
84048437

8405-
CompileUnit *compile_unit = sc.comp_unit;
84068438
if (!compile_unit || compile_unit->GetLanguage() != lldb::eLanguageTypeSwift)
84078439
return true;
84088440

@@ -8417,11 +8449,12 @@ bool SwiftASTContext::GetCompileUnitImports(
84178449
continue;
84188450

84198451
auto *loaded_module =
8420-
LoadOneModule(module, swift_ast_context, stack_frame_wp, error);
8452+
LoadOneModule(module, *this, stack_frame_wp, error);
84218453
if (!loaded_module)
84228454
return false;
84238455

8424-
modules.emplace_back(swift::ImportedModule(loaded_module));
8456+
if (modules)
8457+
modules->emplace_back(swift::ImportedModule(loaded_module));
84258458
}
84268459
return true;
84278460
}

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -773,15 +773,26 @@ class SwiftASTContext : public TypeSystemSwift {
773773
lldb::StackFrameWP &stack_frame_wp,
774774
swift::SourceFile &source_file, Status &error);
775775

776-
/// Retrieve the modules imported by the compilation unit.
777-
static bool GetCompileUnitImports(
778-
SwiftASTContext &swift_ast_context, SymbolContext &sc,
779-
lldb::StackFrameWP &stack_frame_wp,
776+
/// Retrieve/import the modules imported by the compilation
777+
/// unit. Early-exists with false if there was an import failure.
778+
bool GetCompileUnitImports(
779+
SymbolContext &sc, lldb::StackFrameWP &stack_frame_wp,
780780
llvm::SmallVectorImpl<swift::AttributedImport<swift::ImportedModule>>
781781
&modules,
782782
Status &error);
783783

784+
/// Perform all the implicit imports for the current frame.
785+
void PerformCompileUnitImports(SymbolContext &sc,
786+
lldb::StackFrameWP &stack_frame_wp,
787+
Status &error);
788+
784789
protected:
790+
bool GetCompileUnitImportsImpl(
791+
SymbolContext &sc, lldb::StackFrameWP &stack_frame_wp,
792+
llvm::SmallVectorImpl<swift::AttributedImport<swift::ImportedModule>>
793+
*modules,
794+
Status &error);
795+
785796
/// This map uses the string value of ConstStrings as the key, and the
786797
/// TypeBase
787798
/// * as the value. Since the ConstString strings are uniqued, we can use
@@ -867,6 +878,8 @@ class SwiftASTContext : public TypeSystemSwift {
867878
/// respective result (true = loaded, false = failed to load).
868879
std::unordered_map<detail::SwiftLibraryLookupRequest, bool>
869880
library_load_cache;
881+
/// A cache for GetCompileUnitImports();
882+
llvm::DenseSet<std::pair<Module *, lldb::user_id_t>> m_cu_imports;
870883

871884
typedef std::map<Module *, std::vector<lldb::DataBufferSP>> ASTFileDataMap;
872885
ASTFileDataMap m_ast_file_data_map;

lldb/source/Target/Target.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,10 +2557,7 @@ llvm::Optional<SwiftASTContextReader> Target::GetScratchSwiftASTContext(
25572557
!swift_ast_ctx->HasFatalErrors()) {
25582558
StackFrameWP frame_wp(frame_sp);
25592559
SymbolContext sc = frame_sp->GetSymbolContext(lldb::eSymbolContextEverything);
2560-
llvm::SmallVector<swift::AttributedImport<swift::ImportedModule>, 16>
2561-
modules;
2562-
swift_ast_ctx->GetCompileUnitImports(*swift_ast_ctx, sc, frame_wp, modules,
2563-
error);
2560+
swift_ast_ctx->PerformCompileUnitImports(sc, frame_wp, error);
25642561
}
25652562

25662563
if (!swift_ast_ctx)

0 commit comments

Comments
 (0)