Skip to content

perf(editor): lay out only the first screen of a long script and keep typing cost flat - #4428

Open
datlechin wants to merge 5 commits into
mainfrom
perf/editor
Open

datlechin wants to merge 5 commits into
mainfrom
perf/editor

Conversation

@datlechin

Copy link
Copy Markdown
Member

Summary

  • E1: a text view lays out only its first band when it is built around its text, and the reuse queue keeps no more hidden views than the pass left in use. A long script in a window's first editor no longer lays out every line.
  • E2: AXNumberOfCharacters answers the UTF-16 length, the unit every other range the editor answers is in, instead of walking graphemes.
  • E5: with the find panel hidden and unfocused, an edit, an undo or a replaced document only records the caret; Find Next, Previous, Replace, All, the highlights and showing the panel search once first, from that caret.
  • E3: completion reads the caret's statement from the index the statement band keeps (SQL model, band on, within 2,000,000 units, same grammar), and the window cut reads only that statement up to the cut's end. The request's text is the storage's bridged snapshot, not a second copy.
  • E3b: where the band does not keep the index (band off, or past 2,000,000 units), completion warms it off the main thread on the same snapshot and keeps it for the next request, trimmed to any edit made meanwhile.
  • E4: Cmd-Return, Explain and the AI actions take the caret's statement from the mounted editor's index when that editor holds exactly the tab's text and splits it with the same grammar and model; otherwise the text is scanned as before.

Measured on Release builds, arm64, base 9d0d481cf1 against this branch. Instruction and CPU counts are the load-robust rows; machine load was 8 to 12 throughout.

Item Row Before After
E1 restore-67048 peak footprint, main CPU, fragment views 6,737 MB (cap), 42.3 s, 67,048 215 MB, 1.1 s, 40
E1 open-firsttab-67048 peak, main CPU 12,140 MB (cap), 44.8 s 215 MB, 0.34 s
E1 restore-20000 main CPU from first frame to first free turn 3.7 s, 3.9 s 0.57 s
E1 restore-20000 peak, fragment views at the end 384 MB, 9,926 270 MB, 46
E1 restore-5000 main CPU from first frame, interleaved 705 ms 354 ms
E1 json-1000, json-5000, json-10000 main CPU 1.79 s, 19.0 s, 81.4 s (cap) 0.19 s, 0.72 s, 0.22 s
E1 json-5000 peak footprint 933 MB 269 MB
E2 AXNumberOfCharacters main CPU at 490k chars 47.5 ms, 56.0 ms 0.012 ms
E3 type-490k instructions per key 380 M, 389 M 110 M
E3 type-490k main-thread malloc per key 10.8 MB 3.4 MB
E3 type-510k instructions per key 313 M, 308 M 131 M
E3 type-10k instructions per key 88 M, 82 M 87 M, 85 M
E3b type-10mb instructions per key (TablePlus 291 M) 1.92 G with E1 to E5 (base cannot open the tab) 126 M
E3b type-10mb process CPU per key 86 ms with E1 to E5 19 ms
E4 run-at-cursor-5000lines Cmd-Return main CPU, two presses 16.7 ms, 6.6 ms 4.0 ms, 2.2 ms
E4 run-at-cursor-10mb Cmd-Return main CPU, two presses base cannot open the tab 346 ms (cold index), 47 ms
E5 type-5000lines-findset instructions per key, term kept, panel hidden 301 M 84 M
E5 type-5000lines-findset main CPU per key, turns over 50 ms 39.3 ms, 5 20.4 ms, 0
E5 type-10mb-findset main CPU per key base cannot open the tab 5.3 ms

The restore_* rows of the editor dimension measure from the first frame, which this branch draws earlier, so they are left out. Small restores interleaved at the same load: restore-500 787 ms against 672 and 859 ms, restore-1000 662 against 601 ms of main CPU.

Tests changed on purpose:

  • EmphasisGeometryUpdateTests.makeTallTextView: comment only, since init no longer lays out the whole text. No assertion moved.
  • Signature only: the completion stubs in QueryCompletionAdapterLifecycleTests and the completions(in:at:) callers in SQLCompletionServiceEmptyPrefixTests and QueryCompletionRankingTests pass statement: nil; the stubs answer statementGrammar.

New tests: ViewReuseQueueTests, TextViewControllerOpeningLayoutTests, FindPanelHiddenSearchTests, SQLCompletionServiceStatementHintTests, StatementAtCaretRoutingTests, plus cases in TextLayoutManagerFragmentViewTests, AccessibilityTests, SQLCompletionWindowTests, StatementRunControllerTests and QueryCompletionAdapterLifecycleTests.

Deliberately not fixed here:

  • E6a, find-field search off the main thread (gated on the findset rows): the search is a small share of a find-field key. type-5000lines-findset costs 76 ms of main CPU a find-field character, of which the regex is about 4 ms; at 10 MB, 713 ms a character with the regex at 86 to 111 ms. The rest is one emphasis layer per match (18,569 for t at 5000 lines), which E6b removes. E6a alone would not make the field responsive, so it follows E6b.
  • E6b, layers only for laid-out emphases (gated on the findset footprint): the harness records no footprint after the find term, so the gate cannot be read. The CPU rows above point at it as the next PR; it changes one manager under brackets, snippet fields, diagnostics and find.
  • 12e, the 67 M per key below 500,000 characters: no longer there. type-490k 110 M and type-510k 131 M against type-10k 87 M, so there is no frame to name.
  • restore_* and run-at-cursor totals to idle include the query run and the result grid, which moved on main since the base; only the synchronous Cmd-Return action is compared.

Test plan

  • verify.sh test on TableProTextEngineTests, TableProEditorKitTests and 15 app suites: 988 + 24 + 99 cases pass.
  • verify.sh package TableProCore for SQLCompletionWindowTests and SQLStatementCursorIndexTests: pass.
  • Codex review and adversarial review against origin/main: no findings.

@datlechin
datlechin enabled auto-merge (squash) November 23, 2026 06:27

This branch has not been deployed

No deployments
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.

1 participant