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

Alexandre Ganea via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 12 18:48:44 PST 2021


aganea updated this revision to Diff 316291.
aganea added a comment.

Simplify.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D94555/new/

https://reviews.llvm.org/D94555

Files:
  lld/COFF/DebugTypes.cpp


Index: lld/COFF/DebugTypes.cpp
===================================================================
--- lld/COFF/DebugTypes.cpp
+++ lld/COFF/DebugTypes.cpp
@@ -679,6 +679,26 @@
   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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94555.316291.patch
Type: text/x-patch
Size: 1136 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210113/5072a3cf/attachment.bin>


More information about the llvm-commits mailing list