Skip to content

Commit fbd4e65

Browse files
Switched to llvm::SmallDenseMap for live_vars in PrintInstructions
Address code review feedback suggesting the use of LLVM's DenseMap family over std::unordered_map for consistency and potential performance benefits within LLDB. Replaced: std::unordered_map<lldb::user_id_t, VarState> with: llvm::SmallDenseMap<lldb::user_id_t, VarState, 8> The small buffer size of 8 is a heuristic for typical numbers of live variables in scope, reducing allocations for common cases.
1 parent cb0cd3a commit fbd4e65

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

lldb/source/Core/Disassembler.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@
4444
#include "lldb/lldb-private-enumerations.h"
4545
#include "lldb/lldb-private-interfaces.h"
4646
#include "lldb/lldb-private-types.h"
47+
#include "llvm/ADT/DenseMap.h"
4748
#include "llvm/Support/Compiler.h"
4849
#include "llvm/TargetParser/Triple.h"
4950

51+
5052
#include <cstdint>
5153
#include <cstring>
52-
#include <unordered_map>
5354
#include <utility>
5455

5556
#include <cassert>
@@ -403,7 +404,7 @@ void Disassembler::PrintInstructions(Debugger &debugger, const ArchSpec &arch,
403404
};
404405

405406
// Track live variables across instructions (keyed by stable LLDB user_id_t)
406-
std::unordered_map<lldb::user_id_t, VarState> live_vars;
407+
llvm::SmallDenseMap<lldb::user_id_t, VarState, 8> live_vars; // 8 is a good small-buffer guess
407408

408409
// Stateful annotator: updates live_vars and returns only what should be
409410
// printed for THIS instruction.
@@ -432,9 +433,10 @@ void Disassembler::PrintInstructions(Debugger &debugger, const ArchSpec &arch,
432433
VariableListSP var_list_sp = frame->GetInScopeVariableList(true);
433434
if (!var_list_sp) {
434435
// No variables in scope: everything previously live becomes <undef>
435-
for (auto it = live_vars.begin(); it != live_vars.end();) {
436-
events.push_back(llvm::formatv("{0} = <undef>", it->second.name).str());
437-
it = live_vars.erase(it);
436+
for (auto I = live_vars.begin(), E = live_vars.end(); I != E; ) {
437+
auto Cur = I++;
438+
events.push_back(llvm::formatv("{0} = <undef>", Cur->second.name).str());
439+
live_vars.erase(Cur);
438440
}
439441
frame->ChangePC(original_pc);
440442
return events;
@@ -484,31 +486,31 @@ void Disassembler::PrintInstructions(Debugger &debugger, const ArchSpec &arch,
484486
if (loc_clean.empty())
485487
continue;
486488

487-
// Update map + decide if we print
488-
auto it = live_vars.find(var_id);
489-
if (it == live_vars.end()) {
490-
// New var → print
491-
live_vars.emplace(var_id,
492-
VarState{std::string(name), loc_clean.str(), true});
489+
auto insert_res = live_vars.insert({var_id,
490+
VarState{std::string(name),
491+
loc_clean.str(),
492+
/*seen_this_inst*/ true}});
493+
if (insert_res.second) {
494+
// Newly inserted → print
493495
events.push_back(llvm::formatv("{0} = {1}", name, loc_clean).str());
494496
} else {
495-
it->second.seen_this_inst = true;
496-
if (it->second.last_loc != loc_clean) {
497-
it->second.last_loc = loc_clean.str();
498-
events.push_back(
499-
llvm::formatv("{0} = {1}", it->second.name, loc_clean).str());
497+
// Already present
498+
VarState &vs = insert_res.first->second;
499+
vs.seen_this_inst = true;
500+
if (vs.last_loc != loc_clean) {
501+
vs.last_loc = loc_clean.str();
502+
events.push_back(llvm::formatv("{0} = {1}", vs.name, loc_clean).str());
500503
}
501504
}
502505
}
503506

504507
// Anything previously live that we didn't see a location for at this inst
505508
// is now <undef>
506-
for (auto it = live_vars.begin(); it != live_vars.end();) {
507-
if (!it->second.seen_this_inst) {
508-
events.push_back(llvm::formatv("{0} = <undef>", it->second.name).str());
509-
it = live_vars.erase(it);
510-
} else {
511-
++it;
509+
for (auto I = live_vars.begin(), E = live_vars.end(); I != E; ) {
510+
auto Cur = I++;
511+
if (!Cur->second.seen_this_inst) {
512+
events.push_back(llvm::formatv("{0} = <undef>", Cur->second.name).str());
513+
live_vars.erase(Cur);
512514
}
513515
}
514516

0 commit comments

Comments
 (0)