Repository navigation
Backlog
Labels
Description
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
completionSuggestionsRequestedstarts the.completiontrace and then sleepsdebounceNanoseconds, 50,000,000 ns, on every request (apple/TablePro/Views/Editor/QueryCompletionAdapter.swift:59,:119-125).refreshItemsgoes 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 inshowCompletionsandrefreshItems(SuggestionViewModel.swift:100-115,:293-305). SQLCompletionServiceis@MainActor(apple/TablePro/Core/Autocomplete/SQLCompletionService.swift:6-7). TableProCore builds withNonisolatedNonsendingByDefault(apple/Packages/TableProCore/Package.swift:7), soSQLCompletionProvider.completionSessionruns on the caller's actor, andSQLContextAnalyzer.analyzeruns on the main thread (apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLCompletionProvider.swift:60-81). Only the window cut andrankedSessionare@concurrent.- Each
await catalog...ingetCandidateshops to theSQLSchemaProvideractor 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 throughSQLStatementCursorIndex(apple/TablePro/Views/Editor/StatementRunController.swift:235,:246-263). - Each request bridges the whole text storage to a
Stringon 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:
suppressesAfterStatementBreakcopies 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.cutcopies 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:
- Make the request pipeline one
@concurrentfunction over immutable inputs: the cut window, the statement hint and aSendablecatalog 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. - 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. - Make the remaining per-request scans bounded: scan forward over whitespace in
suppressesAfterStatementBreak, fall back to a backward scan bounded bySQLCompletionWindow.radiuswhen no statement hint exists, and resume MongoDB analysis from a lexer checkpoint before the caret. - 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
refreshIfOvertakenandlastDroppedMovePresentspaths. CheckTask.isCancelledbetween the expensive steps. - Begin the
.completiontrace at the key event inSuggestionTriggerCharacterModel, so it covers the whole interval.
Measure
InteractionTracer.completioninterval (begin at the key, end atcompletionWindowDidShow,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-1900kafter 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
datlechin commentedon Nov 26, 2026
#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:
Sendablecatalog snapshot. Each catalog read is still anawaiton theSQLSchemaProvideractor. 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.SQLCompletionWindow.cutstill copies the document twice and scans from offset 0. That runs off main now, but it is still linear in the document.MongoContextAnalyzerstill 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.