Skip to content

Commit d37a1e0

Browse files
committed
Cache the set of buildable source files
This way we don’t need to rebuild the set every time we call `filesToIndex(toCover:)`.
1 parent 15c34bf commit d37a1e0

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

Sources/BuildSystemIntegration/BuildSystemManager.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,20 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
392392
/// `pathComponents` is the result of `key.fileURL?.pathComponents`. We frequently need these path components to
393393
/// determine if a file is descendent of the directory and computing them from the `DocumentURI` is expensive.
394394
let directories: [DocumentURI: (pathComponents: [String]?, info: SourceFileInfo)]
395+
396+
/// Same as `Set(files.filter(\.value.isBuildable).keys)`. Pre-computed because we need thi pretty frequently in
397+
/// `SemanticIndexManager.filesToIndex`.
398+
let buildableSourceFiles: Set<DocumentURI>
399+
400+
internal init(
401+
files: [DocumentURI: SourceFileInfo],
402+
directories: [DocumentURI: (pathComponents: [String]?, info: SourceFileInfo)]
403+
) {
404+
self.files = files
405+
self.directories = directories
406+
self.buildableSourceFiles = Set(files.filter(\.value.isBuildable).keys)
407+
}
408+
395409
}
396410

397411
private let cachedSourceFilesAndDirectories = Cache<SourceFilesAndDirectoriesKey, SourceFilesAndDirectories>()
@@ -1153,6 +1167,13 @@ package actor BuildSystemManager: QueueBasedMessageHandler {
11531167
}
11541168
}
11551169

1170+
/// Returns all source files in the project that are considered buildable.
1171+
///
1172+
/// - SeeAlso: Comment in `sourceFilesAndDirectories` for a definition of what `buildable` means.
1173+
package func buildableSourceFiles() async throws -> Set<DocumentURI> {
1174+
return try await sourceFilesAndDirectories().buildableSourceFiles
1175+
}
1176+
11561177
/// Get all files and directories that are known to the build system, ie. that are returned by a `buildTarget/sources`
11571178
/// request for any target in the project.
11581179
///

Sources/SemanticIndex/SemanticIndexManager.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,7 @@ package final actor SemanticIndexManager {
305305
filesToIndex
306306
} else {
307307
await orLog("Getting files to index") {
308-
try await self.buildSystemManager.sourceFiles(includeNonBuildableFiles: false).keys
309-
.sorted { $0.stringValue < $1.stringValue }
308+
try await self.buildSystemManager.buildableSourceFiles().sorted { $0.stringValue < $1.stringValue }
310309
} ?? []
311310
}
312311
_ = await self.scheduleIndexing(
@@ -426,7 +425,7 @@ package final actor SemanticIndexManager {
426425
indexFilesWithUpToDateUnits: Bool
427426
) async -> [(file: FileToIndex, fileModificationDate: Date?)] {
428427
let sourceFiles = await orLog("Getting source files in project") {
429-
Set(try await buildSystemManager.sourceFiles(includeNonBuildableFiles: false).keys)
428+
try await buildSystemManager.buildableSourceFiles()
430429
}
431430
guard let sourceFiles else {
432431
return []
@@ -461,6 +460,7 @@ package final actor SemanticIndexManager {
461460
.mainFilesContainingFile(uri: uri, crossLanguage: false)
462461
.sorted(by: { $0.stringValue < $1.stringValue }).first
463462
guard let mainFile else {
463+
logger.log("Not indexing \(uri) because its main file could not be inferred")
464464
return nil
465465
}
466466
if !indexFilesWithUpToDateUnits, modifiedFilesIndex.hasUpToDateUnit(for: uri, mainFile: mainFile) {

0 commit comments

Comments
 (0)