Skip to content

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

Merged
pjbgf merged 3 commits intogo-git:releases/v5.xfrom
k-anshul:optimize_infiles_v5.x
Feb 17, 2026
Merged

git: worktree, optimize infiles function for very large repos#1853
pjbgf merged 3 commits intogo-git:releases/v5.xfrom
k-anshul:optimize_infiles_v5.x

Conversation

@k-anshul
Copy link
Contributor

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

Backport for #1852.
Closes #1845.

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

This PR optimizes the inFiles function for very large repositories by replacing a linear search (O(n*m) complexity) with a map-based lookup (O(n+m) complexity). The optimization is particularly beneficial when dealing with large numbers of changes and file filters during git reset operations.

Changes:

  • Replaced slice-based linear search in inFiles with O(1) map lookup
  • Added buildFilePathMap helper function to create file path maps with cleaned paths
  • Pre-allocated removedFiles slice capacity in resetIndex for better memory efficiency

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

}

func inFiles(files []string, v string) bool {
// inFiles checks if the given file is in the list of files. The incoming filepaths in files should be cleaned before calling this function.
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 comment states "The incoming filepaths in files should be cleaned before calling this function", but this is misleading. The function already cleans the input parameter v on line 442, and the map parameter files is already built with cleaned paths by buildFilePathMap. Consider revising the comment to clarify that the map is expected to contain pre-cleaned paths (which is handled by buildFilePathMap), and that the function will clean the lookup key v internally.

Suggested change
// inFiles checks if the given file is in the list of files. The incoming filepaths in files should be cleaned before calling this function.
// inFiles checks if the given file is in the list of files. The files map is expected
// to contain pre-cleaned paths (as produced by buildFilePathMap), and this function
// will clean the lookup key v before checking for membership.

Copilot uses AI. Check for mistakes.
@cedric-appdirect
Copy link
Contributor

Is it possible to have a benchmark or is there an existing benchmark that get better when using this PR?

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 working on this and backporting this change. 🙇

@pjbgf pjbgf merged commit 438a37f into go-git:releases/v5.x Feb 17, 2026
19 of 20 checks passed
@pjbgf
Copy link
Member

pjbgf commented Feb 17, 2026

Is it possible to have a benchmark or is there an existing benchmark that get better when using this PR?

@cedric-appdirect I agree benchmarking performance changes is a good practice. On this case specifically I'm slighly less worried due to the change itself.

That being said, I'm planning to have some level of performance regression tests into CI in the near future.

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