Skip to content

Commit bc40342

Browse files
author
David Ungar
committed
Use sets
1 parent e4fb791 commit bc40342

File tree

2 files changed

+57
-58
lines changed

2 files changed

+57
-58
lines changed

include/swift/Driver/Compilation.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ class Compilation {
113113
/// vector-like collection of jobs that the range scheme would run because
114114
/// there are no incremental supplementary outputs such as swiftdeps,
115115
/// swiftranges, compiledsource
116-
void update(ArrayRef<const Job *> depJobs, ArrayRef<const Job *> rangeJobs,
117-
ArrayRef<const Job *> lackingSuppJobs);
116+
void update(const CommandSet &depJobs, const CommandSet &rangeJobs,
117+
const CommandSet &lackingSuppJobs);
118118

119119
/// Write the information for the -compare-incremental-schemes[-path]
120120
/// options
@@ -505,9 +505,9 @@ class Compilation {
505505
/// How many .swift input files?
506506
unsigned countSwiftInputs() const;
507507

508-
void updateIncrementalComparison(ArrayRef<const Job *> depJobs,
509-
ArrayRef<const Job *> rangeJobs,
510-
ArrayRef<const Job *> lackingSuppJobs) {
508+
void updateIncrementalComparison(const CommandSet &depJobs,
509+
const CommandSet &rangeJobs,
510+
const CommandSet &lackingSuppJobs) {
511511
if (IncrementalComparator.hasValue())
512512
IncrementalComparator.getValue().update(depJobs, rangeJobs,
513513
lackingSuppJobs);

lib/Driver/Compilation.cpp

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -633,22 +633,22 @@ namespace driver {
633633
static_cast<const BatchJob *>(FinishedCmd));
634634
}
635635

636-
SmallVector<const Job *, 16> Dependents;
636+
CommandSet Dependents;
637637
if (!Comp.getUseSourceRangeDependencies()) {
638+
// Can just do the cheapest thing
638639
if (!Comp.IncrementalComparator)
639640
Dependents = subsequentJobsNeededForDeps(FinishedCmd, ReturnCode);
640641
else {
641-
SmallVector<const Job *, 16> DependentsForDeps;
642+
CommandSet DependentsForDeps;
642643
std::tie(Dependents, DependentsForDeps) =
643-
subsequentJobsNeededForRanges(FinishedCmd, ReturnCode);
644+
subsequentJobsNeededForDepsOrRanges(FinishedCmd, ReturnCode);
644645
Dependents = DependentsForDeps;
645646
}
646647
} else {
647-
SmallVector<const Job *, 16> DependentsForDeps;
648+
CommandSet DependentsForDeps;
648649
std::tie(Dependents, DependentsForDeps) =
649-
subsequentJobsNeededForRanges(FinishedCmd, ReturnCode);
650+
subsequentJobsNeededForDepsOrRanges(FinishedCmd, ReturnCode);
650651

651-
// Will reload again, sigh
652652
if (Comp.getShowIncrementalBuildDecisions() &&
653653
(!DependentsForDeps.empty() || !Dependents.empty())) {
654654
llvm::outs() << "After completion of " << LogJob(FinishedCmd)
@@ -742,39 +742,43 @@ namespace driver {
742742
///
743743
/// FIXME: too much global state floating around, e.g.
744744
/// getIncrementalBuildEnabled
745-
SmallVector<const Job *, 16>
746-
subsequentJobsNeededForDeps(const Job *FinishedCmd, const int ReturnCode) {
745+
CommandSet subsequentJobsNeededForDeps(const Job *FinishedCmd,
746+
const int ReturnCode) {
747747
if (!Comp.getIncrementalBuildEnabled())
748748
return {};
749749
SmallVector<const Job *, 16> Dependents;
750750
reloadAndRemarkDeps(FinishedCmd, ReturnCode, Dependents);
751-
return Dependents;
751+
CommandSet DepSet;
752+
for (const Job *Cmd : Dependents)
753+
DepSet.insert(Cmd);
754+
return DepSet;
752755
}
753756

754757
// Returns a pair of jobs needed when using ranges, and jobs needed
755758
// when using dependencies.
756-
std::pair<SmallVector<const Job *, 16>, SmallVector<const Job *, 16>>
757-
subsequentJobsNeededForRanges(const Job *FinishedCmd,
758-
const int ReturnCode) {
759+
std::pair<CommandSet, CommandSet>
760+
subsequentJobsNeededForDepsOrRanges(const Job *FinishedCmd,
761+
const int ReturnCode) {
759762

760763
if (!Comp.getIncrementalBuildEnabled())
761764
return {};
762765
// FIXME: crude, could just use dependencies to schedule only those jobs
763766
// depending on the added tops
764767
const size_t topsBefore = countTopLevelProvides(FinishedCmd);
765768

766-
SmallVector<const Job *, 16> jobsForDependencies;
767-
reloadAndRemarkDeps(FinishedCmd, ReturnCode, jobsForDependencies);
769+
CommandSet jobsForDependencies =
770+
subsequentJobsNeededForDeps(FinishedCmd, ReturnCode);
768771

769772
const size_t topsAfter = countTopLevelProvides(FinishedCmd);
770-
const bool fallBack = topsAfter > topsBefore;
773+
// TODO: see if new type was added outside of a struct (etc) body
774+
const bool userAddedTopLevel = topsAfter > topsBefore;
771775

772776
// TODO: instead of scheduling *all* the jobs, could figure out
773777
// which ones use the introduced top-level names and only schedule those.
774778
// However, as it stands, the caller will fall back to the old
775779
// dependency scheme in this case anyway.
776-
SmallVector<const Job *, 16> jobsForRanges =
777-
fallBack ? jobsForDependencies : SmallVector<const Job *, 16>();
780+
CommandSet jobsForRanges =
781+
userAddedTopLevel ? jobsForDependencies : CommandSet();
778782

779783
Comp.updateIncrementalComparison(jobsForDependencies, jobsForRanges, {});
780784

@@ -880,10 +884,9 @@ namespace driver {
880884
void scheduleFirstRoundJobsForIncrementalCompilation(
881885
DependencyGraphT &DepGraph) {
882886

883-
CommandSet compileJobsToSchedule;
884-
for (const Job *Cmd :
885-
computeFirstRoundCompileJobsForIncrementalCompilation(DepGraph))
886-
compileJobsToSchedule.insert(Cmd);
887+
CommandSet compileJobsToSchedule =
888+
computeFirstRoundCompileJobsForIncrementalCompilation(DepGraph);
889+
887890
for (const Job *Cmd : Comp.getJobs()) {
888891
if (Cmd->getFirstSwiftPrimaryInput().empty() ||
889892
compileJobsToSchedule.count(Cmd))
@@ -896,8 +899,7 @@ namespace driver {
896899
/// Figure out the best strategy and return those jobs. May return
897900
/// duplicates.
898901
template <typename DependencyGraphT>
899-
llvm::SmallVector<const Job *, 16>
900-
computeFirstRoundCompileJobsForIncrementalCompilation(
902+
CommandSet computeFirstRoundCompileJobsForIncrementalCompilation(
901903
DependencyGraphT &DepGraph) {
902904
auto compileJobsToScheduleViaDependencies =
903905
computeDependenciesAndGetNeededCompileJobs(DepGraph);
@@ -909,10 +911,8 @@ namespace driver {
909911
return compileJobsToScheduleViaDependencies;
910912

911913
auto jobs = computeRangesAndGetNeededCompileJobs(DepGraph);
912-
llvm::SmallVector<const Job *, 16> &compileJobsToScheduleViaSourceRanges =
913-
jobs.first;
914-
llvm::SmallVector<const Job *, 16>
915-
&jobsLackingSourceRangeSupplementaryOutputs = jobs.second;
914+
CommandSet &compileJobsToScheduleViaSourceRanges = jobs.first;
915+
CommandSet &jobsLackingSourceRangeSupplementaryOutputs = jobs.second;
916916

917917
const bool shouldFallBack =
918918
decideAndExplainWhetherToFallBackToDependencies(
@@ -937,21 +937,20 @@ namespace driver {
937937
// Even if dependencies would not schedule these, we want them to run
938938
// to create the supplementary outputs for next time.
939939
for (const Job *Cmd : jobsLackingSourceRangeSupplementaryOutputs)
940-
compileJobsToScheduleWhenFallingBack.push_back(Cmd);
940+
compileJobsToScheduleWhenFallingBack.insert(Cmd);
941941

942942
return compileJobsToScheduleWhenFallingBack;
943943
}
944944

945945
bool decideAndExplainWhetherToFallBackToDependencies(
946-
const ArrayRef<const Job *> compileJobsToScheduleViaSourceRanges,
947-
const ArrayRef<const Job *>
948-
&jobsLackingSourceRangeSupplementaryOutputs) {
946+
const CommandSet &compileJobsToScheduleViaSourceRanges,
947+
const CommandSet &jobsLackingSourceRangeSupplementaryOutputs) {
949948
if (!jobsLackingSourceRangeSupplementaryOutputs.empty()) {
950949
if (Comp.getShowIncrementalBuildDecisions()) {
951950
llvm::outs()
952951
<< "Using dependencies: At least one input ('"
953952
<< llvm::sys::path::filename(
954-
jobsLackingSourceRangeSupplementaryOutputs.front()
953+
(*jobsLackingSourceRangeSupplementaryOutputs.begin())
955954
->getFirstSwiftPrimaryInput())
956955
<< "') lacks a supplementary output needed for the source "
957956
"range strategy.\n Maybe dependencies can do better than "
@@ -961,10 +960,8 @@ namespace driver {
961960
}
962961
// Unless the source-range scheme would compile every file,
963962
// it's likely a better bet.
964-
CommandSet uniqueJobs;
965-
for (const auto *J : compileJobsToScheduleViaSourceRanges)
966-
uniqueJobs.insert(J);
967-
if (uniqueJobs.size() < Comp.countSwiftInputs()) {
963+
if (compileJobsToScheduleViaSourceRanges.size() <
964+
Comp.countSwiftInputs()) {
968965
if (Comp.getShowIncrementalBuildDecisions())
969966
llvm::outs() << "Using ranges\n";
970967
return false;
@@ -980,8 +977,7 @@ namespace driver {
980977
/// must be compiled to use ranges in the future (because they were lacking
981978
/// supplementary output files). May include duplicates.
982979
template <typename DependencyGraphT>
983-
std::pair<llvm::SmallVector<const Job *, 16>,
984-
llvm::SmallVector<const Job *, 16>>
980+
std::pair<CommandSet, CommandSet>
985981
computeRangesAndGetNeededCompileJobs(DependencyGraphT &DepGraph) {
986982
using namespace incremental_ranges;
987983

@@ -1001,16 +997,16 @@ namespace driver {
1001997
// primaries, could just keep on.
1002998
// load dependencies for external dependencies and interfacehashes
1003999

1004-
llvm::SmallVector<const Job *, 16> neededJobs;
1000+
CommandSet neededJobs;
10051001
for (const Job *Cmd : Comp.getJobs()) {
10061002
if (SourceRangeBasedInfo::shouldScheduleCompileJob(
10071003
allSourceRangeInfo, Cmd, [&](const bool willBuild, Twine why) {
10081004
noteBuilding(Cmd, willBuild, true, why.str());
10091005
}))
1010-
neededJobs.push_back(Cmd);
1006+
neededJobs.insert(Cmd);
10111007
}
10121008

1013-
llvm::SmallVector<const Job *, 16> jobsLackingSupplementaryOutputs;
1009+
CommandSet jobsLackingSupplementaryOutputs;
10141010
for (const Job *Cmd : Comp.getJobs()) {
10151011
auto pri = Cmd->getFirstSwiftPrimaryInput();
10161012
if (pri.empty())
@@ -1024,21 +1020,23 @@ namespace driver {
10241020
noteBuilding(Cmd, true, true,
10251021
"to create source-range and compiled-source files for the "
10261022
"next time when falling back from source-ranges");
1027-
jobsLackingSupplementaryOutputs.push_back(Cmd);
1023+
jobsLackingSupplementaryOutputs.insert(Cmd);
10281024
}
10291025

10301026
for (const Job *Cmd :
10311027
externallyDependentJobsForRangeBasedIncrementalCompilation(DepGraph))
1032-
neededJobs.push_back(Cmd);
1028+
neededJobs.insert(Cmd);
10331029

1030+
assert(neededJobs.size() <= Comp.countSwiftInputs());
1031+
assert(jobsLackingSupplementaryOutputs.size() <= Comp.countSwiftInputs());
10341032
return {neededJobs, jobsLackingSupplementaryOutputs};
10351033
}
10361034

10371035
/// Return jobs to run if using dependencies, may include duplicates.
10381036
template <typename DependencyGraphT>
1039-
llvm::SmallVector<const Job *, 16>
1037+
CommandSet
10401038
computeDependenciesAndGetNeededCompileJobs(DependencyGraphT &DepGraph) {
1041-
llvm::SmallVector<const Job *, 16> jobsToSchedule;
1039+
CommandSet jobsToSchedule;
10421040
for (const Job *Cmd : Comp.getJobs()) {
10431041
if (Cmd->getFirstSwiftPrimaryInput().empty())
10441042
continue; // not Compile
@@ -1049,19 +1047,19 @@ namespace driver {
10491047
// Dependency load error, just run them all
10501048
for (const Job *Cmd : Comp.getJobs()) {
10511049
if (!Cmd->getFirstSwiftPrimaryInput().empty())
1052-
jobsToSchedule.push_back(Cmd);
1050+
jobsToSchedule.insert(Cmd);
10531051
}
10541052
return jobsToSchedule;
10551053
}
10561054
if (shouldSched.getValue())
1057-
jobsToSchedule.push_back(Cmd);
1055+
jobsToSchedule.insert(Cmd);
10581056
}
10591057
{
10601058
const auto additionalJobs =
10611059
additionalJobsToScheduleForDependencyBasedIncrementalCompilation(
10621060
DepGraph);
10631061
for (const auto *Cmd : additionalJobs)
1064-
jobsToSchedule.push_back(Cmd);
1062+
jobsToSchedule.insert(Cmd);
10651063
}
10661064
return jobsToSchedule;
10671065
}
@@ -1165,7 +1163,7 @@ namespace driver {
11651163
SmallVector<const Job *, 16>
11661164
additionalJobsToScheduleForDependencyBasedIncrementalCompilation(
11671165
DependencyGraphT &DepGraph) {
1168-
SmallVector<const Job *, 16> AdditionalOutOfDateCommands =
1166+
auto AdditionalOutOfDateCommands =
11691167
collectSecondaryJobsFromDependencyGraph(DepGraph);
11701168

11711169
size_t firstSize = AdditionalOutOfDateCommands.size();
@@ -1958,8 +1956,9 @@ const char *Compilation::getAllSourcesPath() const {
19581956
}
19591957

19601958
void Compilation::IncrementalSchemeComparator::update(
1961-
const ArrayRef<const Job *> depJobs, const ArrayRef<const Job *> rangeJobs,
1962-
const ArrayRef<const Job *> lackingSuppJobs) {
1959+
const CommandSet &depJobs,
1960+
const CommandSet &rangeJobs,
1961+
const CommandSet &lackingSuppJobs) {
19631962
for (const auto *cmd : depJobs)
19641963
DependencyCompileJobs.insert(cmd);
19651964
for (const auto *cmd : rangeJobs)
@@ -1995,9 +1994,9 @@ void Compilation::IncrementalSchemeComparator::outputComparison(
19951994
<< "supplementary output creation: " << SourceRangeLackingSuppJobs.size()
19961995
<< ", "
19971996
<< "total: " << SwiftInputCount << ", "
1998-
<< "scheme requested: "
1997+
<< "requested: "
19991998
<< (EnableSourceRangeDependencies ? "ranges" : "deps") << ", "
2000-
<< "scheme used: " << (UseSourceRangeDependencies ? "ranges" : "deps")
1999+
<< "using: " << (UseSourceRangeDependencies ? "ranges" : "deps")
20012000
<< "\n";
20022001
}
20032002

0 commit comments

Comments
 (0)