Skip to content

Commit 19831b7

Browse files
committed
Allow alternate build systems to have larger indexing batch sizes
1 parent 1ab9e2e commit 19831b7

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

Sources/BuildServerIntegration/BuildServerManager.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,17 @@ package actor BuildServerManager: QueueBasedMessageHandler {
316316
/// `nil` if the `BuildServerManager` does not have an underlying build server.
317317
package let configPath: URL?
318318

319+
/// The kind of underlying build server adapter that is being managed.
320+
/// `nil` if the `BuildServerManager` does not have an underlying build server.
321+
package var kind: BuildServerSpec.Kind?
322+
319323
/// The files for which the delegate has requested change notifications, ie. the files for which the delegate wants to
320324
/// get `fileBuildSettingsChanged` and `filesDependenciesUpdated` callbacks.
321325
private var watchedFiles: [DocumentURI: (mainFile: DocumentURI, language: Language)] = [:]
322326

323327
private var connectionToClient: BuildServerManagerConnectionToClient
324328

325-
/// The build serer adapter that is used to answer build server queries.
329+
/// The build server adapter that is used to answer build server queries.
326330
private var buildServerAdapter: BuildServerAdapter?
327331

328332
/// The build server adapter after initialization finishes. When sending messages to the BSP server, this should be
@@ -451,6 +455,7 @@ package actor BuildServerManager: QueueBasedMessageHandler {
451455
self.toolchainRegistry = toolchainRegistry
452456
self.options = options
453457
self.connectionToClient = connectionToClient
458+
self.kind = buildServerSpec?.kind
454459
self.configPath = buildServerSpec?.configPath
455460
self.buildServerAdapter = await buildServerSpec?.createBuildServerAdapter(
456461
toolchainRegistry: toolchainRegistry,

Sources/SemanticIndex/SemanticIndexManager.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ package final actor SemanticIndexManager {
222222
/// The parameter is the number of files that were scheduled to be indexed.
223223
private let indexTasksWereScheduled: @Sendable (_ numberOfFileScheduled: Int) -> Void
224224

225+
/// The number of tasks to prepare concurrently, whenever a index request is scheduled.
226+
private let indexTaskBatchSize: Int
227+
225228
/// Callback that is called when `progressStatus` might have changed.
226229
private let indexProgressStatusDidChange: @Sendable () -> Void
227230

@@ -261,6 +264,7 @@ package final actor SemanticIndexManager {
261264
updateIndexStoreTimeout: Duration,
262265
hooks: IndexHooks,
263266
indexTaskScheduler: TaskScheduler<AnyIndexTaskDescription>,
267+
indexTaskBatchSize: Int,
264268
logMessageToIndexLog:
265269
@escaping @Sendable (
266270
_ message: String, _ type: WindowMessageType, _ structure: StructuredLogKind
@@ -273,6 +277,7 @@ package final actor SemanticIndexManager {
273277
self.updateIndexStoreTimeout = updateIndexStoreTimeout
274278
self.hooks = hooks
275279
self.indexTaskScheduler = indexTaskScheduler
280+
self.indexTaskBatchSize = indexTaskBatchSize
276281
self.logMessageToIndexLog = logMessageToIndexLog
277282
self.indexTasksWereScheduled = indexTasksWereScheduled
278283
self.indexProgressStatusDidChange = indexProgressStatusDidChange
@@ -877,10 +882,7 @@ package final actor SemanticIndexManager {
877882

878883
var indexTasks: [Task<Void, Never>] = []
879884

880-
// TODO: When we can index multiple targets concurrently in SwiftPM, increase the batch size to half the
881-
// processor count, so we can get parallelism during preparation.
882-
// (https://github.com/swiftlang/sourcekit-lsp/issues/1262)
883-
for targetsBatch in sortedTargets.partition(intoBatchesOfSize: 1) {
885+
for targetsBatch in sortedTargets.partition(intoBatchesOfSize: indexTaskBatchSize) {
884886
let preparationTaskID = UUID()
885887
let filesToIndex = targetsBatch.flatMap({ filesByTarget[$0]! })
886888

Sources/SourceKitLSP/Workspace.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,23 @@ package final class Workspace: Sendable, BuildServerManagerDelegate {
162162
if options.backgroundIndexingOrDefault, let uncheckedIndex,
163163
await buildServerManager.initializationData?.prepareProvider ?? false
164164
{
165+
let batchSize: Int
166+
switch await buildServerManager.kind {
167+
case .swiftPM:
168+
// TODO: When we can index multiple targets concurrently in SwiftPM, increase the batch size to half the
169+
// processor count, so we can get parallelism during preparation.
170+
// (https://github.com/swiftlang/sourcekit-lsp/issues/1262)
171+
batchSize = 1
172+
default:
173+
batchSize = max(1, ProcessInfo.processInfo.activeProcessorCount / 2)
174+
}
165175
self.semanticIndexManager = SemanticIndexManager(
166176
index: uncheckedIndex,
167177
buildServerManager: buildServerManager,
168178
updateIndexStoreTimeout: options.indexOrDefault.updateIndexStoreTimeoutOrDefault,
169179
hooks: hooks.indexHooks,
170180
indexTaskScheduler: indexTaskScheduler,
181+
indexTaskBatchSize: batchSize,
171182
logMessageToIndexLog: { [weak sourceKitLSPServer] in
172183
sourceKitLSPServer?.logMessageToIndexLog(message: $0, type: $1, structure: $2)
173184
},

0 commit comments

Comments
 (0)