[PATCH] D40697: Cache modulo values for the .gnu.hash section.

Rui Ueyama via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 30 17:28:43 PST 2017


ruiu created this revision.
Herald added a subscriber: emaste.

This change actually makes the linker slightly faster. My observation
is that, with this patch, link time of clang without debug is about 1%
faster.


https://reviews.llvm.org/D40697

Files:
  lld/ELF/SyntheticSections.cpp
  lld/ELF/SyntheticSections.h


Index: lld/ELF/SyntheticSections.h
===================================================================
--- lld/ELF/SyntheticSections.h
+++ lld/ELF/SyntheticSections.h
@@ -465,6 +465,7 @@
     Symbol *Sym;
     size_t StrTabOffset;
     uint32_t Hash;
+    uint32_t BucketIdx;
   };
 
   std::vector<Entry> Symbols;
Index: lld/ELF/SyntheticSections.cpp
===================================================================
--- lld/ELF/SyntheticSections.cpp
+++ lld/ELF/SyntheticSections.cpp
@@ -1745,7 +1745,7 @@
   // Group symbols by hash value.
   std::vector<std::vector<Entry>> Syms(NBuckets);
   for (const Entry &Ent : Symbols)
-    Syms[Ent.Hash % NBuckets].push_back(Ent);
+    Syms[Ent.BucketIdx].push_back(Ent);
 
   // Write hash buckets. Hash buckets contain indices in the following
   // hash value table.
@@ -1792,20 +1792,22 @@
   if (Mid == V.end())
     return;
 
-  for (SymbolTableEntry &Ent : llvm::make_range(Mid, V.end())) {
-    Symbol *B = Ent.Sym;
-    Symbols.push_back({B, Ent.StrTabOffset, hashGnu(B->getName())});
-  }
-
   // We chose load factor 4 for the on-disk hash table. For each hash
   // collision, the dynamic linker will compare a uint32_t hash value.
   // Since the integer comparison is quite fast, we believe we can make
   // the load factor even larger. 4 is just a conservative choice.
-  NBuckets = std::max<size_t>(Symbols.size() / 4, 1);
+  NBuckets = NextPowerOf2(std::max<size_t>((V.end() - Mid) / 4, 1));
+
+  for (SymbolTableEntry &Ent : llvm::make_range(Mid, V.end())) {
+    Symbol *B = Ent.Sym;
+    uint32_t Hash = hashGnu(B->getName());
+    uint32_t BucketIdx = Hash % NBuckets;
+    Symbols.push_back({B, Ent.StrTabOffset, Hash, BucketIdx});
+  }
 
   std::stable_sort(Symbols.begin(), Symbols.end(),
-                   [&](const Entry &L, const Entry &R) {
-                     return L.Hash % NBuckets < R.Hash % NBuckets;
+                   [](const Entry &L, const Entry &R) {
+                     return L.BucketIdx < R.BucketIdx;
                    });
 
   V.erase(Mid, V.end());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D40697.125064.patch
Type: text/x-patch
Size: 2053 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171201/cdcc46ab/attachment.bin>


More information about the llvm-commits mailing list