Artifact 45c435f0366a955006058de8e6752b86ab2f82c93e66ddeb8b13d6d6a1c9b488:
- File
internal/verify/rebuild.go
— part of check-in
[0fd2a507b8]
at
2026-07-24 15:27:03
on branch trunk
— perf: route history-walking expansions through content.Cache (#151) (#153)
The buffer-reuse fix in #145/#150 cut the cost of expanding one delta chain once. It did nothing for callers that expand many versions of the same artifact in a single operation: each bare content.Expand still replayed the whole O(depth) chain, so a walk over N overlapping revisions cost O(N^2) blob reads even though content.Cache already existed for exactly this pattern.
Audited every non-test content.Expand caller. Two access patterns genuinely repeat over overlapping delta chains and are now routed through a per-operation content.Cache:
- annotate walks every revision of one file along the parent chain. content_deltify stores each older file blob as a delta against its successor, so the per-revision expansions are all walks of the same few chains. One cache threaded through the walk (file blob + manifest listing via new manifest.ListFilesCached/GetManifestCached, both nil-passthrough so existing callers are unchanged) turns it linear. Measured on a deltified 150-revision file: 1.60s -> 0.65s (2.5x), 764k -> 308k allocs; the win grows with history depth.
- verify.Verify and verify.Rebuild sweep every blob more than once (Rebuild: checkBlobs + rebuildManifests + rebuildTags x2). A single cache shared across the pass walks each chain once instead of once per sweep and amortizes overlap within each sweep. Measured on a 150-revision two-file repo: Rebuild 3.34s -> 0.068s (48x), 4.59M -> 98k allocs.
Callers that only ever touch the tip or make a single pass over distinct blobs (branch create, merge-config load, R-card / delta manifest build, MergeParentFiles, crosslinkSingle, shun undelta, pairwise Diff) are left on bare Expand, each with a comment stating why a cache cannot amortize their access.
No behavior change: expansion output is byte-identical, verified across the modernc and ncruces drivers with REQUIRE_FOSSIL_BIN=1 cross-checks and the DST suite. (user: [email protected], size: 2679) [annotate] [blame] [check-ins using]
package verify import ( "fmt" "time" "github.com/danmestas/go-libfossil/db" "github.com/danmestas/go-libfossil/internal/content" "github.com/danmestas/go-libfossil/internal/repo" ) // rebuildTableList is the set of derived tables dropped and reconstructed // during a rebuild. Order does not matter — they are all DELETEd first. var rebuildTableList = []string{ "event", "mlink", "plink", "tagxref", "filename", "leaf", "unclustered", "unsent", } // Rebuild reconstructs all derived tables from raw blob content. // It first verifies blob integrity (read-only), then drops and // rebuilds event/mlink/plink/tagxref/filename/leaf/unclustered/unsent // in a single transaction. func Rebuild(r *repo.Repo) (*Report, error) { if r == nil { panic("verify.Rebuild: nil *repo.Repo") } start := time.Now() report := &Report{} // One expansion cache for the whole rebuild; see verifyCacheBytes. It spans // the pre-transaction blob check and the in-transaction sweeps because a // blob's rid and content are the same whichever querier reads them, and // rebuild never rewrites blob content -- only the derived tables. cache := content.NewCache(verifyCacheBytes) // Phase 1: verify blobs (read-only, outside transaction) if err := checkBlobs(r, report, cache); err != nil { return nil, fmt.Errorf("verify.Rebuild: %w", err) } // Phases 2-4: drop + reconstruct in transaction if err := r.WithTx(func(tx *db.Tx) error { if err := dropDerivedTables(tx); err != nil { return err } if err := rebuildManifests(r, tx, report, cache); err != nil { return fmt.Errorf("rebuild manifests: %w", err) } if err := rebuildTags(r, tx, report, cache); err != nil { return fmt.Errorf("rebuild tags: %w", err) } if err := rebuildLeaves(tx); err != nil { return fmt.Errorf("rebuild leaves: %w", err) } if err := rebuildBookkeeping(tx); err != nil { return fmt.Errorf("rebuild bookkeeping: %w", err) } return nil }); err != nil { return nil, fmt.Errorf("verify.Rebuild: %w", err) } report.TablesRebuilt = rebuildTableList report.Duration = time.Since(start) // Postcondition: TablesRebuilt must be populated after successful rebuild. if len(report.TablesRebuilt) == 0 { panic("verify.Rebuild: postcondition: TablesRebuilt empty after successful rebuild") } return report, nil } // dropDerivedTables deletes all rows from the derived tables. func dropDerivedTables(tx *db.Tx) error { if tx == nil { panic("verify.dropDerivedTables: nil *db.Tx") } for _, table := range rebuildTableList { if _, err := tx.Exec("DELETE FROM " + table); err != nil { return fmt.Errorf("drop %s: %w", table, err) } } return nil }