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

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 1 11:51:40 PDT 2022


Author: Michael Eisel
Date: 2022-06-01T14:51:34-04:00
New Revision: f5709066e3b05b27eb95b8b42d9d01daca48d93c

URL: https://github.com/llvm/llvm-project/commit/f5709066e3b05b27eb95b8b42d9d01daca48d93c
DIFF: https://github.com/llvm/llvm-project/commit/f5709066e3b05b27eb95b8b42d9d01daca48d93c.diff

LOG: [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

Added: 
    

Modified: 
    lld/MachO/SyntheticSections.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index 3a5e9daad7291..d717716a22d74 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -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());
 


        


More information about the llvm-commits mailing list