Files
sourcekit-lsp/Sources/SourceKitLSP/SourceKitIndexDelegate.swift
Alex Hoppen 44a095c8aa Use an AtomicInt32 to count pendingUnitCount instead of using AsyncQueue
Adding an item to `AsyncQueue<Serial>` is linear in the number of pending queue items, thus adding n items to an `AsyncQueue` before any can execute is in O(n^2). This decision was made intentionally because the primary use case for `AsyncQueue` was to track pending LSP requests, of which we don’t expect to have too many pending requests at any given time.

`SourceKitIndexDelegate` was also using `AsyncQueue` to track the number of pending units to be processed and eg. after indexing SourceKit-LSP, I have seen this grow up to ~20,000. With the quadratic behavior, this explodes time-wise.

Turns out that we don’t actually need to use a queue here at all, an atomic is sufficient and much faster.

Independently, we should consider mitigating the quadratic behavior of `AsyncQueue<Serial>` or `AsyncQueue` in general.

Fixes #1541
rdar://130844901
2024-10-08 17:35:13 -07:00

67 lines
2.2 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Dispatch
import IndexStoreDB
import LSPLogging
import SKCore
import SKSupport
import SwiftExtensions
/// `IndexDelegate` for the SourceKit workspace.
actor SourceKitIndexDelegate: IndexDelegate {
/// Registered `MainFilesDelegate`s to notify when main files change.
var mainFilesChangedCallbacks: [@Sendable () async -> Void] = []
/// The count of pending unit events. Whenever this transitions to 0, it represents a time where
/// the index finished processing known events. Of course, that may have already changed by the
/// time we are notified.
let pendingUnitCount = AtomicInt32(initialValue: 0)
public init() {}
nonisolated public func processingAddedPending(_ count: Int) {
pendingUnitCount.value += Int32(count)
}
nonisolated public func processingCompleted(_ count: Int) {
pendingUnitCount.value -= Int32(count)
if pendingUnitCount.value == 0 {
Task {
await indexChanged()
}
}
if pendingUnitCount.value < 0 {
// Technically this is not data race safe because `pendingUnitCount` might change between the check and us setting
// it to 0. But then, this should never happen anyway, so it's fine.
logger.fault("pendingUnitCount dropped below zero: \(self.pendingUnitCount.value)")
pendingUnitCount.value = 0
Task {
await indexChanged()
}
}
}
private func indexChanged() async {
logger.debug("IndexStoreDB changed")
for callback in mainFilesChangedCallbacks {
await callback()
}
}
/// Register a delegate to receive notifications when main files change.
public func addMainFileChangedCallback(_ callback: @escaping @Sendable () async -> Void) {
mainFilesChangedCallbacks.append(callback)
}
}