Skip to content

Speed up CMake Tools activation: lazy language-service data and no activation-time kit-prune storm - #5035

Merged
Hannia Valera (hanniavalera) merged 6 commits into
mainfrom
dev/hanniavalera/activation-backend-perf
Sep 9, 2026
Merged

Hannia Valera (hanniavalera) merged 6 commits into
mainfrom
dev/hanniavalera/activation-backend-perf

Conversation

@hanniavalera

@hanniavalera Hannia Valera (hanniavalera) commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

What

Two backend changes so extension activation no longer blocks on work that isn't needed to activate, and so CMake Tools stops adding to (and misreading) filesystem/handle pressure during startup:

  1. Load the bundled language-service data on first use instead of during activation.
  2. Stop checking user kits for missing compilers on every activation, and never treat a transient filesystem error (e.g. file-handle exhaustion) as a missing compiler.

Why

On machines where process/file I/O is intermittently slow (aggressive antivirus scanning of spawned executables, or extension-host file-handle exhaustion / EMFILE), activation could take minutes and the CMake sidebar stayed hidden. Investigation showed the extreme latency is environmentally triggered, but the extension puts avoidable, environment-sensitive work on the activation critical path, and even contributes to the handle pressure itself:

  • ExtensionManager.init() awaited enableLanguageServices() first, which serially read and parsed ~594 KB of bundled hover/completion JSON (commands/variables/modules/policies) and registered the providers only after that load. That data is only needed when the user hovers or completes in a CMake file — never for activation, the sidebar, or configure/build.
  • Right after loading user kits, activation fire-and-forgot an unbounded burst of compiler-existence probes (up to ~2 per kit). Worse, those probes went through helpers that collapse every error to "missing", so under EMFILE/EACCES the extension both added handle pressure and could prompt to remove valid kits.

How

Language-service data (commit 1):

  • LanguageServiceData.create() is now synchronous; the data loads lazily on the first hover/completion via a memoized ensureLoaded() (a failed load is memoized so it doesn't retry the four files on every keystroke). provideHover/provideCompletionItems are async and honor cancellation/disposal before and after the load. The four reads stay serial (no Promise.all) and there is no background prewarm, so activation performs zero language-asset I/O.
  • enableLanguageServices() registers the hover/completion providers and language configuration immediately; disposeLanguageServices() disposes the provider and clears its registrations. Language-server-only mode is unaffected.

Kit pruning (commit 2):

  • Removed the stale-kit check from activation / user-kit-file reads. Stale kits are still detected during a kit/compiler scan (unchanged entry point), which is the expected maintenance boundary.
  • Hardened the scan-time prune: it probes sequentially (no burst), dedupes repeated compiler paths, is single-flight with one coalesced follow-up, and prompts sequentially.
  • Fixed the error classification (tri-state present/absent/unknown): absolute paths use fs.stat and treat only ENOENT/ENOTDIR as absent; relative names go through which, whose failures are treated as unknown (it synthesizes ENOENT for all failures and so cannot prove absence). A transient error can therefore never prompt removal of a valid kit.

Scope / follow-ups

This is a backend activation-performance/correctness fix. It does not change the actual configure/build work or its timing. The remaining irreducible cost at activation is subprocess latency (e.g. cmake -E capabilities, and vcvarsall for cl.exe presets under antivirus); a companion UI change (#5027) shows an "initializing" placeholder so the sidebar appears immediately while that residual work completes: complementary to this PR, not a substitute for it. Further optimizations identified but intentionally deferred: single-spawn CMake interrogation (capabilities-first), in-process compiler lookup for VS-dev-env detection, and caching compiled JSON schema validators.

Testing

  • yarn backendTests (384 passing) - new test/unit-tests/backend/kitTriState.test.ts (tri-state classification incl. EMFILE/ENFILE/EACCES→unknown and which-error→unknown) and test/unit-tests/backend/languageServiceLazyLoad.test.ts (lazy memoization / failure / disposal semantics).
  • yarn unitTests - 330 passing.
  • tsc + gulp lint clean.
  • Behavior verified in presets and kits/variants modes, single/multi-config, multi-root, and language-server-only mode. Stale-kit prompts now appear on explicit scan rather than on every activation (a deliberate behavior change, noted in the CHANGELOG).
…ivation

Activation previously awaited LanguageServiceData.create(), which serially read and JSON-parsed the four bundled language-service asset files (commands, variables, modules, policies) before providers were registered. On slow or handle-starved machines this delayed activation and could keep the CMake view hidden.

LanguageServiceData.create() is now synchronous and returns an unloaded instance. The asset data is loaded on the first hover/completion request via a memoized ensureLoaded() promise shared by concurrent requests; a load failure is memoized as false (never a rejected promise and never cleared) so providers stop returning results without re-reading the four files on every request. provideHover and provideCompletionItems are now async and check the disposed flag and cancellation both before and after awaiting the load. A per-instance disposed flag suppresses late results from a disposed instance.

ExtensionManager.enableLanguageServices() now constructs the provider and registers the hover, completion, and language configuration immediately without any asset I/O, storing the instance so disposeLanguageServices() can dispose it, dispose and clear the registration list, and clear the field. enableLanguageServices() also disposes any prior registrations first as a duplicate-registration guard. The one-time localized initialization-failure message is reported via a one-shot callback passed into create().
@hanniavalera
Hannia Valera (hanniavalera) force-pushed the dev/hanniavalera/activation-backend-perf branch from 94b3031 to 4d19175 Compare August 11, 2026 15:54
readUserKits() previously fire-and-forgot a full pass over every user kit that probed each compiler for existence. This ran on every activation and on every user-kit file reread, launching an unbounded burst of fs.exists()/paths.which() calls. Both helpers collapse all errors to 'not found', so under file-handle exhaustion (EMFILE) valid compilers were misclassified as missing and the user could be prompted to remove valid kits -- while the burst itself added to the handle pressure.

Activation-time pruning is removed: readUserKits() now only reads and updates the in-memory user kit list. Stale-kit detection is preserved via scanForKits(), which still runs the prune after writing scan results, so stale kits are flagged during an explicit kit/compiler scan instead of on every activation.

The prune itself (still used by scan) is hardened: a static single-flight promise prevents overlapping scans from stacking probe bursts; compiler paths are probed sequentially and deduped within a run; and existence is now tri-state ('present' | 'absent' | 'unknown'). Absolute paths are stat'd directly and relative names are resolved with the 'which' package directly (not paths.which), preserving the error cause. Only ENOENT/ENOTDIR count as definitively absent; any other error (EMFILE/ENFILE/EACCES/EPERM/...) is 'unknown' and the kit is skipped, so transient failures never prompt a removal. Prompts are presented sequentially. The tri-state classifier lives in a small dependency-free module (src/kits/compilerPathState.ts) so it can be unit-tested directly.
Comment thread src/kits/compilerPathState.ts Resolved

@Omotola Omotola left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking, but: if the first hover/completion triggers the lazy load and it fails (e.g. under the file-handle exhaustion this PR targets), the failure is cached permanently and language services stay off until reload. Might be worth retrying on the next hover instead of caching the failure.

@hanniavalera
Hannia Valera (hanniavalera) merged commit 60804ec into main Sep 9, 2026
29 checks passed
@hanniavalera
Hannia Valera (hanniavalera) deleted the dev/hanniavalera/activation-backend-perf branch September 9, 2026 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants