[lld/mac] Cache file IDs of symbols in emitStabs for faster sorting

This reduces the time emitStabs() takes by about 275ms, or 3% of overall
linking time for the project I'm on. Although the parent function is run in
parallel, it's one of the slowest tasks in that concurrent batch (I have
another optimization for another slow task as well).

Differential Revision: https://reviews.llvm.org/D126785
This commit is contained in:
Michael Eisel 2022-06-01 14:49:19 -04:00 committed by Nico Weber
parent f3bdb56d61
commit f5709066e3
1 changed files with 8 additions and 5 deletions

View File

@ -890,7 +890,9 @@ void SymtabSection::emitStabs() {
stabs.emplace_back(std::move(astStab));
}
std::vector<Defined *> symbolsNeedingStabs;
// Cache the file ID for each symbol in an std::pair for faster sorting.
using SortingPair = std::pair<Defined *, int>;
std::vector<SortingPair> symbolsNeedingStabs;
for (const SymtabEntry &entry :
concat<SymtabEntry>(localSymbols, externalSymbols)) {
Symbol *sym = entry.sym;
@ -913,19 +915,20 @@ void SymtabSection::emitStabs() {
if (!file || !file->compileUnit)
continue;
symbolsNeedingStabs.push_back(defined);
symbolsNeedingStabs.emplace_back(defined, defined->isec->getFile()->id);
}
}
llvm::stable_sort(symbolsNeedingStabs, [&](Defined *a, Defined *b) {
return a->isec->getFile()->id < b->isec->getFile()->id;
llvm::stable_sort(symbolsNeedingStabs, [&](const SortingPair &a, const SortingPair &b) {
return a.second < b.second;
});
// Emit STABS symbols so that dsymutil and/or the debugger can map address
// regions in the final binary to the source and object files from which they
// originated.
InputFile *lastFile = nullptr;
for (Defined *defined : symbolsNeedingStabs) {
for (SortingPair &pair : symbolsNeedingStabs) {
Defined *defined = pair.first;
InputSection *isec = defined->isec;
ObjFile *file = cast<ObjFile>(isec->getFile());