Skip to content

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

Merged
datlechin merged 5 commits into
mainfrom
perf/table-name-index
Nov 27, 2026
Merged

datlechin merged 5 commits into
mainfrom
perf/table-name-index

Conversation

@datlechin

@datlechin datlechin commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Summary

  • SQLCompletionNameIndex (TableProAutocomplete) folds a catalog's table list once: names, kinds, the lowercased keys back to back, and the rows sorted by key. SQLSchemaProvider builds it once per table list and default schema, and drops it on every table-list or namespace write.
  • A table slot ranks from the index instead of an item per table. The typed prefix is a binary-searched range; the contains and subsequence passes run on the raw keys and are skipped when the kept pool already scores below anything they could find. Only the best pool survive, in a bounded heap keyed by score then emission position, so the order is the full sort's. Items, with their matched ranges, are built only for the kept rows.
  • Names outside printable ASCII, and a prefix outside it, go through the provider's own matcher, so case folding and Unicode forms rank exactly as before.
  • The empty-prefix suppression runs right after analysis, before any candidate is collected, so a space at a statement start no longer ranks the table list to throw it away.
  • A catalog without an index (the iOS one) keeps the item path, now through the same bounded selection.
  • setNamespaces now also drops the cached bare-name table items: a schema's spelling decides which tables a bare name reaches.
  • Merged with main after perf(autocomplete): run completion requests off the main thread with no wait before them #4601: completionSession stays @concurrent, and a cancelled request stops at the same early return as a suppressed one. The test catalog keeps both onTableRead (main) and indexesTables, and its index read calls onTableRead too.

Left for a follow-up, noted on the issue: narrowing from the previous request's matches, schema. and resolveAlias from the index, the spelling index, cached column items, and dropping the per-item UUID.

Test plan

  • New SQLCompletionNameIndexTests: 25 queries (statement start, FROM, JOIN, INSERT INTO, DROP, CREATE VIEW, a quoted name, mixed case, views, non-ASCII and control-character names) rank identically through the index and through items: labels, kinds, matched ranges, fuzzy penalties and the truncation flag, over 4,509 tables. Plus the prefix range and the early suppression.
  • New SQLSchemaProviderNameIndexTests: the index lists what tableCompletionItems() lists, in the same order, and follows a new table list.
  • After the merge with main (the last merge touched none of this PR's files): verify.sh package TableProCore TableProAutocompleteTests: PASS, 526 executed, 526 passed.
  • verify.sh test over the 24 hosted completion suites (SQLSchemaProvider*, CompletionEngine*, SQLCompletionService*, SQLCompletionProvider*, the adapter and ranking suites): PASS, 359 executed, 359 passed.
  • verify.sh build: PASS. iOS build-for-testing for the generic Simulator: TEST BUILD SUCCEEDED. verify.sh lint on every changed Swift file: PASS.
  • CI's iOS job failed only because the runner had no iOS 27.2 Simulator runtime ("The iOS 27.2 simulator runtime is not available", fixed by ci(ios): install the simulator runtime when the runner image lacks it #4607); its package tests passed. The iOS suites have not run on this change: this Mac has no Simulator runtime.
  • Codex review and security pass on the merged branch: no findings.
  • Release build, synthetic catalogs, median of 7 requests (swift test -c release):
Tables Query Before After
1,000 s / se / FROM o / FROM or 2.0 / 2.0 / 1.5 / 0.9 ms 0.3 / 0.4 / 1.2 / 0.6 ms
10,000 s / se / FROM o / FROM or 11.2 / 10.8 / 9.3 / 10.4 ms 0.15 / 0.55 / 0.63 / 0.55 ms
100,000 s / se / sel 115 / 98 / 75 ms 0.20 / 2.6 / 1.6 ms
100,000 FROM / FROM o / FROM or / FROM xq 75 / 79 / 93 / 70 ms 0.84 / 0.32 / 0.32 / 2.2 ms

Persistent bytes in SQLSchemaProvider after a 100,000-table load were not measured; the index keeps two bytes per name character and about 26 bytes per row (the name shares its storage with the table list), against one SQLCompletionItem per table before, which SQL completion no longer builds.

Closes #4540

… sorting the catalog

A table slot copied every table into an item, filtered them with NSString
searches, scored and fully sorted them, and kept 400. At 100,000 tables that is
70 to 115 ms per request in a release build.

The schema provider now builds a folded name index once per table list. A
request binary-searches the typed prefix, runs the contains and subsequence
passes on the raw keys only when the kept pool could still change, keeps the
best rows in a bounded heap ordered as the full sort was, and builds items only
for those rows. Names outside printable ASCII take the provider's own matcher,
so the order is the same for every name. Empty-prefix suppression now runs
before candidates are collected.
@datlechin
datlechin enabled auto-merge (squash) November 26, 2026 01:51
# Conflicts:
#	apple/Packages/TableProCore/Sources/TableProAutocomplete/SQLCompletionProvider.swift
#	apple/Packages/TableProCore/Tests/TableProAutocompleteTests/SQLCompletionProviderCatalogTests.swift
@datlechin
datlechin merged commit f51edc1 into main Nov 27, 2026
2 of 3 checks passed
@datlechin
datlechin deleted the perf/table-name-index branch November 27, 2026 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant