Repository navigation
perf(lsp): coalesce diagnostics during bursts of edits - #2941
Merged
Merged
Conversation
didChange ran a full diagnostics pass per change, so a burst of edits paid for one whole-ledger validation per notification while only the last result mattered (#2858 measured about 46 ms per change on a 108k-line ledger). didChange now applies the edit to the VFS and bumps the revision at once, as before, so every request sees the latest text, but only schedules the diagnostics pass. A pass falls due once its document has gone DIAGNOSTICS_DEBOUNCE (200 ms) without another change. The main loop sleeps on a select! timer arm for the earliest deadline; when it fires, it first drains the messages already queued, so a backlog that piled up behind a slow pass pushes the deadline back and collapses into one pass. A pass reads the document's text from the VFS when it runs, so it can only describe the current version. Per trigger: - didOpen, root adoption and watched-file revalidation still publish immediately; any publish settles a pending pass for the same file. - didClose cancels a pending pass and clears, as before. - codeLens reads the diagnostics cache, so it runs a pending pass for its document first. Code actions compute from the current text and need nothing. - A URI with no file path never reaches the VFS and keeps the old immediate publish. The schedule never reads the clock itself; MainLoopState takes an injectable clock so the tests advance time by hand instead of sleeping. Closes #2940 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
…cument Review round 1 on the diagnostics debounce: - Shutdown drops every pending pass, so nothing is published between shutdown and exit. - A didChange with no prior didOpen (a client protocol violation) has no buffer for a deferred pass to read, so it publishes immediately from the given text, as before the debounce. - New test pins that an immediate publish (here a watched-file revalidation inside the debounce window) settles a pass pending for the same file; deleting that cancel now fails it. - CHANGELOG Performance entry with the measured pass counts, and the LSP support doc now says didChange is debounced, didSave is never received, and codeLens runs a pending pass first. - Note in DiagnosticsSchedule::schedule where a max-wait cap would go. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review round 2 on the diagnostics debounce: - After shutdown, handle_notification ignores everything but exit (LSP 3.17 Lifecycle), so a late didOpen, didChange or watched-file change can neither schedule nor publish a diagnostics pass. Pinned by test_notifications_after_shutdown_are_ignored. - docs: a didChange for a document with no buffer (non-file URI, or no prior didOpen) still publishes immediately, and shutdown drops pending passes and ignores document notifications. Co-Authored-By: Claude Opus 5.5 <[email protected]>
robcohen
added a commit
that referenced
this pull request
Nov 11, 2026
This was referenced Nov 11, 2026
robcohen
added a commit
that referenced
this pull request
Nov 11, 2026
#2947 merged as 4bc5bd8. Its hunks take main's side; this PR's own changes (6c601dd..00c8866) are applied on top. In the test tail of the LSP main loop, #2941's coalescing tests and this PR's tests are both kept, and change_doc flushes the debounced diagnostics pass before the publishes are read. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
textDocument/didChangeno longer runs a diagnostics pass per change. It still applies the edit to the VFS and bumps the revision immediately, so hover, completion, definition and every other request see the latest text exactly as before. The diagnostics pass is only scheduled, and runs once the document has goneDIAGNOSTICS_DEBOUNCE(200 ms) without another change.DiagnosticsSchedule(new,crates/rustledger-lsp/src/diagnostics_schedule.rs): path-keyed set of documents owing a pass, each with a deadline of last change + 200 ms. It never reads the clock; every method takesnow.select!arm waits oncrossbeam_channel::at(earliest deadline)(ornever()when nothing is pending). When it fires, it first drains the messages already queued (bounded byreceiver.len()at that moment, so a client that never stops sending cannot trap the loop), then runs the passes that are still due. A backlog that piled up behind a slow pass therefore pushes the deadline back and collapses into one pass.MainLoopStateholds aclock(Instant::nowin production) so tests advance time by hand.Why
#2858 measured about 46 ms per change on a 108k-line ledger, i.e. 15 to 30% of a core while typing at Neovim's default 150 ms debounce, and every pass but the last in a burst is thrown away. 200 ms sits above Neovim's 150 ms cadence so consecutive client batches still collapse while the user is typing.
Per-trigger decisions
didChangedidOpendidClosedidSavecodeLenscodeAction/ resolvedidChangewith no priordidOpenshutdownexitaftershutdown, so nothing is published between the twoHow to test
New tests, all on a hand-advanced clock (no sleeping):
diagnostics_schedule::tests::*: burst due once after the last change, interleaved documents each due once, cancel drops the pass.main_loop::tests::coalescing::(plus the three round-1 tests below)test_burst_of_changes_runs_one_pass_matching_final_text: 50 changes give exactly one pass, and its diagnostics equal a freshdidOpenof the final text; requests see each edit immediately.test_timer_drains_queued_changes_into_one_pass: 20 changes queued behind a due pass are drained first, giving one pass for the last version.test_interleaved_documents_each_get_their_final_passtest_no_publish_for_an_intermediate_version: version 2's deadline passes after version 3 arrived; nothing is published for version 2.test_code_lens_during_pending_pass_sees_current_diagnostics: an edit that breaks a balance assertion, thencodeLenswith the pass pending, shows the warning.test_did_close_during_pending_pass_cancels_and_clearstest_did_open_publishes_immediatelyRemoving the codeLens flush or the drain makes the corresponding test fail (checked).
Measured
A throwaway driver (not committed) runs the server in-process over
Connection::memory, opens a generated 99,999-line single-file ledger, sends 50 full-textdidChanges, then countspublishDiagnosticsfor the URI until 15 s of quiet (one publish per pass). Release build, same machine, run sequentially."Last publish" is measured from the first change. A single pass on this ledger costs about 0.7 to 1.5 s here, so on main the server was still working through the backlog for 20 to 50 s after the user stopped typing; with this PR the final diagnostics arrive one debounce plus one pass after the last edit.
Note on behavior: while a client keeps sending changes faster than every 200 ms, diagnostics wait until the edits pause. That is the requested debounce. In practice only continuous typing waits: vscode-languageclient already batches FULL-sync changes through a 250 ms trailing delayer and flushes them before sending a request, and Neovim sends at most one change per 150 ms by default. There is deliberately no max-wait cap (rust-analyzer coalesces without one, and the TypeScript tooling uses a trailing debounce); a comment in
DiagnosticsSchedule::schedulemarks where one would go if continuous typing should ever refresh periodically.Review round 1
docs/development/lsp-support.mdno longer claims the validator runs on everydidChange/didSave:didChangepasses are debounced,didSaveis never received (FULL sync, no save option), andcodeLensruns a pending pass first.test_revalidation_settles_a_pending_pass(edit A, then a watched-file change inside the debounce window, then assert nothing is pending and no redundant pass runs later). Deleting the cancel at the top ofpublish_diagnosticsnow fails it.Shutdownarm clears the schedule;test_shutdown_drops_pending_passes.didChangewithoutdidOpen: restored main's immediate publish from the given text;test_did_change_without_open_publishes_from_given_text.Review round 2
shutdown:handle_notificationnow ignores everything butexitonceshutdown_requestedis set (LSP 3.17 §Lifecycle), so a latedidOpen,didChangeor watched-file change can neither schedule nor publish a pass. Combined with the cleared schedule, nothing is published betweenshutdownandexit.test_notifications_after_shutdown_are_ignoredpins it (and thatexitis still handled); disabling the guard fails it.lsp-support.mdnow says adidChangefor a document with no buffer (non-file URI, or no priordidOpen) still publishes immediately, and thatshutdowndrops pending passes and ignores document notifications.Closes #2940
🤖 Generated with Claude Code