Skip to content

perf(editor): bound the memory each document's undo history holds - #4584

Merged
datlechin merged 3 commits into
mainfrom
perf/editor-undo-budget
Nov 26, 2026
Merged

datlechin merged 3 commits into
mainfrom
perf/editor-undo-budget

Conversation

@datlechin

Copy link
Copy Markdown
Member

Summary

  • CEUndoManager now holds at most maximumHistoryBytes (32 MiB by default) across the undo and redo stacks. Each step is charged the UTF-16 text its mutations and their inverses keep, plus the mutation itself.
  • A new step that goes over the budget drops the oldest undo steps first, then the redo step furthest from the text. The newest step always stays, even when it alone is larger, so the edit just made can be undone. This is levelsOfUndo measured in bytes, so one Format Query cannot outweigh thousands of keystrokes.
  • restore(_:) trims to the same budget, so a hidden query tab's history is bounded the same way when it comes back. History.bytes reports the charge, for the app-wide budget once perf(memory): one app-wide budget for results held off screen #4556 lands.

Measured with a local Swift Testing probe (debug build, not committed): 30 whole-document replaces of a 1.9M-character script, as 30 Format Query runs.

Undo steps kept phys_footprint growth
main 30 60.5 MB
this PR 4 12.2 MB

Test plan

  • UndoBudgetTests (new): steps past the budget drop the oldest and the newest still undo in order with the bytes under the budget; a step larger than the budget is kept and undoes; a whole-document replace is charged both texts; the redo stack counts and a restored history is trimmed to the budget; a budget of zero keeps every step.
  • verify.sh package TableProEditor 'Undo|Composition|TextViewController|SourceEditor': 163 passed.
  • The Codex review pass did not run: the Codex account is at its usage limit until 27 Nov 2026 17:42. It needs a re-run before merge.

Closes #4538

The undo and redo stacks together hold at most a byte budget, 32 MiB by
default, charged with the UTF-16 text each mutation and its inverse keep.
A new step that goes over it drops the oldest steps first, then the redo
step furthest from the text; the newest step always stays. A history
restored into the editor, as a hidden query tab's is, is trimmed the same
way.
@datlechin
datlechin enabled auto-merge (squash) November 26, 2026 01:51
@datlechin
datlechin merged commit 4643c81 into main Nov 26, 2026
11 checks passed
@datlechin
datlechin deleted the perf/editor-undo-budget branch November 27, 2026 16:37
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.

perf(editor): bound the memory each document's undo history holds

1 participant