[PATCH] D139483: Reserve memory before pushing back

Alf via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 6 16:04:34 PST 2022


gAlfonso-bit created this revision.
gAlfonso-bit added a reviewer: MaskRay.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
gAlfonso-bit requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

It is generally good practice, if you know how big the vector is going to be in the end, to reserve before continually calling "push_back" or "emplace_back"


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139483

Files:
  llvm/include/llvm/Testing/Support/Error.h
  llvm/lib/Support/VirtualFileSystem.cpp
  llvm/tools/llvm-cov/CodeCoverage.cpp
  llvm/tools/llvm-cov/CoverageReport.cpp
  llvm/unittests/ADT/DenseMapTest.cpp
  llvm/utils/TableGen/GlobalISelEmitter.cpp
  llvm/utils/TableGen/OptParserEmitter.cpp


Index: llvm/utils/TableGen/OptParserEmitter.cpp
===================================================================
--- llvm/utils/TableGen/OptParserEmitter.cpp
+++ llvm/utils/TableGen/OptParserEmitter.cpp
@@ -424,6 +424,7 @@
                  CmpMarshallingOpts);
 
   std::vector<MarshallingInfo> MarshallingInfos;
+  MarshallingInfos.reserve(OptsWithMarshalling.size());
   for (const auto *R : OptsWithMarshalling)
     MarshallingInfos.push_back(createMarshallingInfo(*R));
 
Index: llvm/utils/TableGen/GlobalISelEmitter.cpp
===================================================================
--- llvm/utils/TableGen/GlobalISelEmitter.cpp
+++ llvm/utils/TableGen/GlobalISelEmitter.cpp
@@ -5859,6 +5859,7 @@
   // Emit a table containing the PredicateBitsets objects needed by the matcher
   // and an enum for the matcher to reference them with.
   std::vector<std::vector<Record *>> FeatureBitsets;
+  FeatureBitsets.reserve(Rules.size());
   for (auto &Rule : Rules)
     FeatureBitsets.push_back(Rule.getRequiredFeatures());
   llvm::sort(FeatureBitsets, [&](const std::vector<Record *> &A,
Index: llvm/unittests/ADT/DenseMapTest.cpp
===================================================================
--- llvm/unittests/ADT/DenseMapTest.cpp
+++ llvm/unittests/ADT/DenseMapTest.cpp
@@ -446,6 +446,7 @@
   std::vector<std::pair<int, CountCopyAndMove>> Values;
   // The size is a random value greater than 64 (hardcoded DenseMap min init)
   const int Count = 65;
+  Values.reserve(Count);
   for (int i = 0; i < Count; i++)
     Values.emplace_back(i, CountCopyAndMove());
 
Index: llvm/tools/llvm-cov/CoverageReport.cpp
===================================================================
--- llvm/tools/llvm-cov/CoverageReport.cpp
+++ llvm/tools/llvm-cov/CoverageReport.cpp
@@ -402,8 +402,8 @@
 
   for (StringRef Filename : Files) {
     FileReports.emplace_back(Filename.drop_front(LCP));
-    Pool.async(&CoverageReport::prepareSingleFileReport, Filename,
-               &Coverage, Options, LCP, &FileReports.back(), &Filters);
+    Pool.async(&CoverageReport::prepareSingleFileReport, Filename, &Coverage,
+               Options, LCP, &FileReports.back(), &Filters);
   }
   Pool.wait();
 
@@ -437,7 +437,8 @@
       prepareFileReports(Coverage, Totals, Files, Options, Filters);
 
   std::vector<StringRef> Filenames;
-  for (const FileCoverageSummary &FCS : FileReports)
+  Filenames.reserve(FileReports.size());
+for (const FileCoverageSummary &FCS : FileReports)
     Filenames.emplace_back(FCS.Name);
   adjustColumnWidths(Filenames, {});
 
Index: llvm/tools/llvm-cov/CodeCoverage.cpp
===================================================================
--- llvm/tools/llvm-cov/CodeCoverage.cpp
+++ llvm/tools/llvm-cov/CodeCoverage.cpp
@@ -554,6 +554,7 @@
 
   // Invoke the demangler.
   std::vector<StringRef> ArgsV;
+  ArgsV.reserve(ViewOpts.DemanglerOpts.size());
   for (StringRef Arg : ViewOpts.DemanglerOpts)
     ArgsV.push_back(Arg);
   std::optional<StringRef> Redirects[] = {
Index: llvm/lib/Support/VirtualFileSystem.cpp
===================================================================
--- llvm/lib/Support/VirtualFileSystem.cpp
+++ llvm/lib/Support/VirtualFileSystem.cpp
@@ -1506,6 +1506,7 @@
 
 std::vector<StringRef> RedirectingFileSystem::getRoots() const {
   std::vector<StringRef> R;
+  R.reserve(Roots.size());
   for (const auto &Root : Roots)
     R.push_back(Root->getName());
   return R;
Index: llvm/include/llvm/Testing/Support/Error.h
===================================================================
--- llvm/include/llvm/Testing/Support/Error.h
+++ llvm/include/llvm/Testing/Support/Error.h
@@ -139,7 +139,8 @@
   bool MatchAndExplain(const ErrorHolder &Holder,
                        testing::MatchResultListener *listener) const override {
     std::vector<std::string> Messages;
-    for (const std::shared_ptr<ErrorInfoBase> &Info: Holder.Infos)
+    Messages.reserve(Holder.Infos.size());
+    for (const std::shared_ptr<ErrorInfoBase> &Info : Holder.Infos)
       Messages.push_back(Info->message());
 
     return Matcher.MatchAndExplain(Messages, listener);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139483.480668.patch
Type: text/x-patch
Size: 4120 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221207/b5354e68/attachment-0001.bin>


More information about the llvm-commits mailing list