Skip to content

Reland "[NFC][lldb] Speed up lookup of shared modules" (229d860) #152607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 12, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions lldb/source/Core/ModuleList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,14 +783,12 @@ class SharedModuleList {
"Search by name not found in SharedModuleList's map");
}

ModuleSP FindModule(const Module *module_ptr) {
if (!module_ptr)
return ModuleSP();
ModuleSP FindModule(const Module &module) {

std::lock_guard<std::recursive_mutex> guard(GetMutex());
if (ModuleSP result = FindModuleInMap(module_ptr))
if (ModuleSP result = FindModuleInMap(module))
return result;
return m_list.FindModule(module_ptr);
return m_list.FindModule(&module);
}

// UUID searches bypass map since UUIDs aren't indexed by filename.
Expand Down Expand Up @@ -834,7 +832,7 @@ class SharedModuleList {
if (!module_sp)
return false;
std::lock_guard<std::recursive_mutex> guard(GetMutex());
RemoveFromMap(module_sp.get());
RemoveFromMap(*module_sp.get());
return m_list.Remove(module_sp, use_notifier);
}

Expand All @@ -847,23 +845,23 @@ class SharedModuleList {

bool RemoveIfOrphaned(const Module *module_ptr) {
std::lock_guard<std::recursive_mutex> guard(GetMutex());
RemoveFromMap(module_ptr, /*if_orphaned=*/true);
RemoveFromMap(*module_ptr, /*if_orphaned=*/true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is not safe, right? The argument is not guaranteed to be non-null.

return m_list.RemoveIfOrphaned(module_ptr);
}

std::recursive_mutex &GetMutex() const { return m_list.GetMutex(); }

private:
ModuleSP FindModuleInMap(const Module *module_ptr) const {
if (!module_ptr->GetFileSpec().GetFilename())
ModuleSP FindModuleInMap(const Module &module) const {
if (!module.GetFileSpec().GetFilename())
return ModuleSP();
ConstString name = module_ptr->GetFileSpec().GetFilename();
ConstString name = module.GetFileSpec().GetFilename();
auto it = m_name_to_modules.find(name);
if (it == m_name_to_modules.end())
return ModuleSP();
const llvm::SmallVectorImpl<ModuleSP> &vector = it->second;
for (const ModuleSP &module_sp : vector) {
if (module_sp.get() == module_ptr)
if (module_sp.get() == &module)
return module_sp;
}
return ModuleSP();
Expand All @@ -888,15 +886,13 @@ class SharedModuleList {
m_name_to_modules[name].push_back(module_sp);
}

void RemoveFromMap(const Module *module_ptr, bool if_orphaned = false) {
if (!module_ptr)
return;
ConstString name = module_ptr->GetFileSpec().GetFilename();
void RemoveFromMap(const Module &module, bool if_orphaned = false) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why this one changed, we were checking for null here, so we can't assume it is now non-null....

ConstString name = module.GetFileSpec().GetFilename();
if (!m_name_to_modules.contains(name))
return;
llvm::SmallVectorImpl<ModuleSP> &vec = m_name_to_modules[name];
for (auto *it = vec.begin(); it != vec.end(); ++it) {
if (it->get() == module_ptr) {
if (it->get() == &module) {
if (!if_orphaned || it->use_count() == kUseCountOrphaned) {
vec.erase(it);
break;
Expand Down
Loading