Skip to content

Commit 719cddc

Browse files
committed
Optimize getSymbols
1 parent 0d30017 commit 719cddc

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

lld/ELF/BPSectionOrderer.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,17 @@ template <> struct lld::BPOrdererTraits<struct BPOrdererELF> {
2626
};
2727
namespace {
2828
struct BPOrdererELF : lld::BPOrderer<BPOrdererELF> {
29+
DenseMap<const InputSectionBase *, Defined *> secToSym;
30+
2931
static uint64_t getSize(const Section &sec) { return sec.getSize(); }
3032
static bool isCodeSection(const Section &sec) {
3133
return sec.flags & llvm::ELF::SHF_EXECINSTR;
3234
}
33-
static SmallVector<Defined *, 0> getSymbols(const Section &sec) {
34-
SmallVector<Defined *, 0> symbols;
35-
for (auto *sym : sec.file->getSymbols())
36-
if (auto *d = llvm::dyn_cast_or_null<Defined>(sym))
37-
if (d->size > 0 && d->section == &sec)
38-
symbols.emplace_back(d);
39-
return symbols;
35+
ArrayRef<Defined *> getSymbols(const Section &sec) {
36+
auto it = secToSym.find(&sec);
37+
if (it == secToSym.end())
38+
return {};
39+
return ArrayRef(it->second);
4040
}
4141

4242
static void
@@ -69,28 +69,27 @@ DenseMap<const InputSectionBase *, int> elf::runBalancedPartitioning(
6969
// Collect candidate sections and associated symbols.
7070
SmallVector<InputSectionBase *> sections;
7171
DenseMap<CachedHashStringRef, DenseSet<unsigned>> rootSymbolToSectionIdxs;
72-
DenseSet<const InputSectionBase *> seenSections;
72+
BPOrdererELF orderer;
7373

7474
auto addSection = [&](Symbol &sym) {
7575
auto *d = dyn_cast<Defined>(&sym);
76-
if (!d || d->size == 0)
76+
if (!d)
7777
return;
7878
auto *sec = dyn_cast_or_null<InputSectionBase>(d->section);
79-
if (sec && seenSections.insert(sec).second) {
80-
rootSymbolToSectionIdxs[CachedHashStringRef(getRootSymbol(sym.getName()))]
81-
.insert(sections.size());
82-
sections.emplace_back(sec);
83-
}
79+
if (!sec || sec->size == 0 || !orderer.secToSym.try_emplace(sec, d).second)
80+
return;
81+
rootSymbolToSectionIdxs[CachedHashStringRef(getRootSymbol(sym.getName()))]
82+
.insert(sections.size());
83+
sections.emplace_back(sec);
8484
};
8585

8686
for (Symbol *sym : ctx.symtab->getSymbols())
8787
addSection(*sym);
8888
for (ELFFileBase *file : ctx.objectFiles)
8989
for (Symbol *sym : file->getLocalSymbols())
9090
addSection(*sym);
91-
92-
return BPOrdererELF::computeOrder(profilePath, forFunctionCompression,
93-
forDataCompression,
94-
compressionSortStartupFunctions, verbose,
95-
sections, rootSymbolToSectionIdxs);
91+
return orderer.computeOrder(profilePath, forFunctionCompression,
92+
forDataCompression,
93+
compressionSortStartupFunctions, verbose,
94+
sections, rootSymbolToSectionIdxs);
9695
}

lld/test/ELF/bp-section-orderer.s

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# RUN: ld.lld a.o --irpgo-profile=a.profdata --bp-startup-sort=function --verbose-bp-section-orderer --icf=all 2>&1 | FileCheck %s --check-prefix=STARTUP-FUNC-ORDER
2222

2323
# STARTUP-FUNC-ORDER: Ordered 3 sections using balanced partitioning
24+
# STARTUP-FUNC-ORDER: Total area under the page fault curve: 3.
2425

2526
# RUN: ld.lld -o - a.o --symbol-ordering-file a.orderfile --irpgo-profile=a.profdata --bp-startup-sort=function | llvm-nm --numeric-sort --format=just-symbols - | FileCheck %s --check-prefix=ORDERFILE
2627
# RUN: ld.lld -o - a.o --symbol-ordering-file a.orderfile --bp-compression-sort=both | llvm-nm --numeric-sort --format=just-symbols - | FileCheck %s --check-prefix=ORDERFILE

0 commit comments

Comments
 (0)