Skip to content

perf(editor): answer attributedString() with the text storage - #4562

Merged
datlechin merged 2 commits into
mainfrom
perf/editor-document-copies
Nov 26, 2026
Merged

datlechin merged 2 commits into
mainfrom
perf/editor-document-copies

Conversation

@datlechin

@datlechin datlechin commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Summary

  • NSTextInputClient.attributedString() returns the text storage itself, as NSTextView does, instead of an attributed substring of the whole document.
  • Length-only reads on the keystroke and caret path read textStorage.length: the run controls' refresh, the statement band (its size guard now runs before the text is read), the pending-caret hand-off, diagnostics and the inline suggestion check.
  • No new read accessor. Measured on this Mac, bridging the storage to a String is not a copy of the document: NSBigMutableString shares its buffer and copies one block when the next edit touches it. The fold pass, the binding writeback and the scans keep that snapshot, which they rely on to stay unchanged while the editor goes on editing. A live NSMutableString accessor would give that up and save nothing.

Measured with a 1.9M-character storage with token-level attribute runs, typing at the end, the read held across the edit (swiftc -O probe, 50 keys):

Read Before After
attributedString() 40.9 ms and 5.7 MB per call the storage, no copy
textStorage.string held across an edit 0.002 ms, at most 9 KB unchanged
textStorage.mutableString 0.002 ms, 96 B not used

So the per-key copies the issue lists cost microseconds each, and the only whole-document copy among them was attributedString().

Test plan

  • TextStorageReadTests (TableProTextEngineTests): attributedString() is the storage (fails on main); reading TextView.string on a 4M-unit document and editing after it grows the heap by under 2 MB, where a copy is 8 MB; the read stays a snapshot after a later edit.
  • verify.sh package TableProEditor over the input, marked text, IME and text view suites: 38 passed. verify.sh test StatementRunControllerTests QueryDiagnosticsRefreshTests InlineSuggestionManagerFocusTests SQLEditorPendingTextTests for the app-side length reads: 42 passed.

Closes #4533

The input system's attributedString() copied every character and attribute
run of the document, 41 ms and 5.7 MB at 1.9M characters. It now returns the
storage, as NSTextView does.

The other reads the issue lists are not copies: bridging the storage's
NSBigMutableString to a String shares its buffer and copies one block when
the next edit touches it, about 2 us. Length-only reads on the keystroke
path read textStorage.length, and the band checks its size limit first.
A test pins that the bridged read neither copies the document nor changes
after a later edit, which the fold pass and the binding rely on.
@datlechin
datlechin enabled auto-merge (squash) November 26, 2026 01:50
@datlechin
datlechin merged commit b2791c6 into main Nov 26, 2026
11 checks passed
@datlechin
datlechin deleted the perf/editor-document-copies 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): stop copying the whole document on every keystroke and caret move

1 participant