From 79b8c3802a430577a83ead5b0baab7038a813116 Mon Sep 17 00:00:00 2001 From: Matthias Maennich Date: Tue, 19 Sep 2017 16:16:25 +0200 Subject: 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 --- Source/CTest/cmCTestBuildHandler.cxx | 1 + Source/CTest/cmCTestGIT.cxx | 1 + Source/CTest/cmCTestP4.cxx | 1 + Source/CTest/cmCTestVC.cxx | 1 + Source/cmCTest.cxx | 2 ++ Source/cmCommonTargetGenerator.cxx | 1 + Source/cmServerProtocol.cxx | 3 ++- Source/cmStateDirectory.cxx | 1 + Source/cmSystemTools.cxx | 1 + Source/cmcmd.cxx | 1 + Source/ctest.cxx | 1 + 11 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Source/CTest/cmCTestBuildHandler.cxx b/Source/CTest/cmCTestBuildHandler.cxx index 1da42d4..f4fc769 100644 --- a/Source/CTest/cmCTestBuildHandler.cxx +++ b/Source/CTest/cmCTestBuildHandler.cxx @@ -769,6 +769,7 @@ int cmCTestBuildHandler::RunMakeCommand(const char* command, int* retVal, } std::vector argv; + argv.reserve(args.size() + 1); for (std::string const& arg : args) { argv.push_back(arg.c_str()); } diff --git a/Source/CTest/cmCTestGIT.cxx b/Source/CTest/cmCTestGIT.cxx index 7fe74af..8cb795e 100644 --- a/Source/CTest/cmCTestGIT.cxx +++ b/Source/CTest/cmCTestGIT.cxx @@ -214,6 +214,7 @@ bool cmCTestGIT::UpdateByCustom(std::string const& custom) std::vector git_custom_command; cmSystemTools::ExpandListArgument(custom, git_custom_command, true); std::vector 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()); } diff --git a/Source/CTest/cmCTestP4.cxx b/Source/CTest/cmCTestP4.cxx index 11f6a00..fdf8932 100644 --- a/Source/CTest/cmCTestP4.cxx +++ b/Source/CTest/cmCTestP4.cxx @@ -464,6 +464,7 @@ bool cmCTestP4::UpdateCustom(const std::string& custom) cmSystemTools::ExpandListArgument(custom, p4_custom_command, true); std::vector 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()); } diff --git a/Source/CTest/cmCTestVC.cxx b/Source/CTest/cmCTestVC.cxx index 7e09ef0..fd7f37a 100644 --- a/Source/CTest/cmCTestVC.cxx +++ b/Source/CTest/cmCTestVC.cxx @@ -56,6 +56,7 @@ bool cmCTestVC::InitialCheckout(const char* command) // Construct the initial checkout command line. std::vector args = cmSystemTools::ParseArguments(command); std::vector vc_co; + vc_co.reserve(args.size() + 1); for (std::string const& arg : args) { vc_co.push_back(arg.c_str()); } diff --git a/Source/cmCTest.cxx b/Source/cmCTest.cxx index e248219..4ea1493 100644 --- a/Source/cmCTest.cxx +++ b/Source/cmCTest.cxx @@ -969,6 +969,7 @@ int cmCTest::RunMakeCommand(const char* command, std::string& output, } std::vector 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 const& args, const char* dir, double timeout, Encoding encoding) { std::vector argv; + argv.reserve(args.size() + 1); for (std::string const& a : args) { argv.push_back(a.c_str()); } diff --git a/Source/cmCommonTargetGenerator.cxx b/Source/cmCommonTargetGenerator.cxx index bd4077f..1189606 100644 --- a/Source/cmCommonTargetGenerator.cxx +++ b/Source/cmCommonTargetGenerator.cxx @@ -194,6 +194,7 @@ std::string cmCommonTargetGenerator::GetManifests() this->GeneratorTarget->GetManifests(manifest_srcs, this->ConfigName); std::vector manifests; + manifests.reserve(manifest_srcs.size()); for (cmSourceFile const* manifest_src : manifest_srcs) { manifests.push_back(this->LocalCommonGenerator->ConvertToOutputFormat( this->LocalCommonGenerator->ConvertToRelativePath( diff --git a/Source/cmServerProtocol.cxx b/Source/cmServerProtocol.cxx index 1b47608..e835b7a 100644 --- a/Source/cmServerProtocol.cxx +++ b/Source/cmServerProtocol.cxx @@ -602,11 +602,12 @@ bool LanguageData::operator==(const LanguageData& other) const void LanguageData::SetDefines(const std::set& defines) { std::vector 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 { diff --git a/Source/cmStateDirectory.cxx b/Source/cmStateDirectory.cxx index 5aa8e5b..85e6366 100644 --- a/Source/cmStateDirectory.cxx +++ b/Source/cmStateDirectory.cxx @@ -442,6 +442,7 @@ const char* cmStateDirectory::GetProperty(const std::string& prop, std::vector child_dirs; std::vector const& children = this->DirectoryState->Children; + child_dirs.reserve(children.size()); for (cmStateSnapshot const& ci : children) { child_dirs.push_back(ci.GetDirectory().GetCurrentSource()); } diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 6fdfd44..26073d3 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -700,6 +700,7 @@ bool cmSystemTools::RunSingleCommand(std::vector const& command, double timeout, Encoding encoding) { std::vector argv; + argv.reserve(command.size() + 1); for (std::string const& cmd : command) { argv.push_back(cmd.c_str()); } diff --git a/Source/cmcmd.cxx b/Source/cmcmd.cxx index 5a9e321..c0c7d03 100644 --- a/Source/cmcmd.cxx +++ b/Source/cmcmd.cxx @@ -348,6 +348,7 @@ int cmcmd::HandleCoCompileCommands(std::vector& args) std::bind(&cmcmd::HandleCppCheck, a1, a2, a3); // copy the command options to a vector of strings std::vector commandOptions; + commandOptions.reserve(coCompileTypes.size()); for (const auto& i : coCompileTypes) { commandOptions.push_back(i.first); } diff --git a/Source/ctest.cxx b/Source/ctest.cxx index fe24a72..f6466fa 100644 --- a/Source/ctest.cxx +++ b/Source/ctest.cxx @@ -186,6 +186,7 @@ int main(int argc, char const* const* argv) // copy the args to a vector std::vector args; + args.reserve(argc); for (int i = 0; i < argc; ++i) { args.push_back(argv[i]); } -- cgit v0.12