Skip to content

perf(editor): stop copying the whole document on every keystroke and caret move #4533

Done

Description

@datlechin

Problem

Typing in a 1.9M-character script costs about 12.7 ms of main-thread CPU per key (5,341 ms over about 420 keys, Time Profiler, typing-1900k). Keystroke to first frame is 23.3 ms median and 33.2 ms p95 in that file. Part of the per-key cost is whole-document work that does not depend on what was typed: several callers turn the text storage into a Swift String on every keystroke or caret move, and each of those is a copy of the document.

Cause

TextView.string is a cross-module getter that returns textStorage.string as String (apple/Packages/TableProEditor/Sources/TableProTextEngine/TextView/TextView.swift:53-56). The storage holds a mutable string, so the bridge makes an immutable copy. The fold calculator relies on exactly that (apple/Packages/TableProEditor/Sources/TableProEditorKit/LineFolding/Model/LineFoldCalculator.swift:102-103). A later as NSString at the call site cannot undo a copy made inside the getter.

Copies on the per-key path, all on the main thread:

  • StatementRunController.scheduleControlsRefresh copies to read the length only (apple/TablePro/Views/Editor/StatementRunController.swift:141). It runs on every edit (apple/TablePro/Views/Editor/SQLEditorCoordinator.swift:253).
  • highlightRange copies before its size guard (apple/TablePro/Views/Editor/StatementRunController.swift:368-369). It runs on every selection change, so on every key (apple/TablePro/Views/Editor/SQLEditorCoordinator.swift:283).
  • passPendingCaretOn copies to compare lengths on every cursor-state change (apple/TablePro/Views/Editor/SQLEditorController.swift:296).
  • TextBindingSync.editorTextDidChange copies for the binding while the document is 500,000 units or less (apple/Packages/TableProEditor/Sources/TableProEditorKit/SourceEditor/TextBindingSync.swift:51-54).
  • The fold pass copies on every edit up to 64,000 units, and 150 ms after the last edit above that (apple/Packages/TableProEditor/Sources/TableProEditorKit/LineFolding/Model/LineFoldCalculator.swift:118, apple/Packages/TableProEditor/Sources/TableProEditorKit/LineFolding/Model/LineFoldModel.swift:31, :284).

Copies on other paths:

  • Find, while the panel is shown: apple/Packages/TableProEditor/Sources/TableProEditorKit/Find/ViewModel/FindPanelViewModel+Find.swift:68.
  • Diagnostics, after each 500 ms pause, once to read text and once for the length: apple/TablePro/Views/Editor/QueryDiagnosticsController.swift:74, :94.
  • Inline suggestions copy to test for an empty document on every eligibility check, and again to build the request (apple/TablePro/Core/AI/InlineSuggestion/InlineSuggestionManager.swift:113, :129).
  • NSTextInputClient.attributedString() copies characters and attribute runs: apple/Packages/TableProEditor/Sources/TableProTextEngine/TextView/TextView+NSTextInput.swift:339-341. How often the input system calls it is not known.

The layout manager reads textStorage?.string as? NSString in rectForOffset, rectsFor(range:in:) and findOffsetAfterEndOf (apple/Packages/TableProEditor/Sources/TableProTextEngine/TextLayoutManager/TextLayoutManager+Public.swift:177, :241, :279). Whether that conditional cast copies is not verified. If it does, every caret placement and every statement band rect copies too.

Change

  • Give the text view one documented O(1) read accessor over the live backing store, textStorage.mutableString (an NSMutableString), and route reads through it.
  • Where only the length is needed, read textStorage.length or documentRange.length.
  • Replace the as? NSString casts in TextLayoutManager+Public.swift with textStorage?.mutableString.
  • Return textStorage from attributedString(), as NSTextView answers it.
  • Take a real copy only where the text must outlive later edits on another thread: the tree-sitter snapshot (perf(editor): parse edits off the main thread without copying the document #4535), the fold scan, the completion request and the binding writeback. Each of those stays one copy, not one per caller.

Measure

  • Instruments Allocations while typing 50 characters at the end of the 1.9M file: bytes from -[NSBigMutableString copyWithZone:] and __CFStringCreateImmutableFunnel3. Expect the per-key copies listed above to go to zero.
  • Re-run typing-1900k: main-thread CPU per key against 12.7 ms, and keystroke to first frame median and p95 against 23.3 / 33.2 ms.
  • A unit test on a 4M-unit storage that the new accessor and the length reads do not allocate.

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