Repository navigation
perf(scanner): keep less per-file bookkeeping during a library scan - #3957
Conversation
A full scan holds its bookkeeping for every file under a root at once. Three parts of it were heavier than they needed to be: - Group inference kept two full copies of every file's group identity, one per group and one per observed root, only to count them and read a few fields. Each bucket now keeps the first file's identity and, for the rest, just the fields the snapshot reads. - The subtitle directory cache kept the listing of every directory for the whole scan. Files are processed in walk order, so it now keeps the 512 most recently used listings and reads a directory again if it has dropped out. - Scan state rows each held their own copy of strings that repeat across the files of a show or folder (roots, group key, title, codec and container names). The rows of one load now share one copy. Scan results are unchanged.
Share the scan state strings in a separate pass so the row scanner is unchanged, name the group states, check the subtitle cache's type assertions, and encode group evidence from a typed struct whose field order keeps the stored JSON identical.
…file Group inference kept a bucket entry for every file. It now keeps a tally per content group and per observed location: the first file's identity, which supplies the snapshot's sample and identity fields, and for the rest of the files only what the snapshot counts (one shared string per file for the group evidence). The per-file identity step is its own function. The snapshots and their evidence are unchanged.
The concurrent cache test now gives the cache room for two directories and has its workers read four, so eviction and re-reads run under the race detector.
|
Warning Review limit reachedThis organization requires a seat for paid usage-based reviews, including automatic and on-demand reviews. Ask an admin to assign a seat or enable paid usage-based reviews for users without a seat in Billing. Next included review available in 35 minutes. View limit details
Pre-merge checks:
|
Code Review Could Not Complete
|
| Options | Enabled |
|---|---|
| Bug | ✅ |
| Performance | ✅ |
| Security | ✅ |
| Business Logic | ❌ |
Measured, it saved 51 MB on a 225,000-file series root but almost nothing on a movie library, where the map cost more at its peak than it saved, and a batched full scan holds too few rows for it to matter. It is not worth the code.
Code Review Could Not Complete
|
| Options | Enabled |
|---|---|
| Bug | ✅ |
| Performance | ✅ |
| Security | ✅ |
| Business Logic | ❌ |
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:
|
|
@kody start-review |
Problem
Related issue: N/A
Validation tasks: none changed. Targeted scans run this group inference and subtitle detection, so it reaches #1231 C1 (passed); their results are the same. Full scans fall under #1250 C1, which has not been run yet.
A library scan holds some bookkeeping for every file under a root at once, and two parts of it were heavier than they needed to be:
This change keeps less of each. Scan results are the same.
Measured on 300,000 synthetic episode paths, group inference peaked at 316 MiB of live Go heap on
mainand 151 MiB on this branch, for the same 70 MiB result. Over 5,000 season folders, the subtitle cache peaked at 18.4 MiB onmainand 2.7 MiB here.Approach
#3958 builds on this to process a full scan in batches; the tally here is the one it adds to batch by batch.
Validation
go vet ./internal/scanner/: pass.-race: pass. On 300,000 synthetic episode paths the snapshots and every file's assignment hash the same on upstreammainand this branch (Benchmarks).TestExternalSubtitleDirCacheReadsSubtitleFoldersfails locally on macOS onmaintoo: it writesCase/Movie.mkvandCase/movie.mkv, which are one file on a case-insensitive disk. CI runs on Linux.a70018daa, including Go lint (run). Web jobs were skipped; no web code changed.Benchmarks
Measured with a temporary harness (not committed; its source is on the evidence page), one measurement per process, five runs each, medians with the range in brackets.
main5159df6dc. After:6ce2cab27; the commit after it,a70018daa, only removes the string sharing described below and changes neither of these components./gc/heap/live:bytes, sampled every millisecond withGOGC=10), the heap still held after the call, bytes allocated.GOMAXPROCS=6andGOMEMLIMIT=3GiB.Time is unchanged (8.2 s and 8.1 s for group inference with
GOGC=10). The snapshots and every file's assignment hash the same on both commits. The held 238 MiB is every file's root and group assignment, which #3958 stops holding for the whole root.An earlier version also shared repeated strings between the scan-state rows of one load. It saved 51 MiB of the 541 MiB those rows hold for a 225,000-file series root, but on a 20,000-movie root it raised the peak by 3 MiB, and #3958 holds 5,000 rows at a time, so I took it out.
Evidence
Evidence: none, no user-visible change. Scans write the same catalog and find the same sidecars. The benchmark output is on the evidence page.
Evidence: https://evidence.siloserver.org/r/silo-server/pr-3957/
Risks
Checklist
AI Disclosure
Note
Reduce per-file bookkeeping in scanner with tallies and an LRU subtitle directory cache
scanGroupInferenceaccumulator. Group and location state is tallied per file instead of keeping a full list of per-file entries, andresult()snapshots the accumulated tallies. TheaggregateGroupState,aggregateLocationState,aggregateGroupOverrideSource, andaggregateGroupEvidencehelpers are removed.scanGroupInference.add, which returns only that batch's assignments.locationTally.snapshotmarks a location ambiguous when multiple groups have non-overridden files.Macroscope summarized a70018d.