Skip to content

git: worktree, optimize infiles function for very large repos#1852

Merged
pjbgf merged 3 commits intogo-git:mainfrom
k-anshul:optimize_infiles
Feb 17, 2026
Merged

git: worktree, optimize infiles function for very large repos#1852
pjbgf merged 3 commits intogo-git:mainfrom
k-anshul:optimize_infiles

Conversation

@k-anshul
Copy link
Contributor

@k-anshul k-anshul commented Feb 17, 2026

Relates to #1845.

Copilot AI review requested due to automatic review settings February 17, 2026 10:26
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Optimizes Worktree reset filtering to avoid the O(N²) filepath.Clean behavior reported in go-git issue #1845 during clone/checkout paths (notably MergeReset).

Changes:

  • Replaces repeated linear scans over opts.Files with a pre-cleaned lookup map for O(1) membership checks.
  • Updates inFiles to operate on the precomputed set instead of repeatedly cleaning and scanning a slice.
  • Applies the optimization to both resetIndex and resetWorktree filtering paths.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

worktree.go Outdated
Comment on lines 503 to 506
filesMap := make(map[string]bool, len(files))
for _, f := range files {
filesMap[filepath.Clean(f)] = true
}
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic to build a cleaned set of files is duplicated here and in resetWorktree. Consider extracting a small helper (e.g., build a cleaned file-set) to keep the two call sites consistent and reduce the chance of future regressions in this hot path.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not required. These are simple conversions and meaningful util function can not be extracted.

Copy link

@EladGabay EladGabay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor suggestion: map[string]struct{} is more idiomatic than map[string]any for set semantics, avoids allocating an interface header per entry

Copy link
Member

@pjbgf pjbgf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@k-anshul thank you for the quick turn around, please take a look at the suggestions below:

worktree.go Outdated
}

removedFiles := make([]string, 0, len(changes))
var filesMap map[string]any
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var filesMap map[string]any
var filesMap map[string]struct{}

worktree.go Outdated
Comment on lines 506 to 512
var filesMap map[string]any
if len(files) > 0 {
filesMap = make(map[string]any, len(files))
for _, f := range files {
filesMap[filepath.Clean(f)] = nil
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's extract this into a helper func instead, to avoid defining this twice.

worktree.go Outdated
removedFiles := make([]string, 0, len(changes))
var filesMap map[string]any
if len(files) > 0 {
filesMap = make(map[string]any, len(files))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
filesMap = make(map[string]any, len(files))
filesMap = make(map[string]struct{}, len(files))

worktree.go Outdated
if len(files) > 0 {
filesMap = make(map[string]any, len(files))
for _, f := range files {
filesMap[filepath.Clean(f)] = nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
filesMap[filepath.Clean(f)] = nil
filesMap[filepath.Clean(f)] = struct{}{}

@k-anshul
Copy link
Contributor Author

Thank you for the review @pjbgf and @EladGabay

I have addressed the review comments. Could you please take a look again ?

Copy link
Member

@pjbgf pjbgf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@k-anshul thanks for working on this. 🙇

@pjbgf pjbgf merged commit 8c5a7de into go-git:main Feb 17, 2026
23 of 25 checks passed
@k-anshul k-anshul deleted the optimize_infiles branch February 17, 2026 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants