Repository navigation
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
- 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)))
- Open
huge.tsand wait for tokenization to finish. - Press Ctrl+H, find
value, replace withvAl. The match count shows19999+. - 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) callscountEOL(c.text)for every inner change, thenLengthEdit.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.applyEditcallscountEOL(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):getOffsetAtruns 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
- Compute the
fromManytransformer once persetTokensAndStatescall and reuse it for tokens and states. That alone halves the cost. - Cache the
LengthEditof each pending change when it is pushed inonDidChangeContent(line 61–70), or cache the composed transformer until_pendingChangeschanges. Then a backlog of N replies costs onecountEOLper change instead of 2N. - Rebase the tokens with the composed
LengthEdit(or a composedStringEdit) instead of looping over pending changes × token blocks callingapplyEdit, which re-counts EOLs.
Secondary observations (same profile, not the main issue)
_largeReplaceAllbuilds the whole replaced text and issues one full-range replace (490 ms). That full-range edit then invalidates bracket pairs (733 ms increate23), 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).
Activity