Browse Source

Improve several occurrences of vector::push_back in loops

Fix issues diagnosed by clang-tidy by pre-allocating the vector capacity
before the loop [performance-inefficient-vector-operation].

Signed-off-by: Matthias Maennich <[email protected]>
Matthias Maennich 8 years ago
parent
commit
79b8c3802a

+ 1 - 0
Source/CTest/cmCTestBuildHandler.cxx

@@ -769,6 +769,7 @@ int cmCTestBuildHandler::RunMakeCommand(const char* command, int* retVal,
   }
 
   std::vector<const char*> argv;
+  argv.reserve(args.size() + 1);
   for (std::string const& arg : args) {
     argv.push_back(arg.c_str());
   }

+ 1 - 0
Source/CTest/cmCTestGIT.cxx

@@ -214,6 +214,7 @@ bool cmCTestGIT::UpdateByCustom(std::string const& custom)
   std::vector<std::string> git_custom_command;
   cmSystemTools::ExpandListArgument(custom, git_custom_command, true);
   std::vector<char const*> git_custom;
+  git_custom.reserve(git_custom_command.size() + 1);
   for (std::string const& i : git_custom_command) {
     git_custom.push_back(i.c_str());
   }

+ 1 - 0
Source/CTest/cmCTestP4.cxx

@@ -464,6 +464,7 @@ bool cmCTestP4::UpdateCustom(const std::string& custom)
   cmSystemTools::ExpandListArgument(custom, p4_custom_command, true);
 
   std::vector<char const*> p4_custom;
+  p4_custom.reserve(p4_custom_command.size() + 1);
   for (std::string const& i : p4_custom_command) {
     p4_custom.push_back(i.c_str());
   }

+ 1 - 0
Source/CTest/cmCTestVC.cxx

@@ -56,6 +56,7 @@ bool cmCTestVC::InitialCheckout(const char* command)
   // Construct the initial checkout command line.
   std::vector<std::string> args = cmSystemTools::ParseArguments(command);
   std::vector<char const*> vc_co;
+  vc_co.reserve(args.size() + 1);
   for (std::string const& arg : args) {
     vc_co.push_back(arg.c_str());
   }

+ 2 - 0
Source/cmCTest.cxx

@@ -969,6 +969,7 @@ int cmCTest::RunMakeCommand(const char* command, std::string& output,
   }
 
   std::vector<const char*> argv;
+  argv.reserve(args.size() + 1);
   for (std::string const& a : args) {
     argv.push_back(a.c_str());
   }
@@ -2569,6 +2570,7 @@ bool cmCTest::RunCommand(std::vector<std::string> const& args,
                          const char* dir, double timeout, Encoding encoding)
 {
   std::vector<const char*> argv;
+  argv.reserve(args.size() + 1);
   for (std::string const& a : args) {
     argv.push_back(a.c_str());
   }

+ 1 - 0
Source/cmCommonTargetGenerator.cxx

@@ -194,6 +194,7 @@ std::string cmCommonTargetGenerator::GetManifests()
   this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName);
 
   std::vector<std::string> manifests;
+  manifests.reserve(manifest_srcs.size());
   for (cmSourceFile const* manifest_src : manifest_srcs) {
     manifests.push_back(this->LocalCommonGenerator->ConvertToOutputFormat(
       this->LocalCommonGenerator->ConvertToRelativePath(

+ 2 - 1
Source/cmServerProtocol.cxx

@@ -602,11 +602,12 @@ bool LanguageData::operator==(const LanguageData& other) const
 void LanguageData::SetDefines(const std::set<std::string>& defines)
 {
   std::vector<std::string> result;
+  result.reserve(defines.size());
   for (std::string const& i : defines) {
     result.push_back(i);
   }
   std::sort(result.begin(), result.end());
-  Defines = result;
+  Defines = std::move(result);
 }
 
 namespace std {

+ 1 - 0
Source/cmStateDirectory.cxx

@@ -442,6 +442,7 @@ const char* cmStateDirectory::GetProperty(const std::string& prop,
     std::vector<std::string> child_dirs;
     std::vector<cmStateSnapshot> const& children =
       this->DirectoryState->Children;
+    child_dirs.reserve(children.size());
     for (cmStateSnapshot const& ci : children) {
       child_dirs.push_back(ci.GetDirectory().GetCurrentSource());
     }

+ 1 - 0
Source/cmSystemTools.cxx

@@ -700,6 +700,7 @@ bool cmSystemTools::RunSingleCommand(std::vector<std::string> const& command,
                                      double timeout, Encoding encoding)
 {
   std::vector<const char*> argv;
+  argv.reserve(command.size() + 1);
   for (std::string const& cmd : command) {
     argv.push_back(cmd.c_str());
   }

+ 1 - 0
Source/cmcmd.cxx

@@ -348,6 +348,7 @@ int cmcmd::HandleCoCompileCommands(std::vector<std::string>& args)
     std::bind(&cmcmd::HandleCppCheck, a1, a2, a3);
   // copy the command options to a vector of strings
   std::vector<std::string> commandOptions;
+  commandOptions.reserve(coCompileTypes.size());
   for (const auto& i : coCompileTypes) {
     commandOptions.push_back(i.first);
   }

+ 1 - 0
Source/ctest.cxx

@@ -186,6 +186,7 @@ int main(int argc, char const* const* argv)
 
   // copy the args to a vector
   std::vector<std::string> args;
+  args.reserve(argc);
   for (int i = 0; i < argc; ++i) {
     args.push_back(argv[i]);
   }