Select module files with ancestor boundary lookups

This commit is contained in:
2026-10-10 14:29:27 +00:00
parent 8552f92ddb
commit 8112a001d2
4 changed files with 56 additions and 6 deletions

View File

@@ -0,0 +1,38 @@
package validation
import (
"fmt"
"testing"
"gitea.maximumdirect.net/eric/ReleaseKit/internal/git"
)
func BenchmarkSelectInputEntriesByModuleCount(b *testing.B) {
const retainedFiles = 20000
for _, boundaries := range []int{0, 1000, 4000} {
b.Run(fmt.Sprintf("boundaries=%d", boundaries), func(b *testing.B) {
entries := make([]git.TreeEntry, 0, retainedFiles+2*boundaries)
entries = append(entries, git.TreeEntry{Path: "go.mod", Type: "blob", Mode: "100644"})
for file := 1; file < retainedFiles; file++ {
entries = append(entries, git.TreeEntry{Path: fmt.Sprintf("source/file-%06d.txt", file), Type: "blob", Mode: "100644"})
}
for module := 0; module < boundaries; module++ {
directory := fmt.Sprintf("modules/unit-%06d", module)
entries = append(entries,
git.TreeEntry{Path: directory + "/go.mod", Type: "blob", Mode: "100644"},
git.TreeEntry{Path: directory + "/source.go", Type: "blob", Mode: "100644"})
}
byPath := make(map[string]int, len(entries))
for i, entry := range entries {
byPath[entry.Path] = i
}
b.ReportAllocs()
for b.Loop() {
selected, err := selectInputEntries(entries, byPath, ".", []string{})
if err != nil || len(selected) != retainedFiles {
b.Fatalf("selected %d files, error=%v; want %d", len(selected), err, retainedFiles)
}
}
})
}
}

View File

@@ -95,7 +95,7 @@ func (tree *CandidateTree) InputEntries() []git.TreeEntry {
}
func selectInputEntries(entries []git.TreeEntry, byPath map[string]int, modulePath string, sharedFiles []string) ([]git.TreeEntry, error) {
boundaries := make([]string, 0)
boundaries := make(map[string]struct{})
for _, entry := range entries {
moduleRelative, ok := relativeToModule(entry.Path, modulePath)
if !ok || moduleRelative == "go.mod" || path.Base(moduleRelative) != "go.mod" {
@@ -104,7 +104,7 @@ func selectInputEntries(entries []git.TreeEntry, byPath map[string]int, modulePa
if insideVendor(moduleRelative) {
continue
}
boundaries = append(boundaries, path.Dir(entry.Path))
boundaries[path.Dir(entry.Path)] = struct{}{}
}
selected := make(map[string]git.TreeEntry)
for _, entry := range entries {
@@ -113,8 +113,8 @@ func selectInputEntries(entries []git.TreeEntry, byPath map[string]int, modulePa
continue
}
excluded := false
for _, boundary := range boundaries {
if entry.Path == boundary || strings.HasPrefix(entry.Path, boundary+"/") {
for ancestor := entry.Path; ancestor != "." && ancestor != modulePath; ancestor = path.Dir(ancestor) {
if _, isBoundary := boundaries[ancestor]; isBoundary {
excluded = true
break
}