Skip to content

perf(autocomplete): rank table names from a prebuilt index instead of sorting the catalog #4540

Done

Description

@datlechin

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

  • getCandidates adds catalog.tableCompletionItems(), every unqualified table, for .from, .join, .into, .dropObject, .createIndex, .createView and .unknown, the clause at the start of every statement (apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLCompletionProvider.swift:188-189, :199-200, :437-457).
  • rankedSession joins the segments into a new array, runs filterByPrefix over all of them (an NSString bridge and up to two range(of:) calls per name, then a subsequence pass that allocates matched ranges), builds enumerated().map tuples that copy every item, sorts all of them and keeps the first limit * 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, holdsEveryMatch is 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: suppressesEmptyPrefix is checked only after engine.getCompletions has 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 SQLCompletionItem with a fresh UUID(), 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, knownTables filters 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, resolveAlias walks tablesResolvableUnqualified, which filters and lowercases every table (SQLSchemaProvider.swift:650-682).
  • The spelling index is one dictionary entry and one single-element Set per table, rebuilt after every table-list write (SQLSchemaProvider.swift:563-575).
  • Column items are rebuilt on every request: columnCompletionItems and allColumnsInScope map the cached ColumnInfo to new items (SQLSchemaProvider.swift:850-859, :903-960). In an ON clause 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 pool rows in a bounded heap (the shape TopRanked already has in apple/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-item UUID.
  • Answer schema. and resolveAlias from 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 or and FROM 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 SQLCompletionProvider ranking tests pass unchanged, so the order is the same.
  • Allocations: persistent bytes held by SQLSchemaProvider after loading a 100,000-table schema and opening one FROM popup, before and after.
  • Transient allocations while typing in ... JOIN b ON | with two 300-column tables.

Part of the performance plan.

Activity

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

    @datlechin
    MemberAuthor

    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:

    • Narrowing: keep the last request's matched rows so a longer prefix filters them instead of scanning the index. The non-prefix scan is now about 2 ms at 100,000 tables, so this matters less than the issue expected.
    • schema. in a table slot (knownTables) and resolveAlias (tablesResolvableUnqualified) still filter every table per request; both can read the index's rows instead.
    • The spelling index (one dictionary entry and one single-element Set per table) is still rebuilt after every table-list write.
    • Column items are still rebuilt per request, and an ON clause still builds each reference three times; cache them per column-cache key.
    • The per-item UUID stays: SQLCompletionItem is Identifiable and its synthesized Equatable compares id, so dropping it changes equality for every caller and needs its own change.
    • Persistent bytes after a 100,000-table load were not measured with Allocations.
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