Repository navigation
Done
Labels
Description
Problem
Wherever a table name can go (the start of a statement, after FROM, JOIN, INTO, DROP, CREATE VIEW), completion filters and fully sorts every table in the catalog on each request. The code's own measure is 4 ms for 5,000 candidates, about 0.8 us each, which makes about 8-15 ms at 10,000 tables and 80-150 ms at 100,000 tables per request. It runs off the main thread, so it shows as popup latency, not a hitch. Once the kept pool is cut at its size, every further key in the name runs the whole request again.
Cause
getCandidatesaddscatalog.tableCompletionItems(), every unqualified table, for.from,.join,.into,.dropObject,.createIndex,.createViewand.unknown, the clause at the start of every statement (apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLCompletionProvider.swift:188-189,:199-200,:437-457).rankedSessionjoins the segments into a new array, runsfilterByPrefixover all of them (anNSStringbridge and up to tworange(of:)calls per name, then a subsequence pass that allocates matched ranges), buildsenumerated().maptuples that copy every item, sorts all of them and keeps the firstlimit * 10(SQLCompletionProvider.swift:84-108,:740,:862-864,:120-121). An empty prefix (FROM |) skips the filter but still scores and sorts every table.- With the pool cut,
holdsEveryMatchis true only for the exact prefix it was ranked for, so a longer prefix asks again (apple/TablePro/Views/Editor/QueryCompletionAdapter.swift:22-26). - A request that will be suppressed does all the work first:
suppressesEmptyPrefixis checked only afterengine.getCompletionshas collected and ranked (apple/TablePro/Core/Autocomplete/SQLCompletionService.swift:75,:85). A space or comma typed at a statement start ranks the whole table list and throws it away. - Each table becomes a
SQLCompletionItemwith a freshUUID(), a lowercased filter string and an identifier array (apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLCompletionItem.swift:47-93,:82). The unqualified list is cached, but dropped on every table-list write and rebuilt on the next popup (apple/TablePro/Core/Autocomplete/SQLSchemaProvider.swift:481-484,:742-748): about 20-30 MB and 50-100 ms on the actor at 100,000 tables. - After
schema.in a table slot,knownTablesfilters every table and maps the matches to new items on every request, uncached (SQLSchemaProvider.swift:786-812). - In a column slot with an unresolved qualifier,
resolveAliaswalkstablesResolvableUnqualified, which filters and lowercases every table (SQLSchemaProvider.swift:650-682). - The spelling index is one dictionary entry and one single-element
Setper table, rebuilt after every table-list write (SQLSchemaProvider.swift:563-575). - Column items are rebuilt on every request:
columnCompletionItemsandallColumnsInScopemap the cachedColumnInfoto new items (SQLSchemaProvider.swift:850-859,:903-960). In anONclause each reference is built three times (SQLCompletionProvider.swift:565-575,:613-617). Any column-cache write clears the whole cached column list (SQLSchemaProvider.swift:65-66). This is linear in columns times references, about 1-5 ms for 3-4 joined tables of 200+ columns.
Change
- Build a compact name index once per table list, off the actor's request path: names, folded keys sorted once, schema ids and kinds in flat arrays, with the per-schema row ranges and the spelling lookup in the same structure.
- Rank without materializing: a prefix match outranks every contains or subsequence match, so binary-search the folded prefix range first and skip the slower passes when that range fills the pool. Keep only the best
poolrows in a bounded heap (the shapeTopRankedalready has inapple/Packages/TableProMacCore/Sources/TableProUtilities/UI/TopRanked.swift), and compute matched ranges only for kept rows. - Narrow, do not rescan: keep the last request's matched row set; a longer prefix filters that set.
- Build
SQLCompletionItems only for the kept pool, and drop the per-itemUUID. - Answer
schema.andresolveAliasfrom the index (per-schema range, folded lookup). - Decide suppression right after analysis, before collecting candidates.
- Cache built column items per column-cache key and invalidate only the entries that changed.
The sidebar search folds the same names (#4438 SQ6, plan item B5). If both land, one folded index per catalog can serve both.
Measure
- Unit benchmark with synthetic catalogs of 1,000, 10,000 and 100,000 tables, typing
s,se,sel,FROM o,FROM orandFROM public.o: wall time and allocations per request. Target at 100,000 tables: a few ms per request against 80-150 ms today. - The existing
SQLCompletionProviderranking tests pass unchanged, so the order is the same. - Allocations: persistent bytes held by
SQLSchemaProviderafter loading a 100,000-table schema and opening oneFROMpopup, before and after. - Transient allocations while typing in
... JOIN b ON |with two 300-column tables.
Part of the performance plan.
Activity
datlechin commentedon Nov 26, 2026
The first part is in #4593: a folded name index per table list, prefix ranges by binary search, a bounded selection that keeps the full sort's order, items built only for the kept rows, and suppression decided before candidates are collected. At 100,000 tables a table-slot request takes 0.2 to 2.6 ms in a release build, against 70 to 115 ms before.
Left for a follow-up, each one standing alone:
schema.in a table slot (knownTables) andresolveAlias(tablesResolvableUnqualified) still filter every table per request; both can read the index's rows instead.Setper table) is still rebuilt after every table-list write.ONclause still builds each reference three times; cache them per column-cache key.UUIDstays:SQLCompletionItemisIdentifiableand its synthesizedEquatablecomparesid, so dropping it changes equality for every caller and needs its own change.