Skip to content

Commit 447b8b5

Browse files
committed
Don’t poll the index for unit changes on every filesDidChange call
`pollForUnitChangesAndWait` is a costly operation since it iterates through all the unit files on the file system. We should only run it during initial indexing (where we might need to build the initial indexstore-db) but not for any subsequent indexing calls.
1 parent b3a73f6 commit 447b8b5

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

Sources/SemanticIndex/SemanticIndexManager.swift

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private struct OpaqueQueuedIndexTask: Equatable {
5454

5555
private enum InProgressIndexStore {
5656
/// We are waiting for preparation of the file's target to be scheduled. The next step is that we wait for
57-
/// prepration to finish before we can update the index store for this file.
57+
/// preparation to finish before we can update the index store for this file.
5858
///
5959
/// `preparationTaskID` identifies the preparation task so that we can transition a file's index state to
6060
/// `updatingIndexStore` when its preparation task has finished.
@@ -273,18 +273,25 @@ package final actor SemanticIndexManager {
273273
/// build system that don't currently have a unit with a timestamp that matches the mtime of the file.
274274
///
275275
/// If `filesToIndex` is `nil`, all files in the build system with out-of-date units are indexed.
276+
///
277+
/// If `ensureAllUnitsRegisteredInIndex` is `true`, ensure that all units are registered in the index before
278+
/// triggering the indexing. This is a costly operation since it iterates through all the unit files on the file
279+
/// system but if existing unit files are not known to the index, we might re-index those files even if they are
280+
/// up-to-date. Generally this should be set to `true` during the initial indexing (in which case we might be need to
281+
/// build the indexstore-db) and `false` for all subsequent indexing.
276282
package func scheduleBuildGraphGenerationAndBackgroundIndexAllFiles(
277283
filesToIndex: [DocumentURI]?,
284+
ensureAllUnitsRegisteredInIndex: Bool,
278285
indexFilesWithUpToDateUnit: Bool
279286
) async {
280287
let taskId = UUID()
281288
let generateBuildGraphTask = Task(priority: .low) {
282289
await withLoggingSubsystemAndScope(subsystem: indexLoggingSubsystem, scope: "build-graph-generation") {
283290
await hooks.buildGraphGenerationDidStart?()
284291
await self.buildSystemManager.waitForUpToDateBuildGraph()
285-
// Ensure that we have an up-to-date indexstore-db. Waiting for the indexstore-db to be updated is cheaper than
286-
// potentially not knowing about unit files, which causes the corresponding source files to be re-indexed.
287-
index.pollForUnitChangesAndWait()
292+
if ensureAllUnitsRegisteredInIndex {
293+
index.pollForUnitChangesAndWait()
294+
}
288295
await hooks.buildGraphGenerationDidFinish?()
289296
// TODO: Ideally this would be a type like any Collection<DocumentURI> & Sendable but that doesn't work due to
290297
// https://github.com/swiftlang/swift/issues/75602
@@ -324,7 +331,11 @@ package final actor SemanticIndexManager {
324331
package func scheduleReindex() async {
325332
await indexStoreUpToDateTracker.markAllKnownOutOfDate()
326333
await preparationUpToDateTracker.markAllKnownOutOfDate()
327-
await scheduleBuildGraphGenerationAndBackgroundIndexAllFiles(filesToIndex: nil, indexFilesWithUpToDateUnit: true)
334+
await scheduleBuildGraphGenerationAndBackgroundIndexAllFiles(
335+
filesToIndex: nil,
336+
ensureAllUnitsRegisteredInIndex: false,
337+
indexFilesWithUpToDateUnit: true
338+
)
328339
}
329340

330341
private func waitForBuildGraphGenerationTasks() async {
@@ -412,6 +423,7 @@ package final actor SemanticIndexManager {
412423

413424
await scheduleBuildGraphGenerationAndBackgroundIndexAllFiles(
414425
filesToIndex: changedFiles,
426+
ensureAllUnitsRegisteredInIndex: false,
415427
indexFilesWithUpToDateUnit: false
416428
)
417429
}

Sources/SourceKitLSP/Workspace.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ package final class Workspace: Sendable, BuildSystemManagerDelegate {
190190
if let semanticIndexManager {
191191
await semanticIndexManager.scheduleBuildGraphGenerationAndBackgroundIndexAllFiles(
192192
filesToIndex: nil,
193+
ensureAllUnitsRegisteredInIndex: true,
193194
indexFilesWithUpToDateUnit: false
194195
)
195196
}

0 commit comments

Comments
 (0)