@@ -54,7 +54,7 @@ private struct OpaqueQueuedIndexTask: Equatable {
54
54
55
55
private enum InProgressIndexStore {
56
56
/// 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.
58
58
///
59
59
/// `preparationTaskID` identifies the preparation task so that we can transition a file's index state to
60
60
/// `updatingIndexStore` when its preparation task has finished.
@@ -273,18 +273,25 @@ package final actor SemanticIndexManager {
273
273
/// build system that don't currently have a unit with a timestamp that matches the mtime of the file.
274
274
///
275
275
/// 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.
276
282
package func scheduleBuildGraphGenerationAndBackgroundIndexAllFiles(
277
283
filesToIndex: [ DocumentURI ] ? ,
284
+ ensureAllUnitsRegisteredInIndex: Bool ,
278
285
indexFilesWithUpToDateUnit: Bool
279
286
) async {
280
287
let taskId = UUID ( )
281
288
let generateBuildGraphTask = Task ( priority: . low) {
282
289
await withLoggingSubsystemAndScope ( subsystem: indexLoggingSubsystem, scope: " build-graph-generation " ) {
283
290
await hooks. buildGraphGenerationDidStart ? ( )
284
291
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
+ }
288
295
await hooks. buildGraphGenerationDidFinish ? ( )
289
296
// TODO: Ideally this would be a type like any Collection<DocumentURI> & Sendable but that doesn't work due to
290
297
// https://github.com/swiftlang/swift/issues/75602
@@ -324,7 +331,11 @@ package final actor SemanticIndexManager {
324
331
package func scheduleReindex( ) async {
325
332
await indexStoreUpToDateTracker. markAllKnownOutOfDate ( )
326
333
await preparationUpToDateTracker. markAllKnownOutOfDate ( )
327
- await scheduleBuildGraphGenerationAndBackgroundIndexAllFiles ( filesToIndex: nil , indexFilesWithUpToDateUnit: true )
334
+ await scheduleBuildGraphGenerationAndBackgroundIndexAllFiles (
335
+ filesToIndex: nil ,
336
+ ensureAllUnitsRegisteredInIndex: false ,
337
+ indexFilesWithUpToDateUnit: true
338
+ )
328
339
}
329
340
330
341
private func waitForBuildGraphGenerationTasks( ) async {
@@ -412,6 +423,7 @@ package final actor SemanticIndexManager {
412
423
413
424
await scheduleBuildGraphGenerationAndBackgroundIndexAllFiles (
414
425
filesToIndex: changedFiles,
426
+ ensureAllUnitsRegisteredInIndex: false ,
415
427
indexFilesWithUpToDateUnit: false
416
428
)
417
429
}
0 commit comments