Skip to content

perf(autocomplete): run completion analysis off the main thread and drop the 50 ms wait #4539

Backlog

Description

@datlechin

Problem

The completion popup took about 94 ms from the key to the first frame that shows it. 50 ms of that is a fixed sleep before any work starts. The sleep cannot simply go: it is also what coalesces a burst of keys today, and the work behind it runs partly on the main thread. Every typed letter or digit asks for completions (apple/Packages/TableProEditor/Sources/TableProEditorKit/CodeSuggestion/Model/SuggestionTriggerCharacterModel.swift:44-56), so without the sleep that main-thread work would land on every key.

Cause

  • completionSuggestionsRequested starts the .completion trace and then sleeps debounceNanoseconds, 50,000,000 ns, on every request (apple/TablePro/Views/Editor/QueryCompletionAdapter.swift:59, :119-125). refreshItems goes through the same call, so a refresh of an open popup over a truncated pool waits too.
  • While a request is in flight and the popup is not shown, a new key is dropped (apple/Packages/TableProEditor/Sources/TableProEditorKit/CodeSuggestion/Model/SuggestionViewModel.swift:284-287), and the request reads the live caret after the sleep (QueryCompletionAdapter.swift:129). So the sleep is the coalescing window. Cancellation and generation fencing exist only in showCompletions and refreshItems (SuggestionViewModel.swift:100-115, :293-305).
  • SQLCompletionService is @MainActor (apple/TablePro/Core/Autocomplete/SQLCompletionService.swift:6-7). TableProCore builds with NonisolatedNonsendingByDefault (apple/Packages/TableProCore/Package.swift:7), so SQLCompletionProvider.completionSession runs on the caller's actor, and SQLContextAnalyzer.analyze runs on the main thread (apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLCompletionProvider.swift:60-81). Only the window cut and rankedSession are @concurrent.
  • Each await catalog... in getCandidates hops to the SQLSchemaProvider actor and back to main, so one request re-enters the main queue several times, each time behind the keystroke's own layout and drawing.
  • The statement locator is typed @MainActor (QueryCompletionAdapter.swift:52-55). Its fast path, maintainedStatement, scans on main through SQLStatementCursorIndex (apple/TablePro/Views/Editor/StatementRunController.swift:235, :246-263).
  • Each request bridges the whole text storage to a String on main (QueryCompletionAdapter.swift:133). The comment there says the copy shares the storage's buffer; for a mutable backing store the bridge copies (see perf(editor): stop copying the whole document on every keystroke and caret move #4533).
  • Smaller whole-document work per request: suppressesAfterStatementBreak copies everything after the caret to test for trailing blanks, on main, when the previous character is ; or a newline (apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLCompletionTriggerPolicy.swift:23-33). When the locator returns nil, SQLCompletionWindow.cut copies the document twice and scans from offset 0, off main (apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLCompletionWindow.swift:19-37). MongoDB analysis scans from offset 0 to the caret on every request, off main (apple/TablePro/Core/Autocomplete/Mongo/MongoCompletionService.swift:57-58, apple/Packages/TableProCore/Sources/TableProAutocomplete/Mongo/MongoContextAnalyzer.swift:106-109).

Change

In this order:

  1. Make the request pipeline one @concurrent function over immutable inputs: the cut window, the statement hint and a Sendable catalog snapshot that the schema provider publishes when its lists change. Analysis, candidate collection and ranking run off main; the request returns to main once, to present. Catalog reads not yet cached start in the background and refresh the popup when they land.
  2. Locate the caret's statement off main on the request's snapshot. Read the typed prefix with textStorage.mutableString.substring(with:) instead of bridging the whole storage.
  3. Make the remaining per-request scans bounded: scan forward over whitespace in suppressesAfterStatementBreak, fall back to a backward scan bounded by SQLCompletionWindow.radius when no statement hint exists, and resume MongoDB analysis from a lexer checkpoint before the caret.
  4. Then remove the sleep. Start the request on the key, keep one request in flight and re-ask for the newest caret when it finishes (latest wins), using the existing refreshIfOvertaken and lastDroppedMovePresents paths. Check Task.isCancelled between the expensive steps.
  5. Begin the .completion trace at the key event in SuggestionTriggerCharacterModel, so it covers the whole interval.

Measure

  • InteractionTracer .completion interval (begin at the key, end at completionWindowDidShow, QueryCompletionAdapter.swift:206-207): median of 50 popups against about 94 ms today. Target: the 50 ms sleep gone from the interval.
  • Time Profiler, main thread only, while typing SELECT a.| FROM orders a JOIN customers c ON | repeatedly: main-thread ms per request, before and after.
  • typing-1900k after step 4: main-thread CPU per key must not rise above today's 12.7 ms.
  • Holding a key down: completion requests per second stay bounded.

Part of the performance plan.

Activity

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

    @datlechin
    MemberAuthor

    #4601 does steps 4 and 5, most of step 1 and part of steps 2 and 3. The request no longer waits 50 ms. Analysis, candidate collection, the catalog reads and ranking run off the main thread. An open popup runs one refresh at a time, and the trace begins on the key. Main-thread CPU per request fell from about 0.6 ms to under 0.07 ms in a Debug benchmark with 5,002 tables and a 180k-character script.

    Three parts of the plan are still open:

    • A Sendable catalog snapshot. Each catalog read is still an await on the SQLSchemaProvider actor. It now hops from the background, not from main, so it no longer queues behind layout and drawing. A snapshot that the provider publishes when its lists change would remove the hops, and with them the background refresh when a read lands late.
    • A bounded backward cut when there is no statement hint. When the locator returns nil, SQLCompletionWindow.cut still copies the document twice and scans from offset 0. That runs off main now, but it is still linear in the document.
    • A MongoDB lexer checkpoint. MongoContextAnalyzer still scans from offset 0 to the caret on every request, off main.

    The statement locator stays @MainActor. Its maintained path scans no characters per request (longScriptCompletionReadsTheBandStatement). The request keeps its one document copy, as #4533 decides.

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