Skip to content

Commit 7ef3480

Browse files
author
David Ungar
committed
Try a cache for external dependency mod times
1 parent 4408456 commit 7ef3480

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

Sources/SwiftDriver/IncrementalCompilation/ModuleDependencyGraph.swift

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ import SwiftOptions
3737

3838
@_spi(Testing) public var phase: Phase
3939

40+
/// Minimize the number of file system modification-time queries.
41+
private var externalDependencyModTimeCache = [ExternalDependency: Bool]()
42+
4043
public init(_ info: IncrementalCompilationState.InitialStateComputer,
4144
_ phase: Phase
4245
) {
@@ -287,14 +290,10 @@ extension ModuleDependencyGraph {
287290

288291
let isNewToTheGraph = fingerprintedExternalDependencies.insert(fed).inserted
289292

290-
var lazyModTimer = LazyModTimer(
291-
externalDependency: fed.externalDependency,
292-
info: info)
293-
294293
// If the graph already includes prior externals, then any new externals are changes
295294
// Short-circuit conjunction may avoid the modTime query
296295
let shouldTryToProcess = info.isCrossModuleIncrementalBuildEnabled &&
297-
(isNewToTheGraph || lazyModTimer.hasExternalFileChanged)
296+
(isNewToTheGraph || hasFileChanged(of: fed.externalDependency))
298297

299298
// Do this no matter what in order to integrate any incremental external dependencies.
300299
let invalidatedNodesFromIncrementalExternal = shouldTryToProcess
@@ -309,7 +308,7 @@ extension ModuleDependencyGraph {
309308
/// When building a graph from scratch, an unchanged but new-to-the-graph external dependendcy should be ignored.
310309
/// Otherwise, it represents an added Import
311310
let callerWantsTheseChanges = (phase.isUpdating && isNewToTheGraph) ||
312-
lazyModTimer.hasExternalFileChanged
311+
hasFileChanged(of: fed.externalDependency)
313312

314313
guard callerWantsTheseChanges else {
315314
return DirectlyInvalidatedNodeSet()
@@ -319,7 +318,18 @@ extension ModuleDependencyGraph {
319318
// return anything that uses that dependency.
320319
return invalidatedNodesFromIncrementalExternal ?? collectUntracedNodesUsing(fed)
321320
}
322-
321+
322+
private func hasFileChanged(of externalDependency: ExternalDependency
323+
) -> Bool {
324+
if let hasChanged = externalDependencyModTimeCache[externalDependency] {
325+
return hasChanged
326+
}
327+
let hasChanged = (externalDependency.modTime(info.fileSystem) ?? .distantFuture)
328+
>= info.buildTime
329+
externalDependencyModTimeCache[externalDependency] = hasChanged
330+
return hasChanged
331+
}
332+
323333
private struct LazyModTimer {
324334
let externalDependency: ExternalDependency
325335
let info: IncrementalCompilationState.InitialStateComputer

0 commit comments

Comments
 (0)