[llvm] [llvm-objdump] Optimize live element tracking (PR #158763)
James Henderson via llvm-commits
llvm-commits at lists.llvm.org
Thu Nov 20 01:16:26 PST 2025
================
@@ -205,14 +231,55 @@ unsigned LiveElementPrinter::moveToFirstVarColumn(formatted_raw_ostream &OS) {
return FirstUnprintedLogicalColumn;
}
-unsigned LiveElementPrinter::findFreeColumn() {
- for (unsigned ColIdx = 0; ColIdx < ActiveCols.size(); ++ColIdx)
- if (!ActiveCols[ColIdx].isActive())
- return ColIdx;
+unsigned LiveElementPrinter::getOrCreateColumn(unsigned ElementIdx) {
+ // Check if the element already has an assigned column.
+ auto it = ElementToColumn.find(ElementIdx);
+ if (it != ElementToColumn.end())
+ return it->second;
+
+ unsigned ColIdx;
+ if (!FreeCols.empty()) {
+ // Get the smallest available index from the set.
+ ColIdx = *FreeCols.begin();
+ // Remove the index from the set.
+ FreeCols.erase(FreeCols.begin());
+ } else {
+ // No free columns, so create a new one.
+ ColIdx = ActiveCols.size();
+ ActiveCols.emplace_back();
+ }
- size_t OldSize = ActiveCols.size();
- ActiveCols.grow(std::max<size_t>(OldSize * 2, 1));
- return OldSize;
+ // Assign the element to the column and update the map.
+ ElementToColumn[ElementIdx] = ColIdx;
+ ActiveCols[ColIdx].ElementIdx = ElementIdx;
+ return ColIdx;
+}
+
+void LiveElementPrinter::freeColumn(unsigned ColIdx) {
+ unsigned ElementIdx = ActiveCols[ColIdx].ElementIdx;
+
+ // Clear the column's data and add it to the free list.
+ ActiveCols[ColIdx].ElementIdx = Column::NullElementIdx;
+ ActiveCols[ColIdx].LiveIn = false;
+ ActiveCols[ColIdx].LiveOut = false;
+ ActiveCols[ColIdx].MustDrawLabel = false;
+
+ // Remove the element's entry from the map and add the column to the free
+ // list.
+ ElementToColumn.erase(ElementIdx);
+ FreeCols.insert(ColIdx);
+}
+
+std::vector<unsigned>
+LiveElementPrinter::getSortedActiveElementIndices() const {
+ // Get all element indexes that currently have an assigned column.
+ std::vector<unsigned> Indices;
+ for (const auto &Pair : ElementToColumn)
+ Indices.push_back(Pair.first);
+
+ // Sort by the DWARF discovery order.
+ llvm::stable_sort(Indices);
----------------
jh7370 wrote:
How costly is this sort? It feels like it doesn't fit, given all the other efforts to improve the performance you've been making.
There are a few other places where we sort by DWARF discovery order too, so similar questions apply in those cases too.
https://github.com/llvm/llvm-project/pull/158763
More information about the llvm-commits
mailing list