Repository navigation
Done
Labels
Description
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,
TextSelectionManagercallsdelegate?.setNeedsDisplay()(apple/Packages/TableProEditor/Sources/TableProTextEngine/TextSelectionManager/TextSelectionManager.swift:201-202), which the text view implements assetNeedsDisplay(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.drawfills the dirty rect and then draws the statement band (apple/Packages/TableProEditor/Sources/TableProEditorKit/SupportingViews/SourceEditorTextView.swift:59-67).drawStatementHighlightasks 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 oneINSERTspans 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'sboundsDidChangeobserver (apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+Lifecycle.swift:100-107). drawLineNumbersbuilds anNSAttributedStringand aCTLinefor every visible number on every draw (apple/Packages/TableProEditor/Sources/TableProEditorKit/Gutter/GutterView.swift:490-526).highlightedLineIDswalks 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.drawSelectedLinesdraws the caret-line band only when that line is invisibleTextRange(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
frameDidChangeNotificationfor 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
NSTextViewinvalidates only the insertion-point line. - Bound the statement band to the dirty rect: intersect the statement range with
layoutManager.textRange(covering: dirtyRect)first, asTextSelectionManager.fillRectsalready 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 theboundsDidChangeredraw. On an edit, invalidate the caret line, or from the edited line down when the line count changes. BoundhighlightedLineIDsto the lines in the dirty rect. - Cache shaped line numbers (keyed by number and highlighted state) instead of a new
CTLineper 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.drawagainst 80 ms andSourceEditorTextView.drawagainst 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.drawandhighlightedLineIDs. - Animation Hitches template on the same scroll.
Part of the performance plan.
Activity
datlechin commentedon Nov 25, 2026
#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:
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.onSetNeedsDisplayand does not opt into responsive scrolling, so that wants a look on screen (Quartz Debug flashing) before it goes.frameDidChangeNotification50 ms after an edit (SQLEditorCoordinator.swift). Besides the gutter and the ribbon,VisibleRangeProviderlistens to it and refreshes the highlighter'svisibleSet, 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.