Repository navigation
perf(catalog): build the letter index in one window pass - #3994
Conversation
The letter index behind queryCatalogLetters sorted the whole title-ordered
list, folded every row's initial to a letter, buffered the list for a total,
then sorted it again by position to find where the letter changes. It now
numbers the rows in one pass over the browse order, keeps only the rows where
the initial changes plus the last row, folds just those to a letter, and
merges neighbouring runs that share a letter in Go. The response is
unchanged.
The DB test now walks the cursor pager a few titles a page and seeds titles
that a linguistic collation interleaves with the letters ('Allo 'Allo!, Ève).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Warning Review limit reachedEnable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
Reviews can continue after your included limit without a manual trigger. An admin must approve usage-based billing. Next included review available in 24 minutes. View limit details
|
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Problem
Related issue: N/A
Validation tasks: none
On a large library, the A–Z jump bar shows up a second or more after the grid.
queryCatalogLettersbuilds the bar, and the web LetterBar waits for it, as will the Apple and Android rails. On 100,000 titles, a cold request takes 0.82–1.02 s, while the first catalog page takes about 75 ms. The per-node cache keeps an index for two minutes, so every later open pays the same cost again.The query sorted the whole list by title, folded each row's first character to a letter with
translate/upper, buffered every row again forCOUNT(*) OVER (), then sorted the list a second time by position to find where the letter changes. This change does the same work in one window pass, which cuts a cold request on 100,000 titles to 0.53–0.56 s.Approach
letterIndexSQLnumbers the rows once, using the browse's ownORDER BYas a named window. From that pass it keeps only the first row, each row whose first character differs from the row before, and the last row. Only those rows, usually a few hundred, are folded to a letter. The Go reader merges neighbouring runs that fold to the same letter (ethenéunder a linguistic collation) and gets the total from the last row's position. The letters, counts, positions and runs in the response are unchanged, and positions still count rows in the orderqueryCatalogItemspages.The remaining cost is the title sort itself, which runs
strcollonce per comparison under a glibc collation. An index-ordered scan avoids that sort, but the planner chooses it only withenable_sortoff, and it is slower for a small library inside a large catalog, so this change leaves the plan to the planner. The cache's TTL stays as it is. The web client refetches the index after a scan, so serving an expired entry would hand it the old one. Only scans and item refreshes publish a change event, so invalidating on events would miss edits and deletions.Server only. No API, schema, or migration change. silo-apple and silo-android need nothing.
Validation
'Allo 'Allo!andÈve, which glibcen_USsorts among the A and E titles. Both tests are already inscripts/ci/db-contracts.txt.en_US.utf8collation and with the title key forced toCOLLATE "C"; also identical for alast_watchedrule (which adds a CTE), empty results, and the episode scope.go build ./...,go vet,gofmt,go mod tidy -diff,make lint-changed,make lint-router-recovery,make verify-apiv2-openapi,make verify-apiv2-contract,make verify-local-paths,make verify-case-collisions,make verify-size-ratchet: pass.make test-go: all packages pass except two real-ffmpeg tests ininternal/mediasampleandinternal/playback, which fail against the local ffmpeg 6.1 and do not importinternal/catalog.Measured on a disposable sandbox with PostgreSQL 18,
en_US.utf8(glibc), defaultwork_mem, warm buffers, one movie library seeded to 25,032 and then 100,032 titles. Endpoint times are curl totals on the host, with the server restarted before each cold request.Not measured: the production library that prompted this, ICU or
Cas the database's default collation (onlyCOLLATE "C"on the key), and behaviour under concurrent load or across several nodes.EXPLAIN (ANALYZE, BUFFERS) excerpts, 100,032 titles, asc
Old (842–899 ms in this run). Of that, ~135 ms is scan and joins, ~390 ms the title sort, ~270 ms the
COUNT(*) OVER ()node where the per-row fold is evaluated, and ~70 ms the second sort andLAG:New (570–668 ms in this run):
Evidence
Evidence: none, no user-visible change
Risks
None identified. The response contract is unchanged. A cold request on a large library is faster but still sorts every match, so it still grows with library size and keeps the existing 10-second deadline.
Checklist
AI Disclosure
🤖 Generated with Claude Code
Note
Build catalog letter index in a single SQL window pass
Reworks
previewPagePlan.letterIndexSQLto rank rows by plan order and return only rows that open a letter run plus the final row, with the folded letter, zero-based position, and an opens flag. The Go loader (QueryExecutor.queryLetterIndex) now tracks the total row count from positions and passes run starts to the newletterIndexOfRunshelper, which computes run lengths and per-letter counts.letterBucketSQLnow takes an initial-character expression and applies accent folding to it.Èveand'Allo 'Allo!fixtures to the Postgres test and updates expected letter counts;orderedTitleKeysnow collects title keys across up to 100 cursor pages.letterBucketSQLcallers must pass an initial-character expression, not a full title-key expression; letter counts are now merged in Go for letters that span multiple runs.Macroscope summarized 41a66a8.