[llvm] 7f86bb0 - [llvm] Call reserve before push_back in a loop
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 8 00:42:57 PST 2022
Author: Gregory Alfonso
Date: 2022-12-08T08:42:50Z
New Revision: 7f86bb0a713cbd2643c4ed5da9bd92d9b24eaa72
URL: https://github.com/llvm/llvm-project/commit/7f86bb0a713cbd2643c4ed5da9bd92d9b24eaa72
DIFF: https://github.com/llvm/llvm-project/commit/7f86bb0a713cbd2643c4ed5da9bd92d9b24eaa72.diff
LOG: [llvm] Call reserve before push_back in a loop
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"
Reviewed By: MaskRay
Differential Revision: https://reviews.llvm.org/D139483
Added:
Modified:
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
Removed:
################################################################################
diff --git a/llvm/include/llvm/Testing/Support/Error.h b/llvm/include/llvm/Testing/Support/Error.h
index 41abc718a6135..14bac9fd9f8a0 100644
--- a/llvm/include/llvm/Testing/Support/Error.h
+++ b/llvm/include/llvm/Testing/Support/Error.h
@@ -139,7 +139,8 @@ class ErrorMessageMatches
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);
diff --git a/llvm/lib/Support/VirtualFileSystem.cpp b/llvm/lib/Support/VirtualFileSystem.cpp
index ace58270ef949..ded5607e0c53e 100644
--- a/llvm/lib/Support/VirtualFileSystem.cpp
+++ b/llvm/lib/Support/VirtualFileSystem.cpp
@@ -1506,6 +1506,7 @@ void RedirectingFileSystem::setRedirection(
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;
diff --git a/llvm/tools/llvm-cov/CodeCoverage.cpp b/llvm/tools/llvm-cov/CodeCoverage.cpp
index 63732fab5889f..d2dfbc28d14b5 100644
--- a/llvm/tools/llvm-cov/CodeCoverage.cpp
+++ b/llvm/tools/llvm-cov/CodeCoverage.cpp
@@ -554,6 +554,7 @@ void CodeCoverageTool::demangleSymbols(const CoverageMapping &Coverage) {
// 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[] = {
diff --git a/llvm/tools/llvm-cov/CoverageReport.cpp b/llvm/tools/llvm-cov/CoverageReport.cpp
index c91edc2b0dc08..be042aa9e0272 100644
--- a/llvm/tools/llvm-cov/CoverageReport.cpp
+++ b/llvm/tools/llvm-cov/CoverageReport.cpp
@@ -437,6 +437,7 @@ void CoverageReport::renderFileReports(
prepareFileReports(Coverage, Totals, Files, Options, Filters);
std::vector<StringRef> Filenames;
+ Filenames.reserve(FileReports.size());
for (const FileCoverageSummary &FCS : FileReports)
Filenames.emplace_back(FCS.Name);
adjustColumnWidths(Filenames, {});
diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp
index 6893f439b46f4..2d01316e658b8 100644
--- a/llvm/unittests/ADT/DenseMapTest.cpp
+++ b/llvm/unittests/ADT/DenseMapTest.cpp
@@ -446,6 +446,7 @@ TEST(DenseMapCustomTest, InitFromIterator) {
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());
diff --git a/llvm/utils/TableGen/GlobalISelEmitter.cpp b/llvm/utils/TableGen/GlobalISelEmitter.cpp
index 06aec93ff7f13..96afc00dc98d0 100644
--- a/llvm/utils/TableGen/GlobalISelEmitter.cpp
+++ b/llvm/utils/TableGen/GlobalISelEmitter.cpp
@@ -5858,6 +5858,7 @@ void GlobalISelEmitter::run(raw_ostream &OS) {
// 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,
diff --git a/llvm/utils/TableGen/OptParserEmitter.cpp b/llvm/utils/TableGen/OptParserEmitter.cpp
index 717a994c219c5..9fff65ec1a2f8 100644
--- a/llvm/utils/TableGen/OptParserEmitter.cpp
+++ b/llvm/utils/TableGen/OptParserEmitter.cpp
@@ -424,6 +424,7 @@ void EmitOptParser(RecordKeeper &Records, raw_ostream &OS) {
CmpMarshallingOpts);
std::vector<MarshallingInfo> MarshallingInfos;
+ MarshallingInfos.reserve(OptsWithMarshalling.size());
for (const auto *R : OptsWithMarshalling)
MarshallingInfos.push_back(createMarshallingInfo(*R));
More information about the llvm-commits
mailing list