Skip to content

perf(lsp): O(doc) semantic token encoding + delta + cancellation#26

Merged
gandarfh merged 1 commit into
mainfrom
perf/semantic-delta-cancel
Jun 13, 2026
Merged

perf(lsp): O(doc) semantic token encoding + delta + cancellation#26
gandarfh merged 1 commit into
mainfrom
perf/semantic-delta-cancel

Conversation

@gandarfh

Copy link
Copy Markdown
Member

Slice 2.5 Fase 3. The baseline flagged semanticTokens/full on the large fixture at ~70ms p95 -- the one operation near an ADR-010 budget.

Root cause (the real win): the cost was never token computation (Semantic_tokens.of_blocks is 0.83ms) -- it was the LSP delta-position encoding calling Doc_position.of_offset (O(offset), counts lines from the document start) once per token, i.e. O(n2) over the document. Replaced with a single forward cursor over the sorted tokens (O(doc)). semanticTokens/full [large]: 69.7ms -> 1.77ms p95 (40x).

semanticTokens/full/delta (LSP 3.17): server stores the last (resultId, data) per document and returns a minimal prefix/suffix edit list; falls back to a full result on a stale/unknown previousResultId (spec-allowed). Saves transport + client re-tokenization for future token-consuming clients.

Cancellation: a dedicated reader thread parses incoming messages and records $/cancelRequest ids; the main loop skips a stale request before doing its work and answers RequestCancelled. Processing stays single-threaded (only the main loop touches docs/stores/stdout). With no request now exceeding ~2ms, the pre-process check is sufficient -- no mid-flight interruption needed.

Tests: 5 new protocol checks (full+resultId, delta no-change -> empty edits, delta after edit -> non-empty edits, cancel -> error/no result, no cancel bleed). 112 total green. The existing semantic-token correctness tests cover the new encoding. CI budget gate stays green.

Note: the desktop does not consume server semantic tokens (Lezer first-paint), so delta benefits future IDE clients; the encoding fix + cancellation ship in the bundled binary regardless.

@gandarfh
gandarfh merged commit ad5c45b into main Jun 13, 2026
9 checks passed
@gandarfh
gandarfh deleted the perf/semantic-delta-cancel branch June 13, 2026 16:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant