fix: replace @lru_cache with instance-level caching in Repository.is_git_repo() #4211
+93
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a memory leak in the
Repositoryclass where using@lru_cacheon theis_git_repo()instance method prevented garbage collection of Repository instances. Thelru_cachedecorator holds references toselfin its cache dictionary, keeping instances alive indefinitely.The fix replaces the
@lru_cachedecorator with instance-level caching using a_is_git_repo_cache: bool | Noneattribute. This maintains O(1) performance for repeated calls while allowing proper garbage collection when instances go out of scope.Fixes #4210
Review & Testing Checklist for Human
Noneinitially, populated on firstis_git_repo()call, and returned directly on subsequent calls@lru_cacheusages on instance methods elsewhere in the codebase that might have the same issueSuggested test plan: Create a simple script that instantiates Repository objects in a loop and monitors memory usage to confirm it stays stable rather than growing.
Notes
Link to Devin run: https://app.devin.ai/sessions/56e14e0b869c4483a02020b60148cce2
Requested by: João