Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions vsintegration/src/FSharp.Editor/Common/CancellableTasks.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,28 @@ module CancellableTasks =
return! Task.WhenAll (tasks)
}

/// Runs the given tasks concurrently, but caps the number of tasks that are running at
/// the same time to at most maxDegreeOfParallelism, to avoid launching an unbounded number
/// of parallel typechecks (e.g. one per document/project) at once.
let inline whenAllThrottled maxDegreeOfParallelism (tasks: CancellableTask<'a> seq) =
cancellableTask {
let! ct = getCancellationToken ()
use semaphore = new SemaphoreSlim(maxDegreeOfParallelism: int)

let runThrottled (task: CancellableTask<'a>) =
backgroundTask {
do! semaphore.WaitAsync(ct)

try
return! start ct task
finally
semaphore.Release() |> ignore
}

let tasks = seq { for task in tasks do yield runThrottled task }
return! Task.WhenAll (tasks)
}

let inline whenAllTasks (tasks: CancellableTask seq) =
cancellableTask {
let! ct = getCancellationToken ()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[<AutoOpen>]
[<AutoOpen>]
module internal Microsoft.VisualStudio.FSharp.Editor.WorkspaceExtensions

open System
Expand Down Expand Up @@ -707,7 +707,8 @@ type Project with
documents
|> Seq.map (fun doc ->
doc.FindFSharpReferencesAsync(symbol, projectSnapshot, (fun range -> onFound doc range), userOpName))
|> CancellableTask.whenAll
// Throttle to avoid launching a typecheck per document in the project all at once.
|> CancellableTask.whenAllThrottled (max 1 Environment.ProcessorCount)
else
for doc in documents do
do! doc.FindFSharpReferencesAsync(symbol, projectSnapshot, (onFound doc), userOpName)
Expand Down
Loading