Artifact 017d6e41c7f97f262614641f56e14ced1b81e953758dbc272fe8fb24496abb80:
- File
internal/manifest/files.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: 3798) [annotate] [blame] [check-ins using]
package manifest import ( "fmt" "sort" "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" ) type FileEntry struct { Name string UUID string Perm string } func ListFiles(r *repo.Repo, rid libfossil.FslID) ([]FileEntry, error) { return ListFilesCached(r, rid, nil) } // ListFilesCached is ListFiles that serves the checkin manifest -- and, for a // delta manifest, its baseline -- through cache. A nil cache is exactly // ListFiles (content.Cache's nil receiver falls through to content.Expand). // A live cache helps only a caller listing many overlapping revisions, such as // annotate walking one file's history; see GetManifestCached. func ListFilesCached(r *repo.Repo, rid libfossil.FslID, cache *content.Cache) ([]FileEntry, error) { d, err := GetManifestCached(r, rid, cache) if err != nil { return nil, fmt.Errorf("manifest.ListFiles: %w", err) } if d.B == "" { return fCardsToEntries(d.F), nil } baseRid, ok := content.AvailableByUUID(r.DB(), d.B) if !ok { return nil, fmt.Errorf("manifest.ListFiles: baseline %s not found", d.B) } baseData, err := cache.Expand(r.DB(), baseRid) if err != nil { return nil, fmt.Errorf("manifest.ListFiles: expand baseline: %w", err) } baseDeck, err := deck.Parse(baseData) if err != nil { return nil, fmt.Errorf("manifest.ListFiles: parse baseline: %w", err) } fileMap := make(map[string]FileEntry) for _, f := range baseDeck.F { fileMap[f.Name] = FileEntry{Name: f.Name, UUID: f.UUID, Perm: f.Perm} } for _, f := range d.F { if f.UUID == "" { delete(fileMap, f.Name) } else { fileMap[f.Name] = FileEntry{Name: f.Name, UUID: f.UUID, Perm: f.Perm} } } entries := make([]FileEntry, 0, len(fileMap)) for _, e := range fileMap { entries = append(entries, e) } sort.Slice(entries, func(i, j int) bool { return entries[i].Name < entries[j].Name }) return entries, nil } func fCardsToEntries(fCards []deck.FileCard) []FileEntry { entries := make([]FileEntry, len(fCards)) for i, f := range fCards { entries[i] = FileEntry{Name: f.Name, UUID: f.UUID, Perm: f.Perm} } sort.Slice(entries, func(i, j int) bool { return entries[i].Name < entries[j].Name }) return entries } // MergeParentFiles returns supplied with every file tracked at parentRID // that is not already in supplied appended. Names in supplied win over the // parent's entry; the parent's untouched files have their content expanded // from the repo so the resulting slice is a full-tree set ready for Checkin. // // parentRID == 0 returns supplied unchanged. func MergeParentFiles(r *repo.Repo, parentRID libfossil.FslID, supplied []File) ([]File, error) { if parentRID == 0 { return supplied, nil } parentFiles, err := ListFiles(r, parentRID) if err != nil { return nil, fmt.Errorf("manifest.MergeParentFiles: %w", err) } suppliedNames := make(map[string]struct{}, len(supplied)) for _, f := range supplied { suppliedNames[f.Name] = struct{}{} } merged := supplied for _, pf := range parentFiles { if _, ok := suppliedNames[pf.Name]; ok { continue } baseRid, ok := content.AvailableByUUID(r.DB(), pf.UUID) if !ok { return nil, fmt.Errorf("manifest.MergeParentFiles: blob %s for %s not found", pf.UUID, pf.Name) } // Bare Expand: each untouched parent file is expanded once to build a // single commit's full-tree file set. Distinct blobs, no overlap. data, err := content.Expand(r.DB(), baseRid) if err != nil { return nil, fmt.Errorf("manifest.MergeParentFiles: expand %s: %w", pf.Name, err) } merged = append(merged, File{ Name: pf.Name, Content: data, Perm: pf.Perm, }) } return merged, nil }