Artifact 99ee4456ece1008f01f63027ca388936925dd36cc3fa54c542e4591c4b46ecec:
- File
internal/verify/rebuild_tags.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: 4296) [annotate] [blame] [check-ins using]
package verify import ( "fmt" "github.com/danmestas/go-libfossil/db" "github.com/danmestas/go-libfossil/internal/blob" "github.com/danmestas/go-libfossil/internal/content" "github.com/danmestas/go-libfossil/internal/deck" libfossil "github.com/danmestas/go-libfossil/internal/fsltype" "github.com/danmestas/go-libfossil/internal/repo" "github.com/danmestas/go-libfossil/internal/tag" ) // rebuildTags reconstructs tagxref rows from inline T-cards in checkin // manifests and from control artifacts. It runs inside the single rebuild // transaction, so it uses tag.ApplyTagWithTx to avoid nested transactions. // // Precondition: rebuildManifests has already populated event/plink/mlink/filename. // The plink graph must exist before tag propagation can walk descendants. func rebuildTags(_ *repo.Repo, tx *db.Tx, report *Report, cache *content.Cache) error { if tx == nil { panic("rebuildTags: nil *db.Tx") } if report == nil { panic("rebuildTags: nil *Report") } entries, err := collectBlobEntries(tx) if err != nil { return err } // Pass 1: process checkin manifests (inline T-cards where UUID == "*"). // Both passes re-expand every blob; sharing checkBlobs'/rebuildManifests' // cache means each blob's chain is walked once for the whole rebuild, not // once per pass. for _, e := range entries { data, err := cache.Expand(tx, e.rid) if err != nil { continue // already counted in BlobsSkipped by rebuildManifests } d, err := deck.Parse(data) if err != nil { continue } if d.Type != deck.Checkin { continue } if err := rebuildInlineTags(tx, e.rid, d); err != nil { return fmt.Errorf("rebuildTags inline rid=%d: %w", e.rid, err) } } // Pass 2: process control artifacts (T-cards with explicit target UUID). for _, e := range entries { data, err := cache.Expand(tx, e.rid) if err != nil { continue // already counted in BlobsSkipped by rebuildManifests } d, err := deck.Parse(data) if err != nil { continue } if d.Type != deck.Control { continue } if err := rebuildControlTags(tx, e.rid, d); err != nil { return fmt.Errorf("rebuildTags control rid=%d: %w", e.rid, err) } } return nil } // rebuildInlineTags processes inline T-cards (UUID == "*") from a checkin manifest. func rebuildInlineTags(tx *db.Tx, rid libfossil.FslID, d *deck.Deck) error { if tx == nil { panic("rebuildInlineTags: nil *db.Tx") } if d == nil { panic("rebuildInlineTags: nil *deck.Deck") } mtime := libfossil.TimeToJulian(d.D) for _, tc := range d.T { if tc.UUID != "*" { continue } tagType, ok := mapDeckTagType(tc.Type) if !ok { continue } if err := tag.ApplyTagWithTx(tx, tag.ApplyOpts{ TargetRID: rid, SrcRID: rid, // inline: checkin is its own source TagName: tc.Name, TagType: tagType, Value: tc.Value, MTime: mtime, }); err != nil { return fmt.Errorf("inline tag %q: %w", tc.Name, err) } } return nil } // rebuildControlTags processes T-cards from a control artifact. func rebuildControlTags(tx *db.Tx, srcRID libfossil.FslID, d *deck.Deck) error { if tx == nil { panic("rebuildControlTags: nil *db.Tx") } if d == nil { panic("rebuildControlTags: nil *deck.Deck") } mtime := libfossil.TimeToJulian(d.D) for _, tc := range d.T { if tc.UUID == "*" { continue // self-referencing — handled by rebuildInlineTags } targetRID, ok := blob.Exists(tx, tc.UUID) if !ok { continue // target not found — phantom or not yet received } tagType, ok := mapDeckTagType(tc.Type) if !ok { continue } if err := tag.ApplyTagWithTx(tx, tag.ApplyOpts{ TargetRID: targetRID, SrcRID: srcRID, TagName: tc.Name, TagType: tagType, Value: tc.Value, MTime: mtime, }); err != nil { return fmt.Errorf("control tag %q on rid=%d: %w", tc.Name, targetRID, err) } } return nil } // mapDeckTagType converts a deck.TagType to a tag package constant. // Returns (tagType, true) on success, (0, false) for unrecognized types. func mapDeckTagType(dt deck.TagType) (int, bool) { switch dt { case deck.TagPropagating: return tag.TagPropagating, true case deck.TagSingleton: return tag.TagSingleton, true case deck.TagCancel: return tag.TagCancel, true default: return 0, false } }