|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Foundation |
| 14 | +import LSPLogging |
| 15 | +import LanguageServerProtocol |
| 16 | +import SKCore |
| 17 | + |
| 18 | +/// Describes the state of indexing for a single source file |
| 19 | +private enum FileIndexStatus { |
| 20 | + /// The index is up-to-date. |
| 21 | + case upToDate |
| 22 | + /// The file is being indexed by the given task. |
| 23 | + case inProgress(Task<Void, Never>) |
| 24 | +} |
| 25 | + |
| 26 | +/// Schedules index tasks and keeps track of the index status of files. |
| 27 | +public final actor SemanticIndexManager { |
| 28 | + /// The underlying index. This is used to check if the index of a file is already up-to-date, in which case it doesn't |
| 29 | + /// need to be indexed again. |
| 30 | + private let index: CheckedIndex |
| 31 | + |
| 32 | + /// The build system manager that is used to get compiler arguments for a file. |
| 33 | + private let buildSystemManager: BuildSystemManager |
| 34 | + |
| 35 | + /// The index status of the source files that the `SemanticIndexManager` knows about. |
| 36 | + /// |
| 37 | + /// Files that have never been indexed are not in this dictionary. |
| 38 | + private var indexStatus: [DocumentURI: FileIndexStatus] = [:] |
| 39 | + |
| 40 | + /// The `TaskScheduler` that manages the scheduling of index tasks. This is shared among all `SemanticIndexManager`s |
| 41 | + /// in the process, to ensure that we don't schedule more index operations than processor cores from multiple |
| 42 | + /// workspaces. |
| 43 | + private let indexTaskScheduler: TaskScheduler<UpdateIndexStoreTaskDescription> |
| 44 | + |
| 45 | + /// Callback that is called when an index task has finished. |
| 46 | + /// |
| 47 | + /// Currently only used for testing. |
| 48 | + private let indexTaskDidFinish: (@Sendable (UpdateIndexStoreTaskDescription) -> Void)? |
| 49 | + |
| 50 | + // MARK: - Public API |
| 51 | + |
| 52 | + public init( |
| 53 | + index: UncheckedIndex, |
| 54 | + buildSystemManager: BuildSystemManager, |
| 55 | + indexTaskScheduler: TaskScheduler<UpdateIndexStoreTaskDescription>, |
| 56 | + indexTaskDidFinish: (@Sendable (UpdateIndexStoreTaskDescription) -> Void)? |
| 57 | + ) { |
| 58 | + self.index = index.checked(for: .modifiedFiles) |
| 59 | + self.buildSystemManager = buildSystemManager |
| 60 | + self.indexTaskScheduler = indexTaskScheduler |
| 61 | + self.indexTaskDidFinish = indexTaskDidFinish |
| 62 | + } |
| 63 | + |
| 64 | + /// Schedules a task to index all files in `files` that don't already have an up-to-date index. |
| 65 | + /// Returns immediately after scheduling that task. |
| 66 | + /// |
| 67 | + /// Indexing is being performed with a low priority. |
| 68 | + public func scheduleBackgroundIndex(files: some Collection<DocumentURI>) { |
| 69 | + self.index(files: files, priority: .low) |
| 70 | + } |
| 71 | + |
| 72 | + /// Wait for all in-progress index tasks to finish. |
| 73 | + public func waitForUpToDateIndex() async { |
| 74 | + logger.info("Waiting for up-to-date index") |
| 75 | + await withTaskGroup(of: Void.self) { taskGroup in |
| 76 | + for (_, status) in indexStatus { |
| 77 | + switch status { |
| 78 | + case .inProgress(let task): |
| 79 | + taskGroup.addTask { |
| 80 | + await task.value |
| 81 | + } |
| 82 | + case .upToDate: |
| 83 | + break |
| 84 | + } |
| 85 | + } |
| 86 | + await taskGroup.waitForAll() |
| 87 | + } |
| 88 | + index.pollForUnitChangesAndWait() |
| 89 | + logger.debug("Done waiting for up-to-date index") |
| 90 | + } |
| 91 | + |
| 92 | + /// Ensure that the index for the given files is up-to-date. |
| 93 | + /// |
| 94 | + /// This tries to produce an up-to-date index for the given files as quickly as possible. To achieve this, it might |
| 95 | + /// suspend previous target-wide index tasks in favor of index tasks that index a fewer files. |
| 96 | + public func waitForUpToDateIndex(for uris: some Collection<DocumentURI>) async { |
| 97 | + logger.info( |
| 98 | + "Waiting for up-to-date index for \(uris.map { $0.fileURL?.lastPathComponent ?? $0.stringValue }.joined(separator: ", "))" |
| 99 | + ) |
| 100 | + let filesWithOutOfDateIndex = uris.filter { uri in |
| 101 | + switch indexStatus[uri] { |
| 102 | + case .inProgress, nil: return true |
| 103 | + case .upToDate: return false |
| 104 | + } |
| 105 | + } |
| 106 | + // Create a new index task for the files that aren't up-to-date. The newly scheduled index tasks will |
| 107 | + // - Wait for the existing index operations to finish if they have the same number of files. |
| 108 | + // - Reschedule the background index task in favor of an index task with fewer source files. |
| 109 | + await self.index(files: filesWithOutOfDateIndex, priority: nil).value |
| 110 | + index.pollForUnitChangesAndWait() |
| 111 | + logger.debug("Done waiting for up-to-date index") |
| 112 | + } |
| 113 | + |
| 114 | + // MARK: - Helper functions |
| 115 | + |
| 116 | + /// Index the given set of files at the given priority. |
| 117 | + /// |
| 118 | + /// The returned task finishes when all files are indexed. |
| 119 | + @discardableResult |
| 120 | + private func index(files: some Collection<DocumentURI>, priority: TaskPriority?) -> Task<Void, Never> { |
| 121 | + let outOfDateFiles = files.filter { |
| 122 | + if case .upToDate = indexStatus[$0] { |
| 123 | + return false |
| 124 | + } |
| 125 | + return true |
| 126 | + } |
| 127 | + |
| 128 | + var indexTasks: [Task<Void, Never>] = [] |
| 129 | + |
| 130 | + // TODO (indexing): Group index operations by target when we support background preparation. |
| 131 | + for files in outOfDateFiles.partition(intoNumberOfBatches: ProcessInfo.processInfo.processorCount * 5) { |
| 132 | + let indexTask = Task(priority: priority) { |
| 133 | + await self.indexTaskScheduler.schedule( |
| 134 | + priority: priority, |
| 135 | + UpdateIndexStoreTaskDescription( |
| 136 | + filesToIndex: Set(files), |
| 137 | + buildSystemManager: self.buildSystemManager, |
| 138 | + index: self.index, |
| 139 | + didFinishCallback: { [weak self] taskDescription in |
| 140 | + self?.indexTaskDidFinish?(taskDescription) |
| 141 | + } |
| 142 | + ) |
| 143 | + ).value |
| 144 | + for file in files { |
| 145 | + indexStatus[file] = .upToDate |
| 146 | + } |
| 147 | + } |
| 148 | + indexTasks.append(indexTask) |
| 149 | + |
| 150 | + for file in files { |
| 151 | + indexStatus[file] = .inProgress(indexTask) |
| 152 | + } |
| 153 | + } |
| 154 | + let indexTasksImmutable = indexTasks |
| 155 | + |
| 156 | + return Task(priority: priority) { |
| 157 | + await withTaskGroup(of: Void.self) { taskGroup in |
| 158 | + for indexTask in indexTasksImmutable { |
| 159 | + taskGroup.addTask(priority: priority) { |
| 160 | + await indexTask.value |
| 161 | + } |
| 162 | + } |
| 163 | + await taskGroup.waitForAll() |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments