Skip to content

Commit 5b309e1

Browse files
authored
Merge pull request #538 from davidungar/try-mod-time-cache
[Incremental] Try a cache for external dependency mod times
2 parents 1d03b9b + b9f3aa5 commit 5b309e1

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

Sources/SwiftDriver/IncrementalCompilation/ModuleDependencyGraph.swift

Lines changed: 13 additions & 11 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,13 +318,16 @@ extension ModuleDependencyGraph {
319318
// return anything that uses that dependency.
320319
return invalidatedNodesFromIncrementalExternal ?? collectUntracedNodesUsing(fed)
321320
}
322-
323-
private struct LazyModTimer {
324-
let externalDependency: ExternalDependency
325-
let info: IncrementalCompilationState.InitialStateComputer
326321

327-
lazy var hasExternalFileChanged = (externalDependency.modTime(info.fileSystem) ?? .distantFuture)
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)
328328
>= info.buildTime
329+
externalDependencyModTimeCache[externalDependency] = hasChanged
330+
return hasChanged
329331
}
330332

331333
/// Try to read and integrate an external dependency.

0 commit comments

Comments
 (0)