Skip to content

perf(editor): keep statement boundaries in an index that edits update #4534

Done

Description

@datlechin

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

  • scheduleControlsRefresh runs on every edit (apple/TablePro/Views/Editor/SQLEditorCoordinator.swift:253). Up to 64,000 units it calls refreshControls on 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).
  • refreshControls copies the document and runs QueryStatementScanner.navigableStatements over all of it, then assigns runnableStatements wholesale, 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 long INSERT ... VALUES statement, 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 from apple/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 @concurrent locate path.
  • 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: scheduleControlsRefresh and 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

  1. added
    enhancement
    platform-macos
    on Nov 25, 2026
  2. datlechin commented on Nov 25, 2026

    @datlechin
    MemberAuthor

    #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:

    • Resync after an edit. SQLStatementCursorIndex.textDidChange still 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.
    • Checkpoints inside a long statement. A checkpoint holds the last ended statement and the client delimiter only. Resuming inside a statement also needs the boundary tracker's own state (any SQLStatementBoundaryTracking: open blocks, BEGIN ... END depth, held ;), so each tracker needs a snapshot type first. Until then a caret inside one 1M-unit INSERT ... VALUES still scans that statement per lookup, which is the "edit inside one 1M-unit statement" case of the Measure section.
    • The gutter asking only for the drawn range. The ribbon still receives the whole [StatementRun] array; that is cheap once the scan is off the main thread, and becomes a query once the index above exists.
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