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 <matthias@maennich.net>
This commit is contained in:
Matthias Maennich 2017-09-19 16:16:25 +02:00 committed by Brad King
parent a45928cdeb
commit 79b8c3802a
11 changed files with 13 additions and 1 deletions

View File

@ -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());
}

View File

@ -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());
}

View File

@ -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());
}

View File

@ -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());
}

View File

@ -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());
}

View File

@ -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(

View File

@ -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 {

View File

@ -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());
}

View File

@ -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());
}

View File

@ -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);
}

View File

@ -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]);
}