Skip to content

Commit cf8f7b8

Browse files
committed
Reland "[NFC][lldb] Speed up lookup of shared modules" (229d860)
Original commit message: By profiling LLDB debugging a Swift application without a dSYM and a large amount of .o files, I identified that querying shared modules was the biggest bottleneck when running "frame variable", and Clang types need to be searched. One of the reasons for that slowness is that the shared module list can can grow very large, and the search through it is O(n). To solve this issue, this patch adds a new hashmap to the shared module list whose key is the name of the module, and the value is all the modules that share that name. This should speed up any search where the query contains the module name. rdar://156753350
1 parent 9faac93 commit cf8f7b8

File tree

1 file changed

+229
-0
lines changed

1 file changed

+229
-0
lines changed

lldb/source/Core/ModuleList.cpp

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,235 @@ size_t ModuleList::GetIndexForModule(const Module *module) const {
755755
}
756756

757757
namespace {
758+
/// A wrapper around ModuleList for shared modules. Provides fast lookups for
759+
/// file-based ModuleSpec queries.
760+
class SharedModuleList {
761+
public:
762+
/// Finds all the modules matching the module_spec, and adds them to \p
763+
/// matching_module_list.
764+
void FindModules(const ModuleSpec &module_spec,
765+
ModuleList &matching_module_list) const {
766+
std::lock_guard<std::recursive_mutex> guard(GetMutex());
767+
// Try map first for performance - if found, skip expensive full list
768+
// search.
769+
if (FindModulesInMap(module_spec, matching_module_list))
770+
return;
771+
m_list.FindModules(module_spec, matching_module_list);
772+
// Assert that modules were found in the list but not the map, it's
773+
// because the module_spec has no filename or the found module has a
774+
// different filename. For example, when searching by UUID and finding a
775+
// module with an alias.
776+
assert((matching_module_list.IsEmpty() ||
777+
module_spec.GetFileSpec().GetFilename().IsEmpty() ||
778+
module_spec.GetFileSpec().GetFilename() !=
779+
matching_module_list.GetModuleAtIndex(0)
780+
->GetFileSpec()
781+
.GetFilename()) &&
782+
"Search by name not found in SharedModuleList's map");
783+
}
784+
785+
ModuleSP FindModule(const Module *module_ptr) {
786+
if (!module_ptr)
787+
return ModuleSP();
788+
789+
std::lock_guard<std::recursive_mutex> guard(GetMutex());
790+
if (ModuleSP result = FindModuleInMap(module_ptr))
791+
return result;
792+
return m_list.FindModule(module_ptr);
793+
}
794+
795+
// UUID searches bypass map since UUIDs aren't indexed by filename.
796+
ModuleSP FindModule(const UUID &uuid) const {
797+
return m_list.FindModule(uuid);
798+
}
799+
800+
void Append(const ModuleSP &module_sp, bool use_notifier) {
801+
if (!module_sp)
802+
return;
803+
std::lock_guard<std::recursive_mutex> guard(GetMutex());
804+
m_list.Append(module_sp, use_notifier);
805+
AddToMap(module_sp);
806+
}
807+
808+
size_t RemoveOrphans(bool mandatory) {
809+
std::unique_lock<std::recursive_mutex> lock(GetMutex(), std::defer_lock);
810+
if (mandatory) {
811+
lock.lock();
812+
} else {
813+
if (!lock.try_lock())
814+
return 0;
815+
}
816+
size_t total_count = 0;
817+
size_t run_count;
818+
do {
819+
// Remove indexed orphans first, then remove non-indexed orphans. This
820+
// order is important because the shared count will be different if a
821+
// module is indexed or not.
822+
run_count = RemoveOrphansFromMapAndList();
823+
run_count += m_list.RemoveOrphans(mandatory);
824+
total_count += run_count;
825+
// Because removing orphans might make new orphans, remove from both
826+
// containers until a fixed-point is reached.
827+
} while (run_count != 0);
828+
829+
return total_count;
830+
}
831+
832+
bool Remove(const ModuleSP &module_sp, bool use_notifier = true) {
833+
if (!module_sp)
834+
return false;
835+
std::lock_guard<std::recursive_mutex> guard(GetMutex());
836+
RemoveFromMap(module_sp.get());
837+
return m_list.Remove(module_sp, use_notifier);
838+
}
839+
840+
void ReplaceEquivalent(const ModuleSP &module_sp,
841+
llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules) {
842+
std::lock_guard<std::recursive_mutex> guard(GetMutex());
843+
m_list.ReplaceEquivalent(module_sp, old_modules);
844+
ReplaceEquivalentInMap(module_sp);
845+
}
846+
847+
bool RemoveIfOrphaned(const Module *module_ptr) {
848+
std::lock_guard<std::recursive_mutex> guard(GetMutex());
849+
RemoveFromMap(module_ptr, /*if_orphaned=*/true);
850+
return m_list.RemoveIfOrphaned(module_ptr);
851+
}
852+
853+
std::recursive_mutex &GetMutex() const { return m_list.GetMutex(); }
854+
855+
private:
856+
ModuleSP FindModuleInMap(const Module *module_ptr) const {
857+
if (!module_ptr->GetFileSpec().GetFilename())
858+
return ModuleSP();
859+
ConstString name = module_ptr->GetFileSpec().GetFilename();
860+
auto it = m_name_to_modules.find(name);
861+
if (it == m_name_to_modules.end())
862+
return ModuleSP();
863+
const llvm::SmallVectorImpl<ModuleSP> &vector = it->second;
864+
for (const ModuleSP &module_sp : vector) {
865+
if (module_sp.get() == module_ptr)
866+
return module_sp;
867+
}
868+
return ModuleSP();
869+
}
870+
871+
bool FindModulesInMap(const ModuleSpec &module_spec,
872+
ModuleList &matching_module_list) const {
873+
auto it = m_name_to_modules.find(module_spec.GetFileSpec().GetFilename());
874+
if (it == m_name_to_modules.end())
875+
return false;
876+
const llvm::SmallVectorImpl<ModuleSP> &vector = it->second;
877+
bool found = false;
878+
for (const ModuleSP &module_sp : vector) {
879+
if (module_sp->MatchesModuleSpec(module_spec)) {
880+
matching_module_list.Append(module_sp);
881+
found = true;
882+
}
883+
}
884+
return found;
885+
}
886+
887+
void AddToMap(const ModuleSP &module_sp) {
888+
ConstString name = module_sp->GetFileSpec().GetFilename();
889+
if (name.IsEmpty())
890+
return;
891+
m_name_to_modules[name].push_back(module_sp);
892+
}
893+
894+
void RemoveFromMap(const Module *module_ptr, bool if_orphaned = false) {
895+
if (!module_ptr)
896+
return;
897+
ConstString name = module_ptr->GetFileSpec().GetFilename();
898+
if (!m_name_to_modules.contains(name))
899+
return;
900+
llvm::SmallVectorImpl<ModuleSP> &vec = m_name_to_modules[name];
901+
for (auto *it = vec.begin(); it != vec.end(); ++it) {
902+
if (it->get() == module_ptr) {
903+
// use_count == 2 means only held by map and list (orphaned).
904+
constexpr long kUseCountOrphaned = 2;
905+
if (!if_orphaned || it->use_count() == kUseCountOrphaned) {
906+
vec.erase(it);
907+
break;
908+
}
909+
}
910+
}
911+
}
912+
913+
void ReplaceEquivalentInMap(const ModuleSP &module_sp) {
914+
RemoveEquivalentModulesFromMap(module_sp);
915+
AddToMap(module_sp);
916+
}
917+
918+
void RemoveEquivalentModulesFromMap(const ModuleSP &module_sp) {
919+
ConstString name = module_sp->GetFileSpec().GetFilename();
920+
if (name.IsEmpty())
921+
return;
922+
923+
auto it = m_name_to_modules.find(name);
924+
if (it == m_name_to_modules.end())
925+
return;
926+
927+
// First remove any equivalent modules. Equivalent modules are modules
928+
// whose path, platform path and architecture match.
929+
ModuleSpec equivalent_module_spec(module_sp->GetFileSpec(),
930+
module_sp->GetArchitecture());
931+
equivalent_module_spec.GetPlatformFileSpec() =
932+
module_sp->GetPlatformFileSpec();
933+
934+
llvm::SmallVectorImpl<ModuleSP> &vec = it->second;
935+
llvm::erase_if(vec, [&equivalent_module_spec](ModuleSP &element) {
936+
return element->MatchesModuleSpec(equivalent_module_spec);
937+
});
938+
}
939+
940+
/// Remove orphans from the vector and return the removed modules.
941+
ModuleList RemoveOrphansFromVector(llvm::SmallVectorImpl<ModuleSP> &vec) {
942+
ModuleList to_remove;
943+
for (int i = vec.size() - 1; i >= 0; --i) {
944+
ModuleSP module = vec[i];
945+
constexpr long kUseCountOrphaned = 2;
946+
constexpr long kUseCountLocalVariable = 1;
947+
// use_count == 3: map + list + local variable = orphaned.
948+
if (module.use_count() == kUseCountOrphaned + kUseCountLocalVariable) {
949+
to_remove.Append(module);
950+
vec.erase(vec.begin() + i);
951+
}
952+
}
953+
return to_remove;
954+
}
955+
956+
/// Remove orphans that exist in both the map and list. This does not remove
957+
/// any orphans that exist exclusively on the list.
958+
///
959+
/// The mutex must be locked by the caller.
960+
int RemoveOrphansFromMapAndList() {
961+
// Modules might hold shared pointers to other modules, so removing one
962+
// module might orphan other modules. Keep removing modules until
963+
// there are no further modules that can be removed.
964+
bool made_progress = true;
965+
int remove_count = 0;
966+
while (made_progress) {
967+
made_progress = false;
968+
for (auto &[name, vec] : m_name_to_modules) {
969+
if (vec.empty())
970+
continue;
971+
ModuleList to_remove = RemoveOrphansFromVector(vec);
972+
remove_count += to_remove.GetSize();
973+
made_progress = !to_remove.IsEmpty();
974+
m_list.Remove(to_remove);
975+
}
976+
}
977+
return remove_count;
978+
}
979+
980+
ModuleList m_list;
981+
982+
/// A hash map from a module's filename to all the modules that share that
983+
/// filename, for fast module lookups by name.
984+
llvm::DenseMap<ConstString, llvm::SmallVector<ModuleSP, 1>> m_name_to_modules;
985+
};
986+
758987
struct SharedModuleListInfo {
759988
ModuleList module_list;
760989
ModuleListProperties module_list_properties;

0 commit comments

Comments
 (0)