Skip to content

F# VS editor: reduce background CPU load by gating expensive analyzers to the active document only - #20117

Draft
xperiandri wants to merge 1 commit into
dotnet:mainfrom
xperiandri:fix/active-doc-gating
Draft

F# VS editor: reduce background CPU load by gating expensive analyzers to the active document only#20117
xperiandri wants to merge 1 commit into
dotnet:mainfrom
xperiandri:fix/active-doc-gating

Conversation

@xperiandri

Copy link
Copy Markdown
Contributor

Fixes #20114

Summary

CPU profiling of devenv.exe (VS Insiders + F# extension) showed ThreadPoolWorkQueue.Dispatch at 72 % total / 59 % self, driven by continuous F# background analysis running for all open tabs, not just the active one.

This PR implements the highest-impact P0 fixes from docs/ide/background-activity-minimization-plan.md:

Changes

New: ActiveDocumentDetection module

vsintegration/src/FSharp.Editor/Diagnostics/ActiveDocumentDetection.fs

Shared helper that uses IVsMonitorSelection / IVsWindowFrame to determine whether a Roslyn Document is the currently focused VS editor tab.

  • tryGetActiveDocumentMoniker — returns string voption (the active file path, or ValueNone)
  • isActiveDocument — returns bool; falls back to true (= don't suppress) when detection fails, so analysis is never silently lost

Gated analyzers / services

Component Before After
UnusedOpensDiagnosticAnalyzer Used inline shell detection Refactored to shared ActiveDocumentDetection module
UnusedDeclarationsAnalyzer No gating — ran full typecheck for every open tab Gated to active document
SimplifyNameDiagnosticAnalyzer No gating — 3 concurrent typechecks for all open tabs Gated to active document
FSharpInlayHintsService No gating — N−1 unnecessary typechecks Gated to active document

Behavior

  • Non-active documents receive no background diagnostics/hints while out of focus.
  • Switching to a tab immediately triggers fresh analysis for that tab.
  • This matches C#'s default behavior (BackgroundAnalysisScope.VisibleFilesAndOpenFilesWithPreviouslyReportedDiagnostics).
  • Roslyn clears stale diagnostics automatically when a document becomes active.

Not in scope (tracked in the plan)

- Add ActiveDocumentDetection module (IVsMonitorSelection-based helper)
- Gate UnusedDeclarationsAnalyzer to active document
- Gate SimplifyNameDiagnosticAnalyzer to active document
- Gate FSharpInlayHintsService to active document
- Gate UnusedOpensDiagnosticAnalyzer to active document
- Add ActiveDocumentDetection.fs to FSharp.Editor.fsproj

Fixes dotnet#20114
@majocha

majocha commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

It seems this can be controlled by VS host:

image

https://learn.microsoft.com/en-us/visualstudio/code-quality/configure-live-code-analysis-scope-managed-code?view=visualstudio

Maybe it's possible to expose this setting for F#.

type internal SimplifyNameDiagnosticAnalyzer [<ImportingConstructor>] () =
type internal SimplifyNameDiagnosticAnalyzer
[<ImportingConstructor>]
([<Import("Microsoft.VisualStudio.Shell.SVsServiceProvider")>] serviceProvider: IServiceProvider) =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Import("...") sets only the contract name. The parameter still requires IServiceProvider, but Visual Studio exports SVsServiceProvider. MEF rejects all four parts. Import SVsServiceProvider by type.

/// Returns the document moniker (full file path) of the currently focused
/// editor window, or ValueNone if it cannot be determined.
let tryGetActiveDocumentMoniker (serviceProvider: IServiceProvider) : string voption =
match serviceProvider.GetService(typeof<SVsShellMonitorSelection>) with

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Roslyn runs these callbacks on worker threads. This code calls UI-bound shell services without switching threads, so exceptions escape the fail-open path. Track selection on the UI thread and publish a thread-safe snapshot.

| filePath ->
match tryGetActiveDocumentMoniker serviceProvider with
| ValueNone -> true // couldn't determine the active document, don't suppress analysis
| ValueSome activeMoniker -> String.Equals(activeMoniker, filePath, StringComparison.OrdinalIgnoreCase)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Results now depend on focus, but no provider invalidates results when focus moves between visible panes. Invalidate both documents on selection changes, or remove result-level gating.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

F# VS editor: reduce background CPU load by gating expensive analyzers to the active document only

3 participants