From 426f3295f61eaf2feb58c7e4b9ebe838b21cf453 Mon Sep 17 00:00:00 2001 From: Nicolas van Kempen Date: Thu, 23 Mar 2023 04:02:50 -0400 Subject: Ninja: Use more efficient data structures to collect outputs --- Source/cmGlobalNinjaGenerator.cxx | 15 +++------------ Source/cmGlobalNinjaGenerator.h | 12 ++++-------- Source/cmLocalNinjaGenerator.cxx | 26 ++++++++++++++------------ Source/cmNinjaTypes.h | 3 +-- 4 files changed, 22 insertions(+), 34 deletions(-) diff --git a/Source/cmGlobalNinjaGenerator.cxx b/Source/cmGlobalNinjaGenerator.cxx index 37856d9..75ed693 100644 --- a/Source/cmGlobalNinjaGenerator.cxx +++ b/Source/cmGlobalNinjaGenerator.cxx @@ -1368,17 +1368,7 @@ void cmGlobalNinjaGenerator::AppendTargetDepends( } void cmGlobalNinjaGenerator::AppendTargetDependsClosure( - cmGeneratorTarget const* target, cmNinjaDeps& outputs, - const std::string& config, const std::string& fileConfig, bool genexOutput) -{ - cmNinjaOuts outs; - this->AppendTargetDependsClosure(target, outs, config, fileConfig, - genexOutput, true); - cm::append(outputs, outs); -} - -void cmGlobalNinjaGenerator::AppendTargetDependsClosure( - cmGeneratorTarget const* target, cmNinjaOuts& outputs, + cmGeneratorTarget const* target, std::unordered_set& outputs, const std::string& config, const std::string& fileConfig, bool genexOutput, bool omit_self) { @@ -1399,7 +1389,8 @@ void cmGlobalNinjaGenerator::AppendTargetDependsClosure( // relevant for filling the cache entries properly isolated and a global // result set that is relevant for the result of the top level call to // AppendTargetDependsClosure. - cmNinjaOuts this_outs; // this will be the new cache entry + std::unordered_set + this_outs; // this will be the new cache entry for (auto const& dep_target : this->GetTargetDirectDepends(target)) { if (!dep_target->IsInBuildSystem()) { diff --git a/Source/cmGlobalNinjaGenerator.h b/Source/cmGlobalNinjaGenerator.h index 12c6698..0af8cde 100644 --- a/Source/cmGlobalNinjaGenerator.h +++ b/Source/cmGlobalNinjaGenerator.h @@ -351,15 +351,10 @@ public: const std::string& fileConfig, cmNinjaTargetDepends depends); void AppendTargetDependsClosure(cmGeneratorTarget const* target, - cmNinjaDeps& outputs, + std::unordered_set& outputs, const std::string& config, const std::string& fileConfig, - bool genexOutput); - void AppendTargetDependsClosure(cmGeneratorTarget const* target, - cmNinjaOuts& outputs, - const std::string& config, - const std::string& fileConfig, - bool genexOutput, bool omit_self); + bool genexOutput, bool omit_self = true); void AppendDirectoryForConfig(const std::string& prefix, const std::string& config, @@ -617,7 +612,8 @@ private: bool GenexOutput; }; - std::map TargetDependsClosures; + std::map> + TargetDependsClosures; TargetAliasMap TargetAliases; diff --git a/Source/cmLocalNinjaGenerator.cxx b/Source/cmLocalNinjaGenerator.cxx index 305dab3..d0bd375 100644 --- a/Source/cmLocalNinjaGenerator.cxx +++ b/Source/cmLocalNinjaGenerator.cxx @@ -5,11 +5,11 @@ #include #include #include -#include #include #include #include +#include #include #include "cmsys/FStream.hxx" @@ -26,6 +26,7 @@ #include "cmMakefile.h" #include "cmMessageType.h" #include "cmNinjaTargetGenerator.h" +#include "cmNinjaTypes.h" #include "cmPolicies.h" #include "cmRulePlaceholderExpander.h" #include "cmSourceFile.h" @@ -601,7 +602,7 @@ void cmLocalNinjaGenerator::WriteCustomCommandBuildStatement( continue; } - cmNinjaDeps orderOnlyDeps; + std::unordered_set orderOnlyDeps; if (!cc->GetDependsExplicitOnly()) { // A custom command may appear on multiple targets. However, some build @@ -617,19 +618,15 @@ void cmLocalNinjaGenerator::WriteCustomCommandBuildStatement( assert(j != targets.end()); this->GetGlobalNinjaGenerator()->AppendTargetDependsClosure( *j, orderOnlyDeps, ccg.GetOutputConfig(), fileConfig, ccgs.size() > 1); - std::sort(orderOnlyDeps.begin(), orderOnlyDeps.end()); ++j; for (; j != targets.end(); ++j) { - std::vector jDeps; - std::vector depsIntersection; + std::unordered_set jDeps; this->GetGlobalNinjaGenerator()->AppendTargetDependsClosure( *j, jDeps, ccg.GetOutputConfig(), fileConfig, ccgs.size() > 1); - std::sort(jDeps.begin(), jDeps.end()); - std::set_intersection(orderOnlyDeps.begin(), orderOnlyDeps.end(), - jDeps.begin(), jDeps.end(), - std::back_inserter(depsIntersection)); - orderOnlyDeps = depsIntersection; + cm::erase_if(orderOnlyDeps, [&jDeps](std::string const& dep) { + return jDeps.find(dep) == jDeps.end(); + }); } } @@ -658,13 +655,17 @@ void cmLocalNinjaGenerator::WriteCustomCommandBuildStatement( std::vector cmdLines; this->AppendCustomCommandLines(ccg, cmdLines); + cmNinjaDeps sortedOrderOnlyDeps(orderOnlyDeps.begin(), + orderOnlyDeps.end()); + std::sort(sortedOrderOnlyDeps.begin(), sortedOrderOnlyDeps.end()); + if (cmdLines.empty()) { cmNinjaBuild build("phony"); build.Comment = cmStrCat("Phony custom command for ", mainOutput); build.Outputs = std::move(ccOutputs.ExplicitOuts); build.WorkDirOuts = std::move(ccOutputs.WorkDirOuts); build.ExplicitDeps = std::move(ninjaDeps); - build.OrderOnlyDeps = orderOnlyDeps; + build.OrderOnlyDeps = std::move(sortedOrderOnlyDeps); gg->WriteBuild(this->GetImplFileStream(fileConfig), build); } else { std::string customStep = cmSystemTools::GetFilenameName(mainOutput); @@ -710,7 +711,8 @@ void cmLocalNinjaGenerator::WriteCustomCommandBuildStatement( this->ConstructComment(ccg), comment, depfile, cc->GetJobPool(), cc->GetUsesTerminal(), /*restat*/ !symbolic || !byproducts.empty(), fileConfig, - std::move(ccOutputs), std::move(ninjaDeps), std::move(orderOnlyDeps)); + std::move(ccOutputs), std::move(ninjaDeps), + std::move(sortedOrderOnlyDeps)); } } } diff --git a/Source/cmNinjaTypes.h b/Source/cmNinjaTypes.h index c8a411e..b77e0b5 100644 --- a/Source/cmNinjaTypes.h +++ b/Source/cmNinjaTypes.h @@ -5,8 +5,8 @@ #include "cmConfigure.h" // IWYU pragma: keep #include -#include #include +#include #include #include @@ -17,7 +17,6 @@ enum cmNinjaTargetDepends }; using cmNinjaDeps = std::vector; -using cmNinjaOuts = std::set; using cmNinjaVars = std::map; class cmNinjaRule -- cgit v0.12