From af3fd6f22f317d4209ff679e4f7c8dd8d2d0f89f Mon Sep 17 00:00:00 2001 From: Aaron Orenstein Date: Fri, 11 Aug 2017 14:11:18 -0700 Subject: Performance: Add an index to Change cmLocalGenerator::GeneratorTargets. Add an index to Change cmLocalGenerator::GeneratorTargets for faster lookup by name. Also changed a bunch of uses of cmLocalGenerator::GetGeneratorTargets() to take const references instead of copying the vector. Represent generator targets as a map (name -> target) to make name lookups more efficient instead of looping through the entire vector to find the desired one. --- Source/cmComputeTargetDepends.cxx | 2 +- Source/cmExtraCodeBlocksGenerator.cxx | 10 ++++++---- Source/cmExtraCodeLiteGenerator.cxx | 5 +++-- Source/cmExtraEclipseCDT4Generator.cxx | 9 +++++---- Source/cmExtraKateGenerator.cxx | 2 +- Source/cmExtraSublimeTextGenerator.cxx | 5 +++-- Source/cmGlobalGenerator.cxx | 8 ++++---- Source/cmGlobalKdevelopGenerator.cxx | 5 +++-- Source/cmGlobalUnixMakefileGenerator3.cxx | 23 ++++++++++++----------- Source/cmGraphVizWriter.cxx | 6 ++++-- Source/cmLocalGenerator.cxx | 27 ++++++++++++++------------- Source/cmLocalGenerator.h | 6 +++++- Source/cmLocalNinjaGenerator.cxx | 4 ++-- Source/cmLocalUnixMakefileGenerator3.cxx | 16 ++++++++-------- 14 files changed, 71 insertions(+), 57 deletions(-) diff --git a/Source/cmComputeTargetDepends.cxx b/Source/cmComputeTargetDepends.cxx index ff19eac..8a3a671 100644 --- a/Source/cmComputeTargetDepends.cxx +++ b/Source/cmComputeTargetDepends.cxx @@ -161,7 +161,7 @@ void cmComputeTargetDepends::CollectTargets() std::vector const& lgens = this->GlobalGenerator->GetLocalGenerators(); for (unsigned int i = 0; i < lgens.size(); ++i) { - const std::vector targets = + const std::vector& targets = lgens[i]->GetGeneratorTargets(); for (std::vector::const_iterator ti = targets.begin(); ti != targets.end(); ++ti) { diff --git a/Source/cmExtraCodeBlocksGenerator.cxx b/Source/cmExtraCodeBlocksGenerator.cxx index 5b7b827..e7a8975 100644 --- a/Source/cmExtraCodeBlocksGenerator.cxx +++ b/Source/cmExtraCodeBlocksGenerator.cxx @@ -296,8 +296,9 @@ void cmExtraCodeBlocksGenerator::CreateNewProjectFile( // and UTILITY targets for (std::vector::const_iterator lg = lgs.begin(); lg != lgs.end(); lg++) { - std::vector targets = (*lg)->GetGeneratorTargets(); - for (std::vector::iterator ti = targets.begin(); + const std::vector& targets = + (*lg)->GetGeneratorTargets(); + for (std::vector::const_iterator ti = targets.begin(); ti != targets.end(); ti++) { std::string targetName = (*ti)->GetName(); switch ((*ti)->GetType()) { @@ -359,8 +360,9 @@ void cmExtraCodeBlocksGenerator::CreateNewProjectFile( for (std::vector::const_iterator lg = lgs.begin(); lg != lgs.end(); lg++) { cmMakefile* makefile = (*lg)->GetMakefile(); - std::vector targets = (*lg)->GetGeneratorTargets(); - for (std::vector::iterator ti = targets.begin(); + const std::vector& targets = + (*lg)->GetGeneratorTargets(); + for (std::vector::const_iterator ti = targets.begin(); ti != targets.end(); ti++) { switch ((*ti)->GetType()) { case cmStateEnums::EXECUTABLE: diff --git a/Source/cmExtraCodeLiteGenerator.cxx b/Source/cmExtraCodeLiteGenerator.cxx index b478f34..96502d5 100644 --- a/Source/cmExtraCodeLiteGenerator.cxx +++ b/Source/cmExtraCodeLiteGenerator.cxx @@ -292,8 +292,9 @@ void cmExtraCodeLiteGenerator::CreateNewProjectFile( for (std::vector::const_iterator lg = lgs.begin(); lg != lgs.end(); lg++) { cmMakefile* makefile = (*lg)->GetMakefile(); - std::vector targets = (*lg)->GetGeneratorTargets(); - for (std::vector::iterator ti = targets.begin(); + const std::vector& targets = + (*lg)->GetGeneratorTargets(); + for (std::vector::const_iterator ti = targets.begin(); ti != targets.end(); ti++) { projectType = CollectSourceFiles(makefile, *ti, cFiles, otherFiles); } diff --git a/Source/cmExtraEclipseCDT4Generator.cxx b/Source/cmExtraEclipseCDT4Generator.cxx index 2a6ce98..473af37 100644 --- a/Source/cmExtraEclipseCDT4Generator.cxx +++ b/Source/cmExtraEclipseCDT4Generator.cxx @@ -475,7 +475,7 @@ void cmExtraEclipseCDT4Generator::CreateLinksForTargets(cmXMLWriter& xml) this->GlobalGenerator->GetLocalGenerators().begin(); lgIt != this->GlobalGenerator->GetLocalGenerators().end(); ++lgIt) { cmMakefile* makefile = (*lgIt)->GetMakefile(); - const std::vector targets = + const std::vector& targets = (*lgIt)->GetGeneratorTargets(); for (std::vector::const_iterator ti = targets.begin(); @@ -853,8 +853,9 @@ void cmExtraEclipseCDT4Generator::CreateCProjectFile() const for (std::vector::const_iterator it = this->GlobalGenerator->GetLocalGenerators().begin(); it != this->GlobalGenerator->GetLocalGenerators().end(); ++it) { - std::vector targets = (*it)->GetGeneratorTargets(); - for (std::vector::iterator l = targets.begin(); + const std::vector& targets = + (*it)->GetGeneratorTargets(); + for (std::vector::const_iterator l = targets.begin(); l != targets.end(); ++l) { std::vector includeDirs; std::string config = mf->GetSafeDefinition("CMAKE_BUILD_TYPE"); @@ -910,7 +911,7 @@ void cmExtraEclipseCDT4Generator::CreateCProjectFile() const for (std::vector::const_iterator it = this->GlobalGenerator->GetLocalGenerators().begin(); it != this->GlobalGenerator->GetLocalGenerators().end(); ++it) { - const std::vector targets = + const std::vector& targets = (*it)->GetGeneratorTargets(); std::string subdir = (*it)->ConvertToRelativePath( this->HomeOutputDirectory, (*it)->GetCurrentBinaryDirectory()); diff --git a/Source/cmExtraKateGenerator.cxx b/Source/cmExtraKateGenerator.cxx index 3730433..e366774 100644 --- a/Source/cmExtraKateGenerator.cxx +++ b/Source/cmExtraKateGenerator.cxx @@ -115,7 +115,7 @@ void cmExtraKateGenerator::WriteTargets(const cmLocalGenerator* lg, for (std::vector::const_iterator it = this->GlobalGenerator->GetLocalGenerators().begin(); it != this->GlobalGenerator->GetLocalGenerators().end(); ++it) { - const std::vector targets = + const std::vector& targets = (*it)->GetGeneratorTargets(); std::string currentDir = (*it)->GetCurrentBinaryDirectory(); bool topLevel = (currentDir == (*it)->GetBinaryDirectory()); diff --git a/Source/cmExtraSublimeTextGenerator.cxx b/Source/cmExtraSublimeTextGenerator.cxx index 1fd1418..a62a546 100644 --- a/Source/cmExtraSublimeTextGenerator.cxx +++ b/Source/cmExtraSublimeTextGenerator.cxx @@ -185,8 +185,9 @@ void cmExtraSublimeTextGenerator::AppendAllTargets( for (std::vector::const_iterator lg = lgs.begin(); lg != lgs.end(); lg++) { cmMakefile* makefile = (*lg)->GetMakefile(); - std::vector targets = (*lg)->GetGeneratorTargets(); - for (std::vector::iterator ti = targets.begin(); + const std::vector& targets = + (*lg)->GetGeneratorTargets(); + for (std::vector::const_iterator ti = targets.begin(); ti != targets.end(); ti++) { std::string targetName = (*ti)->GetName(); switch ((*ti)->GetType()) { diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx index 18d10c5..85ba5ee 100644 --- a/Source/cmGlobalGenerator.cxx +++ b/Source/cmGlobalGenerator.cxx @@ -2594,9 +2594,9 @@ void cmGlobalGenerator::GetTargetSets(TargetDependSet& projectTargets, continue; } // Get the targets in the makefile - std::vector tgts = (*i)->GetGeneratorTargets(); + const std::vector& tgts = (*i)->GetGeneratorTargets(); // loop over all the targets - for (std::vector::iterator l = tgts.begin(); + for (std::vector::const_iterator l = tgts.begin(); l != tgts.end(); ++l) { cmGeneratorTarget* target = *l; if (this->IsRootOnlyTarget(target) && @@ -2789,9 +2789,9 @@ void cmGlobalGenerator::WriteSummary() cmGeneratedFileStream fout(fname.c_str()); for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i) { - std::vector tgts = + const std::vector& tgts = this->LocalGenerators[i]->GetGeneratorTargets(); - for (std::vector::iterator it = tgts.begin(); + for (std::vector::const_iterator it = tgts.begin(); it != tgts.end(); ++it) { if ((*it)->GetType() == cmStateEnums::INTERFACE_LIBRARY) { continue; diff --git a/Source/cmGlobalKdevelopGenerator.cxx b/Source/cmGlobalKdevelopGenerator.cxx index e72c6e3..85a1417 100644 --- a/Source/cmGlobalKdevelopGenerator.cxx +++ b/Source/cmGlobalKdevelopGenerator.cxx @@ -128,8 +128,9 @@ bool cmGlobalKdevelopGenerator::CreateFilelistFile( } // get all sources - std::vector targets = (*it)->GetGeneratorTargets(); - for (std::vector::iterator ti = targets.begin(); + const std::vector& targets = + (*it)->GetGeneratorTargets(); + for (std::vector::const_iterator ti = targets.begin(); ti != targets.end(); ti++) { std::vector sources; cmGeneratorTarget* gt = *ti; diff --git a/Source/cmGlobalUnixMakefileGenerator3.cxx b/Source/cmGlobalUnixMakefileGenerator3.cxx index 23b3718..e42bf20 100644 --- a/Source/cmGlobalUnixMakefileGenerator3.cxx +++ b/Source/cmGlobalUnixMakefileGenerator3.cxx @@ -382,8 +382,8 @@ void cmGlobalUnixMakefileGenerator3::WriteMainCMakefileLanguageRules( for (unsigned int i = 0; i < lGenerators.size(); ++i) { lg = static_cast(lGenerators[i]); // for all of out targets - std::vector tgts = lg->GetGeneratorTargets(); - for (std::vector::iterator l = tgts.begin(); + const std::vector& tgts = lg->GetGeneratorTargets(); + for (std::vector::const_iterator l = tgts.begin(); l != tgts.end(); l++) { if (((*l)->GetType() == cmStateEnums::EXECUTABLE) || ((*l)->GetType() == cmStateEnums::STATIC_LIBRARY) || @@ -414,8 +414,8 @@ void cmGlobalUnixMakefileGenerator3::WriteDirectoryRule2( // The directory-level rule should depend on the target-level rules // for all targets in the directory. std::vector depends; - std::vector targets = lg->GetGeneratorTargets(); - for (std::vector::iterator l = targets.begin(); + const std::vector& targets = lg->GetGeneratorTargets(); + for (std::vector::const_iterator l = targets.begin(); l != targets.end(); ++l) { cmGeneratorTarget* gtarget = *l; int type = gtarget->GetType(); @@ -547,8 +547,8 @@ void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules( for (i = 0; i < this->LocalGenerators.size(); ++i) { lg = static_cast(this->LocalGenerators[i]); // for each target Generate the rule files for each target. - std::vector targets = lg->GetGeneratorTargets(); - for (std::vector::iterator t = targets.begin(); + const std::vector& targets = lg->GetGeneratorTargets(); + for (std::vector::const_iterator t = targets.begin(); t != targets.end(); ++t) { cmGeneratorTarget* gtarget = *t; // Don't emit the same rule twice (e.g. two targets with the same @@ -629,8 +629,8 @@ void cmGlobalUnixMakefileGenerator3::WriteConvenienceRules2( depends.push_back("cmake_check_build_system"); // for each target Generate the rule files for each target. - std::vector targets = lg->GetGeneratorTargets(); - for (std::vector::iterator t = targets.begin(); + const std::vector& targets = lg->GetGeneratorTargets(); + for (std::vector::const_iterator t = targets.begin(); t != targets.end(); ++t) { cmGeneratorTarget* gtarget = *t; int type = gtarget->GetType(); @@ -807,7 +807,7 @@ void cmGlobalUnixMakefileGenerator3::InitializeProgressMarks() this->LocalGenerators.begin(); lgi != this->LocalGenerators.end(); ++lgi) { cmLocalGenerator* lg = *lgi; - std::vector targets = lg->GetGeneratorTargets(); + const std::vector& targets = lg->GetGeneratorTargets(); for (std::vector::const_iterator t = targets.begin(); t != targets.end(); ++t) { cmGeneratorTarget* gt = *t; @@ -952,8 +952,9 @@ void cmGlobalUnixMakefileGenerator3::WriteHelpRule( // the targets if (lg2 == lg || lg->IsRootMakefile()) { // for each target Generate the rule files for each target. - std::vector targets = lg2->GetGeneratorTargets(); - for (std::vector::iterator t = targets.begin(); + const std::vector& targets = + lg2->GetGeneratorTargets(); + for (std::vector::const_iterator t = targets.begin(); t != targets.end(); ++t) { cmGeneratorTarget* target = *t; cmStateEnums::TargetType type = target->GetType(); diff --git a/Source/cmGraphVizWriter.cxx b/Source/cmGraphVizWriter.cxx index 7e953ce..015f887 100644 --- a/Source/cmGraphVizWriter.cxx +++ b/Source/cmGraphVizWriter.cxx @@ -420,7 +420,8 @@ int cmGraphVizWriter::CollectAllTargets() for (std::vector::const_iterator lit = this->LocalGenerators.begin(); lit != this->LocalGenerators.end(); ++lit) { - std::vector targets = (*lit)->GetGeneratorTargets(); + const std::vector& targets = + (*lit)->GetGeneratorTargets(); for (std::vector::const_iterator it = targets.begin(); it != targets.end(); ++it) { const char* realTargetName = (*it)->GetName().c_str(); @@ -445,7 +446,8 @@ int cmGraphVizWriter::CollectAllExternalLibs(int cnt) for (std::vector::const_iterator lit = this->LocalGenerators.begin(); lit != this->LocalGenerators.end(); ++lit) { - std::vector targets = (*lit)->GetGeneratorTargets(); + const std::vector& targets = + (*lit)->GetGeneratorTargets(); for (std::vector::const_iterator it = targets.begin(); it != targets.end(); ++it) { const char* realTargetName = (*it)->GetName().c_str(); diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index 2c5db10..2c8157e 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -214,8 +214,8 @@ void cmLocalGenerator::TraceDependencies() this->GlobalGenerator->CreateEvaluationSourceFiles(*ci); } // Generate the rule files for each target. - std::vector targets = this->GetGeneratorTargets(); - for (std::vector::iterator t = targets.begin(); + const std::vector& targets = this->GetGeneratorTargets(); + for (std::vector::const_iterator t = targets.begin(); t != targets.end(); ++t) { if ((*t)->GetType() == cmStateEnums::INTERFACE_LIBRARY) { continue; @@ -548,6 +548,8 @@ void cmLocalGenerator::GenerateInstallRules() void cmLocalGenerator::AddGeneratorTarget(cmGeneratorTarget* gt) { this->GeneratorTargets.push_back(gt); + this->GeneratorTargetSearchIndex.insert( + std::pair(gt->GetName(), gt)); this->GlobalGenerator->IndexGeneratorTarget(gt); } @@ -581,11 +583,10 @@ private: cmGeneratorTarget* cmLocalGenerator::FindLocalNonAliasGeneratorTarget( const std::string& name) const { - std::vector::const_iterator ti = - std::find_if(this->GeneratorTargets.begin(), this->GeneratorTargets.end(), - NamedGeneratorTargetFinder(name)); - if (ti != this->GeneratorTargets.end()) { - return *ti; + GeneratorTargetMap::const_iterator ti = + this->GeneratorTargetSearchIndex.find(name); + if (ti != this->GeneratorTargetSearchIndex.end()) { + return ti->second; } return CM_NULLPTR; } @@ -600,8 +601,8 @@ void cmLocalGenerator::ComputeTargetManifest() } // Add our targets to the manifest for each configuration. - std::vector targets = this->GetGeneratorTargets(); - for (std::vector::iterator t = targets.begin(); + const std::vector& targets = this->GetGeneratorTargets(); + for (std::vector::const_iterator t = targets.begin(); t != targets.end(); ++t) { cmGeneratorTarget* target = *t; if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) { @@ -625,8 +626,8 @@ bool cmLocalGenerator::ComputeTargetCompileFeatures() } // Process compile features of all targets. - std::vector targets = this->GetGeneratorTargets(); - for (std::vector::iterator t = targets.begin(); + const std::vector& targets = this->GetGeneratorTargets(); + for (std::vector::const_iterator t = targets.begin(); t != targets.end(); ++t) { cmGeneratorTarget* target = *t; for (std::vector::iterator ci = configNames.begin(); @@ -2121,8 +2122,8 @@ void cmLocalGenerator::GenerateTargetInstallRules( { // Convert the old-style install specification from each target to // an install generator and run it. - std::vector tgts = this->GetGeneratorTargets(); - for (std::vector::iterator l = tgts.begin(); + const std::vector& tgts = this->GetGeneratorTargets(); + for (std::vector::const_iterator l = tgts.begin(); l != tgts.end(); ++l) { if ((*l)->GetType() == cmStateEnums::INTERFACE_LIBRARY) { continue; diff --git a/Source/cmLocalGenerator.h b/Source/cmLocalGenerator.h index 9f78be4..9083c83 100644 --- a/Source/cmLocalGenerator.h +++ b/Source/cmLocalGenerator.h @@ -16,6 +16,7 @@ #include "cmOutputConverter.h" #include "cmPolicies.h" #include "cmStateSnapshot.h" +#include "cm_unordered_map.hxx" #include "cmake.h" class cmComputeLinkInformation; @@ -353,8 +354,11 @@ protected: std::string::size_type ObjectPathMax; std::set ObjectMaxPathViolations; - std::set WarnCMP0063; + typedef CM_UNORDERED_MAP GeneratorTargetMap; + GeneratorTargetMap GeneratorTargetSearchIndex; std::vector GeneratorTargets; + + std::set WarnCMP0063; std::vector ImportedGeneratorTargets; std::vector OwnedImportedGeneratorTargets; std::map AliasTargets; diff --git a/Source/cmLocalNinjaGenerator.cxx b/Source/cmLocalNinjaGenerator.cxx index 266710c..9fa3ca5 100644 --- a/Source/cmLocalNinjaGenerator.cxx +++ b/Source/cmLocalNinjaGenerator.cxx @@ -79,8 +79,8 @@ void cmLocalNinjaGenerator::Generate() } } - std::vector targets = this->GetGeneratorTargets(); - for (std::vector::iterator t = targets.begin(); + const std::vector& targets = this->GetGeneratorTargets(); + for (std::vector::const_iterator t = targets.begin(); t != targets.end(); ++t) { if ((*t)->GetType() == cmStateEnums::INTERFACE_LIBRARY) { continue; diff --git a/Source/cmLocalUnixMakefileGenerator3.cxx b/Source/cmLocalUnixMakefileGenerator3.cxx index 9b9d22c..2d10021 100644 --- a/Source/cmLocalUnixMakefileGenerator3.cxx +++ b/Source/cmLocalUnixMakefileGenerator3.cxx @@ -116,10 +116,10 @@ void cmLocalUnixMakefileGenerator3::Generate() this->Makefile->IsOn("CMAKE_SKIP_ASSEMBLY_SOURCE_RULES"); // Generate the rule files for each target. - std::vector targets = this->GetGeneratorTargets(); + const std::vector& targets = this->GetGeneratorTargets(); cmGlobalUnixMakefileGenerator3* gg = static_cast(this->GlobalGenerator); - for (std::vector::iterator t = targets.begin(); + for (std::vector::const_iterator t = targets.begin(); t != targets.end(); ++t) { if ((*t)->GetType() == cmStateEnums::INTERFACE_LIBRARY) { continue; @@ -172,8 +172,8 @@ void cmLocalUnixMakefileGenerator3::ComputeObjectFilenames( void cmLocalUnixMakefileGenerator3::GetLocalObjectFiles( std::map& localObjectFiles) { - std::vector targets = this->GetGeneratorTargets(); - for (std::vector::iterator ti = targets.begin(); + const std::vector& targets = this->GetGeneratorTargets(); + for (std::vector::const_iterator ti = targets.begin(); ti != targets.end(); ++ti) { cmGeneratorTarget* gt = *ti; if (gt->GetType() == cmStateEnums::INTERFACE_LIBRARY) { @@ -382,9 +382,9 @@ void cmLocalUnixMakefileGenerator3::WriteLocalMakefileTargets( // for each target we just provide a rule to cd up to the top and do a make // on the target - std::vector targets = this->GetGeneratorTargets(); + const std::vector& targets = this->GetGeneratorTargets(); std::string localName; - for (std::vector::iterator t = targets.begin(); + for (std::vector::const_iterator t = targets.begin(); t != targets.end(); ++t) { if (((*t)->GetType() == cmStateEnums::EXECUTABLE) || ((*t)->GetType() == cmStateEnums::STATIC_LIBRARY) || @@ -1562,8 +1562,8 @@ void cmLocalUnixMakefileGenerator3::WriteLocalAllRules( this->WriteDivider(ruleFileStream); ruleFileStream << "# Targets provided globally by CMake.\n" << "\n"; - std::vector targets = this->GetGeneratorTargets(); - std::vector::iterator glIt; + const std::vector& targets = this->GetGeneratorTargets(); + std::vector::const_iterator glIt; for (glIt = targets.begin(); glIt != targets.end(); ++glIt) { if ((*glIt)->GetType() == cmStateEnums::GLOBAL_TARGET) { std::string targetString = -- cgit v0.12