[llvm-branch-commits] [lld] e7a371f - [LLD][COFF] Avoid std::vector resizes during type merging

Alexandre Ganea via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jan 13 11:40:03 PST 2021


Author: Alexandre Ganea
Date: 2021-01-13T14:35:03-05:00
New Revision: e7a371f9fd0076c187f4cd1a9c7546867faeb19b

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

LOG: [LLD][COFF] Avoid std::vector resizes during type merging

Consistently saves approx. 0.6 sec (out of 18 sec) on a large output (400 MB EXE, 2 GB PDB).

Differential Revision: https://reviews.llvm.org/D94555

Added: 
    

Modified: 
    lld/COFF/DebugTypes.cpp

Removed: 
    


################################################################################
diff  --git a/lld/COFF/DebugTypes.cpp b/lld/COFF/DebugTypes.cpp
index 52c24aaf214f..fedcb054540f 100644
--- a/lld/COFF/DebugTypes.cpp
+++ b/lld/COFF/DebugTypes.cpp
@@ -679,6 +679,26 @@ void TpiSource::mergeUniqueTypeRecords(ArrayRef<uint8_t> typeRecords,
   auto nextUniqueIndex = uniqueTypes.begin();
   assert(mergedTpi.recs.empty());
   assert(mergedIpi.recs.empty());
+
+  // Pre-compute the number of elements in advance to avoid std::vector resizes.
+  unsigned nbTpiRecs = 0;
+  unsigned nbIpiRecs = 0;
+  forEachTypeChecked(typeRecords, [&](const CVType &ty) {
+    if (nextUniqueIndex != uniqueTypes.end() &&
+        *nextUniqueIndex == ghashIndex) {
+      assert(ty.length() <= codeview::MaxRecordLength);
+      size_t newSize = alignTo(ty.length(), 4);
+      (isIdRecord(ty.kind()) ? nbIpiRecs : nbTpiRecs) += newSize;
+      ++nextUniqueIndex;
+    }
+    ++ghashIndex;
+  });
+  mergedTpi.recs.reserve(nbTpiRecs);
+  mergedIpi.recs.reserve(nbIpiRecs);
+
+  // Do the actual type merge.
+  ghashIndex = 0;
+  nextUniqueIndex = uniqueTypes.begin();
   forEachTypeChecked(typeRecords, [&](const CVType &ty) {
     if (nextUniqueIndex != uniqueTypes.end() &&
         *nextUniqueIndex == ghashIndex) {


        


More information about the llvm-branch-commits mailing list