Repository navigation
Done
Labels
Description
Problem
The gutter's run controls rescan every statement in the document after each typing pause. In the 1.9M-character file, StatementRunController.scheduleControlsRefresh took 664 ms of main-thread CPU over about 420 keys (Time Profiler, typing-1900k). The code's own comment puts one full pass at 6 ms at 190 KB and 31 ms at the 2,000,000-unit limit, so each pause in a large script is a main-thread stall a following key waits behind.
Cause
scheduleControlsRefreshruns on every edit (apple/TablePro/Views/Editor/SQLEditorCoordinator.swift:253). Up to 64,000 units it callsrefreshControlson the keystroke itself; above that it waits 150 ms and then makes the same full pass on the main thread (apple/TablePro/Views/Editor/StatementRunController.swift:33-38,:137-151).refreshControlscopies the document and runsQueryStatementScanner.navigableStatementsover all of it, then assignsrunnableStatementswholesale, so the ribbon redraws (apple/TablePro/Views/Editor/StatementRunController.swift:154-166).- The caret's statement band uses
SQLStatementCursorIndex. It resumes from the last recorded checkpoint before the caret, but checkpoints are recorded only at statement ends at least 4,096 units apart, and the scan runs on to the end of the caret's statement (apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLStatementCursorIndex.swift:6,:31-57;apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLStatementScan.swift:320-353). Inside one longINSERT ... VALUESstatement, every key scans that whole statement. textDidChange(at:)drops every checkpoint at or after the edit (SQLStatementCursorIndex.swift:25-29). Nothing re-synchronizes after the edit, so the next lookup rescans forward from the edit.- Keyword auto-case answers "is the caret in a literal or comment" by lexing from offset 0 to the caret on every word boundary, on the main thread, in documents under 500,000 units (
apple/Packages/TableProCore/Sources/TableProAutocomplete/KeywordUppercaseHelper.swift:22-40, called fromapple/TablePro/Views/Editor/SQLEditorCoordinator.swift:852-866).
Change
- Keep statement boundaries in one offset-keyed index per document, updated by edits. On an edit at L, rescan from the boundary before L and stop when a boundary matches an old one shifted by the edit's delta in the same lexer state.
- Record checkpoints inside long statements too (at token boundaries outside literals and comments), so a caret lookup inside a huge statement scans a bounded window.
- The gutter asks the index only for the statements that intersect the drawn range, instead of materializing every statement after each pause.
- Any full scan that remains (first open, a whole-document replace) runs on an immutable snapshot off the main actor and posts the result, through the existing
@concurrentlocatepath. - Keyword auto-case reads the literal or comment state at the caret from the same index instead of lexing from the start.
Measure
- Extend
apple/TableProTests/Views/Editor/StatementRunPerformanceGuardTests.swift: assert the units scanned per single-character edit stay bounded (for example under 8,000) in a 2,000,000-unit document, including an edit inside one 1M-unit statement. - Re-run
typing-1900k:scheduleControlsRefreshand the refresh task against 664 ms over about 420 keys; target near zero on the main thread. - Hangs instrument while typing in the 1.9M file with 300 ms pauses: count main-thread stalls over 16 ms before and after.
Part of the performance plan.
Activity
datlechin commentedon Nov 25, 2026
#4571 ships the parts that stand alone: the controls' full scan above the synchronous limit runs off the main actor on a snapshot and is dropped if an edit lands first, and keyword auto-case reads from the caret statement's start instead of from offset 0.
Left for a follow-up, each larger than this item:
SQLStatementCursorIndex.textDidChangestill drops every checkpoint at or after the edit, and nothing re-synchronizes. Stopping at the first boundary that matches an old one shifted by the delta needs the old boundary list kept alongside the checkpoints, with the lexer state at each.any SQLStatementBoundaryTracking: open blocks,BEGIN ... ENDdepth, held;), so each tracker needs a snapshot type first. Until then a caret inside one 1M-unitINSERT ... VALUESstill scans that statement per lookup, which is the "edit inside one 1M-unit statement" case of the Measure section.[StatementRun]array; that is cheap once the scan is off the main thread, and becomes a query once the index above exists.