Skip to content

perf(editor): redraw only the changed lines on a caret move or edit #4536

Done

Description

@datlechin

Problem

Every caret move, which includes every keystroke, invalidates the whole text view, and the gutter redraws every visible line number on every edit and every scroll frame. In the 1.9M-character file, display took 572 ms of main-thread CPU over about 420 keys, with GutterView.draw at 80 ms and SourceEditorTextView.draw at 33 ms (Time Profiler, typing-1900k). The cost follows the editor's pixel area and visible line count, not the document size, so tall panes and large displays pay most.

Cause

  • When a caret view moves, TextSelectionManager calls delegate?.setNeedsDisplay() (apple/Packages/TableProEditor/Sources/TableProTextEngine/TextSelectionManager/TextSelectionManager.swift:201-202), which the text view implements as setNeedsDisplay(bounds) (apple/Packages/TableProEditor/Sources/TableProTextEngine/TextView/TextView+TextSelectionManagerDelegate.swift:11-13). The view is layer-backed (apple/Packages/TableProEditor/Sources/TableProTextEngine/TextView/TextView.swift:346), so every backing tile is drawn again to move the current-line highlight. Only the old and new line rects changed.
  • SourceEditorTextView.draw fills the dirty rect and then draws the statement band (apple/Packages/TableProEditor/Sources/TableProEditorKit/SupportingViews/SourceEditorTextView.swift:59-67). drawStatementHighlight asks for the rects of the whole caret statement and only then filters by the dirty rect (SourceEditorTextView.swift:118-132). rectsFor(range:) walks every line of that statement (apple/Packages/TableProEditor/Sources/TableProTextEngine/TextLayoutManager/TextLayoutManager+Public.swift:268-269). A dump whose one INSERT spans 10,000 lines pays that per tile per key.
  • Each edit sets gutterView.needsDisplay = true (apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+TextViewDelegate.swift:22), and so does every scroll frame through the clip view's boundsDidChange observer (apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+Lifecycle.swift:100-107).
  • drawLineNumbers builds an NSAttributedString and a CTLine for every visible number on every draw (apple/Packages/TableProEditor/Sources/TableProEditorKit/Gutter/GutterView.swift:490-526). highlightedLineIDs walks every line of a ranged selection on every gutter draw (GutterView.swift:469-480), so after Select All, each scroll frame walks the whole document's lines.
  • drawSelectedLines draws the caret-line band only when that line is in visibleTextRange (GutterView.swift:428-433). That gate is why the per-scroll full redraw exists: dropping the redraw alone would leave the band missing when the caret line scrolls in.
  • 50 ms after each edit burst, the coordinator posts a frameDidChangeNotification for a frame that did not change (apple/TablePro/Views/Editor/SQLEditorCoordinator.swift:245-250). Its observers redraw the gutter and the folding ribbon and mark the scroll view for layout (TextViewController+Lifecycle.swift:124-136). Real frame changes already arrive from the text view (TextView.swift:350).

Change

  • Keep the previous and current caret-line and selection rects; on a caret move invalidate only the old and new rects, plus the old and new statement-band rects, as NSTextView invalidates only the insertion-point line.
  • Bound the statement band to the dirty rect: intersect the statement range with layoutManager.textRange(covering: dirtyRect) first, as TextSelectionManager.fillRects already does (apple/Packages/TableProEditor/Sources/TableProTextEngine/TextSelectionManager/TextSelectionManager+FillRects.swift:55-62).
  • In the gutter, test the caret-line band against the dirty rect instead of visibleTextRange, then drop the boundsDidChange redraw. On an edit, invalidate the caret line, or from the edited line down when the line count changes. Bound highlightedLineIDs to the lines in the dirty rect.
  • Cache shaped line numbers (keyed by number and highlighted state) instead of a new CTLine per number per draw.
  • Remove the synthetic frame-change notification.

Measure

  • Quartz Debug "Flash screen updates": today the whole editor and gutter flash per key; after, only the changed lines.
  • Re-run typing-1900k: GutterView.draw against 80 ms and SourceEditorTextView.draw against 33 ms over about 420 keys, and display time against 572 ms.
  • Time Profiler on a 5 s scroll of the 1.9M file after Select All: GutterView.draw and highlightedLineIDs.
  • Animation Hitches template on the same scroll.

Part of the performance plan.

Activity

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
  2. datlechin commented on Nov 25, 2026

    @datlechin
    MemberAuthor

    #4570 ships partial invalidation for caret moves, selection changes, the statement band and the gutter rows, the band bounded to the dirty rect, cached line numbers, and bounded selected-line IDs.

    Left, because each changes behavior I could not check from tests alone:

    • The gutter redraw on every scroll frame (TextViewController+Lifecycle.swift, setUpOnScrollChangeObserver). The caret band no longer depends on the visible range, so the gate that needed it is gone, and each of those redraws is now cheap. Removing the redraw relies on AppKit drawing the gutter's newly exposed rows by itself; the gutter is layer-backed with .onSetNeedsDisplay and does not opt into responsive scrolling, so that wants a look on screen (Quartz Debug flashing) before it goes.
    • The synthetic frameDidChangeNotification 50 ms after an edit (SQLEditorCoordinator.swift). Besides the gutter and the ribbon, VisibleRangeProvider listens to it and refreshes the highlighter's visibleSet, which an edit that moves the visible character range without changing the frame does not otherwise update. Removing it needs the provider to follow edits itself first.
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