Artifact 497166ca32f04bf23e8d0c3972e612c394c0f8804133933dbef5a21479221f4c:
- File
internal/manifest/dephantomize.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: 4074) [annotate] [blame] [check-ins using]
package manifest import ( "fmt" "log/slog" "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" ) // AfterDephantomize crosslinks a formerly-phantom blob and any dependents. // Matches Fossil's after_dephantomize (content.c:389-456). func AfterDephantomize(r *repo.Repo, rid libfossil.FslID) { if r == nil { panic("manifest.AfterDephantomize: r must not be nil") } if rid <= 0 { return } afterDephantomize(r, rid, true) } func afterDephantomize(r *repo.Repo, rid libfossil.FslID, linkFlag bool) { // Work stack replaces recursion. Bounded by total blob count in repo. type workItem struct { rid libfossil.FslID linkFlag bool } stack := []workItem{{rid: rid, linkFlag: linkFlag}} const maxIterations = 1_000_000 // Guard against pathological delta chains. iterations := 0 for len(stack) > 0 { iterations++ if iterations > maxIterations { return // Safety bound exceeded. } // Pop from stack. item := stack[len(stack)-1] stack = stack[:len(stack)-1] current := item.rid if current <= 0 { continue } if item.linkFlag { if err := crosslinkSingle(r, current); err != nil { slog.Warn("manifest.AfterDephantomize: crosslink failed, blob left un-crosslinked", "rid", current, "error", err) } } // Process orphaned delta manifests whose baseline is this rid. orphanRows, err := r.DB().Query("SELECT rid FROM orphan WHERE baseline=?", current) if err == nil { var orphans []libfossil.FslID for orphanRows.Next() { var orid int64 if orphanRows.Scan(&orid) == nil { orphans = append(orphans, libfossil.FslID(orid)) } } orphanRows.Close() for _, orid := range orphans { if err := crosslinkSingle(r, orid); err != nil { slog.Warn("manifest.AfterDephantomize: crosslink of orphan failed, blob left un-crosslinked", "rid", orid, "baseline", current, "error", err) } } if len(orphans) > 0 { if _, err := r.DB().Exec("DELETE FROM orphan WHERE baseline=?", current); err != nil { continue } } } // Find delta children not yet crosslinked. childRows, err := r.DB().Query( `SELECT rid FROM delta WHERE srcid=? AND NOT EXISTS (SELECT 1 FROM mlink WHERE mid=delta.rid)`, current) if err != nil { continue } var children []libfossil.FslID for childRows.Next() { var crid int64 if childRows.Scan(&crid) == nil { children = append(children, libfossil.FslID(crid)) } } childRows.Close() // Push all children onto work stack (reverse order for LIFO processing). for i := len(children) - 1; i >= 0; i-- { stack = append(stack, workItem{rid: children[i], linkFlag: true}) } } } // crosslinkSingle crosslinks a single blob by rid. func crosslinkSingle(r *repo.Repo, rid libfossil.FslID) error { if r == nil { panic("crosslinkSingle: r must not be nil") } if rid <= 0 { panic("crosslinkSingle: rid must be positive") } // Bare Expand: crosslinks one just-dephantomized artifact, expanded once. // The whole-repository crosslink sweep that has genuine chain overlap is // Crosslink/CrosslinkContext, which already runs its own content.Cache. data, err := content.Expand(r.DB(), rid) if err != nil { return fmt.Errorf("crosslinkSingle expand rid=%d: %w", rid, err) } d, err := deck.Parse(data) if err != nil { return fmt.Errorf("crosslinkSingle parse rid=%d: %w", rid, err) } switch d.Type { case deck.Checkin: return crosslinkCheckin(r, rid, d) case deck.Wiki: _, err = crosslinkWiki(r, rid, d) return err case deck.Ticket: _, err = crosslinkTicket(r, rid, d) return err case deck.Event: _, err = crosslinkEvent(r, rid, d) return err case deck.Attachment: return crosslinkAttachment(r, rid, d) case deck.Cluster: return CrosslinkCluster(r.DB(), rid, d) case deck.ForumPost: return crosslinkForum(r, rid, d) case deck.Control: return crosslinkControl(r, rid, d) } return nil }