Skip to content

Commit 5dabb21

Browse files
author
David Ungar
committed
Add comments and direct/indirect naming
1 parent 53f09ee commit 5dabb21

File tree

4 files changed

+56
-38
lines changed

4 files changed

+56
-38
lines changed

Sources/SwiftDriver/IncrementalCompilation/InitialStateComputer.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,11 @@ extension IncrementalCompilationState.InitialStateComputer {
152152

153153
// Any externals not already in graph must be additions which should trigger
154154
// recompilation. Thus, `ChangedOrAdded`.
155-
let nodesInvalidatedByExternals = graph.collectNodesInvalidatedByChangedOrAddedExternals()
156-
let inputsInvalidatedByExternals = graph.collectInputsUsingTransitivelyInvalidated(nodes: nodesInvalidatedByExternals)
157-
return (graph, inputsInvalidatedByExternals)
155+
let nodesDirectlyInvalidatedByExternals = graph.collectNodesDirectlyInvalidatedByChangedOrAddedExternals()
156+
// Wait till the last minute to do the transitive closure as an optimization.
157+
let inputsTransitivelyInvalidatedByExternals = graph.collectInputsUsingTransitivelyInvalidated(
158+
nodes: nodesDirectlyInvalidatedByExternals)
159+
return (graph, inputsTransitivelyInvalidatedByExternals)
158160
}
159161

160162
/// Builds a graph

Sources/SwiftDriver/IncrementalCompilation/ModuleDependencyGraph.swift

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ extension ModuleDependencyGraph {
106106

107107
// MARK: - Getting a graph read from priors ready to use
108108
extension ModuleDependencyGraph {
109-
func collectNodesInvalidatedByChangedOrAddedExternals() -> Set<Node> {
109+
func collectNodesDirectlyInvalidatedByChangedOrAddedExternals() -> Set<Node> {
110110
fingerprintedExternalDependencies.reduce(into: Set()) { invalidatedNodes, fed in
111111
invalidatedNodes.formUnion (
112-
self.collectNodesInvalidatedByProcessing(fingerprintedExternalDependency: fed))
112+
self.collectNodesDirectlyInvalidatedByProcessing(fingerprintedExternalDependency: fed))
113113
}
114114
}
115115
}
@@ -252,18 +252,18 @@ extension ModuleDependencyGraph {
252252
/// If reading for the first time, the driver is compiling all outdated source files anyway, so only
253253
/// nodes invalidated by external dependencies matter.
254254
/// But when updating, all invalidations matter.
255-
let invalidatedNodes = phase.isUpdating
256-
? results.allInvalidatedNodes
257-
: results.nodesInvalidatedByUsingSomeExternal
255+
let directlyInvalidatedNodes = phase.isUpdating
256+
? results.allDirectlyInvalidatedNodes
257+
: results.nodesDirectlyInvalidatedByUsingSomeExternal
258258

259-
return collectInputsUsingTransitivelyInvalidated(nodes: invalidatedNodes)
259+
return collectInputsUsingTransitivelyInvalidated(nodes: directlyInvalidatedNodes)
260260
}
261261

262262
/// Given nodes that are invalidated, find all the affected inputs that must be recompiled.
263263
func collectInputsUsingTransitivelyInvalidated(
264-
nodes invalidatedNodes: Set<Node>
264+
nodes directlyInvalidatedNodes: Set<Node>
265265
) -> Set<TypedVirtualPath> {
266-
collectSwiftDepsUsingTransitivelyInvalidated(nodes: invalidatedNodes)
266+
collectSwiftDepsUsingTransitivelyInvalidated(nodes: directlyInvalidatedNodes)
267267
.reduce(into: Set()) { invalidatedInputs, invalidatedSwiftDeps in
268268
invalidatedInputs.insert(getInput(for: invalidatedSwiftDeps))
269269
}
@@ -276,7 +276,8 @@ extension ModuleDependencyGraph {
276276
/// Process a possibly-fingerprinted external dependency by reading and integrating, if applicable.
277277
/// Return the nodes thus invalidated.
278278
/// But always integrate, in order to detect future changes.
279-
func collectNodesInvalidatedByProcessing(
279+
/// This function does not to the transitive closure; that is left to the callers
280+
func collectNodesDirectlyInvalidatedByProcessing(
280281
fingerprintedExternalDependency fed: FingerprintedExternalDependency)
281282
-> Set<Node> {
282283

@@ -291,16 +292,28 @@ extension ModuleDependencyGraph {
291292
let shouldTryToProcess = info.isCrossModuleIncrementalBuildEnabled &&
292293
(isNewToTheGraph || lazyModTimer.hasExternalFileChanged)
293294

294-
let invalidatedNodesFromIncrementalExternal = shouldTryToProcess
295-
? collectNodesInvalidatedByAttemptingToProcess(fed, info)
295+
// Do this no matter what in order to integrate any incremental external dependencies.
296+
let directlyInvalidatedNodesFromIncrementalExternal = shouldTryToProcess
297+
? collectNodesDirectlyInvalidatedByAttemptingToProcess(fed, info)
296298
: nil
297299

298-
let callerWantsTheseChanges = (phase.isUpdating && isNewToTheGraph) ||
300+
if phase == .buildingAfterEachCompilation {
301+
// going to compile every input anyway, less work for callers
302+
return Set()
303+
}
304+
305+
/// When building a graph from scratch, an unchanged but new-to-the-graph external dependendcy should be ignored.
306+
/// Otherwise, it represents an added Import
307+
let callerWantsTheseChanges = (phase != .buildingWithoutAPrior && isNewToTheGraph) ||
299308
lazyModTimer.hasExternalFileChanged
300309

301-
return !callerWantsTheseChanges
302-
? Set<Node>()
303-
: invalidatedNodesFromIncrementalExternal ?? collectUntracedNodesDirectlyUsing(fed)
310+
guard callerWantsTheseChanges else {
311+
return Set()
312+
}
313+
314+
// If there was an error integrating the external dependency, or if it was not an incremental one,
315+
// return anything that uses that dependency.
316+
return directlyInvalidatedNodesFromIncrementalExternal ?? collectUntracedNodesDirectlyUsing(fed)
304317
}
305318

306319
private struct LazyModTimer {
@@ -311,15 +324,17 @@ extension ModuleDependencyGraph {
311324
>= info.buildTime
312325
}
313326

314-
private func collectNodesInvalidatedByAttemptingToProcess(
327+
/// Try to read and integrate an external dependency.
328+
/// Return nil if it's not incremental, or if an error occurs.
329+
private func collectNodesDirectlyInvalidatedByAttemptingToProcess(
315330
_ fed: FingerprintedExternalDependency,
316331
_ info: IncrementalCompilationState.InitialStateComputer) -> Set<Node>? {
317332
fed.incrementalDependencySource?
318333
.read(in: info.fileSystem, reporter: info.reporter)
319334
.map { unserializedDepGraph in
320335
info.reporter?.report("Integrating changes from: \(fed.externalDependency)")
321336
return Integrator.integrate(from: unserializedDepGraph, into: self)
322-
.allInvalidatedNodes
337+
.allDirectlyInvalidatedNodes
323338
}
324339
}
325340

Sources/SwiftDriver/IncrementalCompilation/ModuleDependencyGraphParts/Integrator.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ extension ModuleDependencyGraph.Integrator {
7575
private mutating func integrate() {
7676
integrateEachSourceNode()
7777
handleDisappearedNodes()
78-
destination.ensureGraphWillRetrace(results.allInvalidatedNodes)
78+
// Ensure transitive closure will get started.
79+
destination.ensureGraphWillRetrace(results.allDirectlyInvalidatedNodes)
7980
}
8081
private mutating func integrateEachSourceNode() {
8182
sourceGraph.forEachNode { integrate(oneNode: $0) }
@@ -196,9 +197,9 @@ extension ModuleDependencyGraph.Integrator {
196197
externalDependency fingerprintedExternalDependency: FingerprintedExternalDependency,
197198
moduleFileGraphUseNode moduleUseNode: Graph.Node) {
198199

199-
let invalidated = destination.collectNodesInvalidatedByProcessing(
200+
let invalidated = destination.collectNodesDirectlyInvalidatedByProcessing(
200201
fingerprintedExternalDependency: fingerprintedExternalDependency)
201-
results.addNodesInvalidatedByUsingSomeExternal(invalidated)
202+
results.addNodesDirectlyInvalidatedByUsingSomeExternal(invalidated)
202203
}
203204
}
204205

@@ -208,25 +209,25 @@ extension ModuleDependencyGraph.Integrator {
208209
public struct Results {
209210
typealias Node = ModuleDependencyGraph.Node
210211

211-
private(set) var allInvalidatedNodes = Set<Node>()
212-
private(set) var nodesInvalidatedByUsingSomeExternal = Set<Node>()
212+
private(set) var allDirectlyInvalidatedNodes = Set<Node>()
213+
private(set) var nodesDirectlyInvalidatedByUsingSomeExternal = Set<Node>()
213214

214-
mutating func addNodesInvalidatedByUsingSomeExternal(_ invalidated: Set<Node>)
215+
mutating func addNodesDirectlyInvalidatedByUsingSomeExternal(_ invalidated: Set<Node>)
215216
{
216-
allInvalidatedNodes.formUnion(invalidated)
217-
nodesInvalidatedByUsingSomeExternal.formUnion(invalidated)
217+
allDirectlyInvalidatedNodes.formUnion(invalidated)
218+
allDirectlyInvalidatedNodes.formUnion(invalidated)
218219
}
219220
mutating func addDisappeared(_ node: Node) {
220-
allInvalidatedNodes.insert(node)
221+
allDirectlyInvalidatedNodes.insert(node)
221222
}
222223
mutating func addChanged(_ node: Node) {
223-
allInvalidatedNodes.insert(node)
224+
allDirectlyInvalidatedNodes.insert(node)
224225
}
225226
mutating func addPatriated(_ node: Node) {
226-
allInvalidatedNodes.insert(node)
227+
allDirectlyInvalidatedNodes.insert(node)
227228
}
228229
mutating func addNew(_ node: Node) {
229-
allInvalidatedNodes.insert(node)
230+
allDirectlyInvalidatedNodes.insert(node)
230231
}
231232
}
232233
}

Tests/SwiftDriverTests/ModuleDependencyGraphTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ class ModuleDependencyGraphTests: XCTestCase {
752752

753753
func testLoadPassesWithFingerprint() {
754754
let graph = ModuleDependencyGraph(mock: de)
755-
_ = graph.getInvalidatedNodesForSimulatedLoad(
755+
_ = graph.getDirectlyInvalidatedNodesForSimulatedLoad(
756756
0,
757757
[MockDependencyKind.nominal: ["A@1"]])
758758
}
@@ -952,7 +952,7 @@ extension ModuleDependencyGraph {
952952
includePrivateDeps: Bool = true,
953953
hadCompilationError: Bool = false)
954954
{
955-
_ = getInvalidatedNodesForSimulatedLoad(
955+
_ = getDirectlyInvalidatedNodesForSimulatedLoad(
956956
swiftDepsIndex, dependencyDescriptions,
957957
interfaceHash,
958958
includePrivateDeps: includePrivateDeps,
@@ -966,19 +966,19 @@ extension ModuleDependencyGraph {
966966
hadCompilationError: Bool = false)
967967
-> [Int]
968968
{
969-
let invalidatedNodes = getInvalidatedNodesForSimulatedLoad(
969+
let directlyIinvalidatedNodes = getDirectlyInvalidatedNodesForSimulatedLoad(
970970
swiftDepsIndex,
971971
dependencyDescriptions,
972972
interfaceHash,
973973
includePrivateDeps: includePrivateDeps,
974974
hadCompilationError: hadCompilationError)
975975

976-
return collectSwiftDepsUsingTransitivelyInvalidated(nodes: invalidatedNodes)
976+
return collectSwiftDepsUsingTransitivelyInvalidated(nodes: directlyIinvalidatedNodes)
977977
.map { $0.mockID }
978978
}
979979

980980

981-
func getInvalidatedNodesForSimulatedLoad(
981+
func getDirectlyInvalidatedNodesForSimulatedLoad(
982982
_ swiftDepsIndex: Int,
983983
_ dependencyDescriptions: [MockDependencyKind: [String]],
984984
_ interfaceHashIfPresent: String? = nil,
@@ -1001,7 +1001,7 @@ extension ModuleDependencyGraph {
10011001

10021002
let results = Integrator.integrate(from: sfdg, into: self)
10031003

1004-
return results.allInvalidatedNodes
1004+
return results.allDirectlyInvalidatedNodes
10051005
}
10061006

10071007
func findUntracedSwiftDepsDependent(onExternal s: String) -> [Int] {

0 commit comments

Comments
 (0)