Skip to content

P2 Replace All in a large file freezes the renderer ~2s after the edit: TextMate setTokensAndStates recounts EOLs of all pending changes twice per worker reply #350739

Todo

Description

Summary

Replace All in a large TypeScript file blocks the renderer for about 2 s after the edit is applied. The time goes to TextMateWorkerTokenizerController.setTokensAndStates. Every queued worker reply that arrives while the Replace All change is still pending recomputes linesLengthEditFromModelContentChange(...) for all pending changes, and it does so twice. Each computation runs countEOL over the full replacement text, which here is the whole 3.9 MB file. In this run, 51 such replies cost about 33 ms each, roughly 1.7 s of main-thread time, for work whose result is the same each time.

Environment

  • VS Code Insiders 1.149.0-insider (commit e8b6d8b), Linux x64, 8 CPUs, 31 GB. Software rendering on a shared VM, so absolute timings are noisy; the call structure is not.
  • Signed out, default settings, no extensions besides the built-ins.
  • Code pointers are from main at cb7b13b (2026-11-25).

Steps to reproduce

  1. Generate the test file (100,000 lines, 3.89 MB, 60,000 occurrences of value):
    open('huge.ts', 'w').write(''.join(f"export class Widget{i} {{\n\tprivate value{i}: number = {i};\n\tcompute(x: number): number {{ return x * this.value{i} + {i}; }}\n\tget label(): string {{ return `widget-{i}-${{this.value{i}}}`; }}\n}}\n" for i in range(20000)))
  2. Open huge.ts and wait for tokenization to finish.
  3. Press Ctrl+H, find value, replace with vAl. The match count shows 19999+.
  4. Click Replace All.

The editor stays unresponsive for 2.7–4.8 s across runs. The DevTools Performance panel shows one long task of 1.8–2.1 s for the replace itself, followed by a second burst of about 2 s of $setTokensAndStates handlers.

Measurements (CPU profile of one Replace All)

Function Time
FindModelBoundToEditorModel.replaceAll (whole command) 1,317 ms
_largeReplaceAll (getValue + regex replace + _executeEditorCommand) 490 ms
pushEditOperations 315 ms
TextMateWorkerTokenizerController.$setTokensAndStates 1,798 ms across 232 separate handler runs
↳ countEOL (self) 1,767 ms
↳ via linesLengthEditFromModelContentChange 1,649 ms (834 ms from the tokens fromMany, 807 ms from the states fromMany)
GC during the operation about 1.3 s
Bracket pair colorizer ListAstNode.create23 733 ms

There were 232 $setTokensAndStates runs in total. 56 ran before Replace All and 176 after it. Of those 176, 125 were cheap (99 ms combined). 51 were expensive: 1,662 ms combined, about 33 ms each (16–90 ms). They are the replies that arrive while the Replace All change is still in _pendingChanges. The worker kept tokenizing the old version and posting replies (about one every 20 ms or 200 lines) while the renderer was blocked for 1.3 s by Replace All. Once Replace All finishes, those queued replies are handled one after another, each with the same 3.9 MB pending change.

Root cause

src/vs/workbench/services/textMate/browser/backgroundTokenization/textMateWorkerTokenizerController.ts, in setTokensAndStates:

  • Line 158: MonotonousIndexTransformer.fromMany(this._pendingChanges.map(c => linesLengthEditFromModelContentChange(c.changes))), used to filter and shift tokens.
  • Line 188: the same expression again, unconditionally, to compute curToFutureTransformerStates.
  • linesLengthEditFromModelContentChange (src/vs/editor/common/model/textModelStringEdit.ts:52-61) calls countEOL(c.text) for every inner change, then LengthEdit.compose. For a Replace All whose text is the whole file, this scans 3.9 MB each time.
  • Lines 178–183: when tokens survive the filter, every pending inner change is also applied to every token block with applyEdit. applyEdit calls countEOL(text) again (contiguousMultilineTokens.ts:111). Since this full-range edit touches every line, the filter drops all tokens in this workload, but with many smaller pending edits this loop is changes × token blocks × countEOL.
  • Line 185 / _stringEditFromChanges (line 231): getOffsetAt runs for every pending change on every reply.

The cost of each reply is O(size of all pending changes), and the result only changes when _pendingChanges changes. Every reply in a backlog pays it again, here 51 × 2 full-text scans.

Suggested fixes

  1. Compute the fromMany transformer once per setTokensAndStates call and reuse it for tokens and states. That alone halves the cost.
  2. Cache the LengthEdit of each pending change when it is pushed in onDidChangeContent (line 61–70), or cache the composed transformer until _pendingChanges changes. Then a backlog of N replies costs one countEOL per change instead of 2N.
  3. Rebase the tokens with the composed LengthEdit (or a composed StringEdit) instead of looping over pending changes × token blocks calling applyEdit, which re-counts EOLs.

Secondary observations (same profile, not the main issue)

  • _largeReplaceAll builds the whole replaced text and issues one full-range replace (490 ms). That full-range edit then invalidates bracket pairs (733 ms in create23), tokens, and decorations across the whole file.
  • Undo of the same Replace All took about 3.1 s.

Related

  • Performance Issues Large Find-Replace #318720, "Performance Issues Large Find-Replace", is about workspace-wide replace in the Search view across thousands of files, with didOpen/format floods to language servers. It is a different code path, so this is not a duplicate.

Evidence

  • The CPU profile of one Replace All was captured with dbgjs on the renderer (replace-all-huge-ts.cpuprofile) and is available on request. The steps above reproduce it in a few seconds.

🤖 Authored with GitHub Copilot on behalf of Benjamin Christopher Simmonds (@benibenj).

Metadata

Metadata

Labels

perftokenization

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions