Artifact 705b9bcc12c25fa6a0cea3e19692b08c082719a408142465a87fe024acc5879a:
- File
internal/shun/purge.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: 3270) [annotate] [blame] [check-ins using]
package shun import ( "fmt" "github.com/danmestas/go-libfossil/db" "github.com/danmestas/go-libfossil/internal/blob" "github.com/danmestas/go-libfossil/internal/content" libfossil "github.com/danmestas/go-libfossil/internal/fsltype" ) // PurgeResult summarizes what Purge deleted. type PurgeResult struct { BlobsDeleted int DeltasExpanded int PrivateCleaned int } // Purge physically removes all shunned blobs from the repo. // Follows Fossil's shun_artifacts() algorithm from shun.c. // Requires *db.DB (not Querier) because it runs in a transaction. func Purge(d *db.DB) (PurgeResult, error) { if d == nil { panic("shun.Purge: d must not be nil") } var result PurgeResult err := d.WithTx(func(tx *db.Tx) error { // Step 1: Find rids to delete. _, err := tx.Exec(`CREATE TEMP TABLE toshun(rid INTEGER PRIMARY KEY)`) if err != nil { return fmt.Errorf("create toshun: %w", err) } _, err = tx.Exec(`INSERT INTO toshun SELECT rid FROM blob WHERE uuid IN (SELECT uuid FROM shun)`) if err != nil { return fmt.Errorf("populate toshun: %w", err) } // Step 2: Undelta dependents — expand blobs whose delta source is being shunned. rows, err := tx.Query(`SELECT rid FROM delta WHERE srcid IN (SELECT rid FROM toshun)`) if err != nil { return fmt.Errorf("query dependents: %w", err) } var dependents []libfossil.FslID for rows.Next() { var rid libfossil.FslID if err := rows.Scan(&rid); err != nil { rows.Close() return fmt.Errorf("scan dependent: %w", err) } dependents = append(dependents, rid) } rows.Close() // Bare Expand: each dependent of a shunned base is expanded once to be // rewritten as whole content. Distinct rids, single pass -- and the // deltas are being deleted, so there is no chain left to cache against. for _, rid := range dependents { expanded, err := content.Expand(tx, rid) if err != nil { return fmt.Errorf("expand rid %d: %w", rid, err) } compressed, err := blob.Compress(expanded) if err != nil { return fmt.Errorf("compress rid %d: %w", rid, err) } _, err = tx.Exec("UPDATE blob SET content=?, size=? WHERE rid=?", compressed, len(expanded), rid) if err != nil { return fmt.Errorf("update blob rid %d: %w", rid, err) } _, err = tx.Exec("DELETE FROM delta WHERE rid=?", rid) if err != nil { return fmt.Errorf("delete delta rid %d: %w", rid, err) } result.DeltasExpanded++ } // Step 3: Delete shunned blobs. _, err = tx.Exec("DELETE FROM delta WHERE rid IN (SELECT rid FROM toshun)") if err != nil { return fmt.Errorf("delete shunned deltas: %w", err) } res, err := tx.Exec("DELETE FROM blob WHERE rid IN (SELECT rid FROM toshun)") if err != nil { return fmt.Errorf("delete shunned blobs: %w", err) } n, _ := res.RowsAffected() result.BlobsDeleted = int(n) _, err = tx.Exec("DROP TABLE toshun") if err != nil { return fmt.Errorf("drop toshun: %w", err) } // Step 4: Orphan cleanup. res, err = tx.Exec("DELETE FROM private WHERE NOT EXISTS (SELECT 1 FROM blob WHERE rid=private.rid)") if err != nil { return fmt.Errorf("cleanup private: %w", err) } n, _ = res.RowsAffected() result.PrivateCleaned = int(n) return nil }) return result, err }