Skip to content

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

Done

Description

@datlechin

Problem

The SQL editor's undo history has no bound. Every edit group stays until the document is replaced, and hidden query tabs keep their whole history while another tab is shown. A Format Query or another whole-document replace keeps the entire old text as one undo step. Over a long session with large scripts, undo is memory that only grows. No session footprint is measured for it yet.

Cause

  • CEUndoManager keeps undoStack and redoStack as plain arrays with no level or byte limit; push appends a group or a mutation and nothing ever trims the front (apple/Packages/TableProEditor/Sources/TableProTextEngine/Utils/CEUndoManager.swift:49-51, :262-277). The editor never sets levelsOfUndo, unlike the structure and principal change managers (apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift:85).
  • Each mutation stores its inverse with the replaced text (CEUndoManager.swift:228-234). Format Query replaces the whole document when nothing is selected (apple/TablePro/Views/Editor/SQLEditorCoordinator.swift:547), and so does replaceDocumentText (apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+UndoHistory.swift:35-53); each keeps the whole old document as one inverse.
  • On a tab switch, takeUndoHistory moves the stacks out of the editor together with a copy of the text (TextViewController+UndoHistory.swift:16-22), and EditorUndoHistories keeps one entry per open tab until the tab closes (apple/TablePro/Views/Editor/EditorUndoHistories.swift:19-38).

Change

  • Give each document's undo history a byte budget, charged with the inserted and inverse text of every mutation. When a new group goes over it, drop the oldest groups first. This is the same model as NSUndoManager.levelsOfUndo, measured in bytes instead of steps, so one large replace cannot outweigh thousands of small edits.
  • Charge the histories of hidden tabs to the same per-document budget, and to the app-wide memory budget once perf(memory): one app-wide budget for results held off screen #4556 lands.
  • Keep the redo stack under the same budget.

Measure

  • Unit test: push groups past the budget and check the oldest are dropped, the newest still undo in order, and the bytes held stay under the budget.
  • Allocations after 30 Format Query runs on a 1.9M-character script and after a long typing session: persistent bytes held by undo groups, before and after.
  • phys_footprint with 20 query tabs of 1 MB each that each saw a Format Query, before and after.

Part of the performance plan.

Activity

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementplatform-macos

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions