Artifact c708f13c8d475de1410b392d9fc568ccc1281b0fc2b16132baf3c4b216288048:
- File
internal/merge/resolve.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: 2271) [annotate] [blame] [check-ins using]
package merge import ( "bufio" "path/filepath" "strings" "github.com/danmestas/go-libfossil/internal/content" libfossil "github.com/danmestas/go-libfossil/internal/fsltype" "github.com/danmestas/go-libfossil/internal/manifest" "github.com/danmestas/go-libfossil/internal/repo" ) // PatternRule maps a glob pattern to a strategy name. type PatternRule struct { Glob string Strategy string } // Resolver picks the merge strategy for a given filename. type Resolver struct { patterns []PatternRule fallback string } // LoadResolver reads the .libfossil-merge file from the repo at the given // version, plus the merge-strategy config key as fallback. func LoadResolver(r *repo.Repo, tipRid libfossil.FslID) *Resolver { if r == nil { panic("merge.LoadResolver: r must not be nil") } res := &Resolver{fallback: "three-way"} var cfgDefault string if r.DB().QueryRow("SELECT value FROM config WHERE name='merge-strategy'").Scan(&cfgDefault) == nil && cfgDefault != "" { res.fallback = cfgDefault } if tipRid > 0 { files, err := manifest.ListFiles(r, tipRid) if err == nil { for _, f := range files { if f.Name == ".libfossil-merge" { rid, ok := content.AvailableByUUID(r.DB(), f.UUID) if ok { // Bare Expand: one config file (.libfossil-merge) at one // version, read once. No revision walk to cache. data, err := content.Expand(r.DB(), rid) if err == nil { res.patterns = parseMergeFile(data) } } break } } } } return res } // Resolve returns the strategy name for a filename. func (res *Resolver) Resolve(filename string) string { for _, p := range res.patterns { matched, matchErr := filepath.Match(p.Glob, filepath.Base(filename)) if matchErr != nil { continue } if matched { return p.Strategy } } return res.fallback } func parseMergeFile(data []byte) []PatternRule { var rules []PatternRule scanner := bufio.NewScanner(strings.NewReader(string(data))) for scanner.Scan() { line := strings.TrimSpace(scanner.Text()) if line == "" || strings.HasPrefix(line, "#") { continue } parts := strings.Fields(line) if len(parts) >= 2 { rules = append(rules, PatternRule{Glob: parts[0], Strategy: parts[1]}) } } return rules }