Skip to content

Commit 30f83ff

Browse files
committed
cache git collections with lazy initialization
Replaces direct instantiations of Tags, Commits, Remotes, and References with lazy initialization. This avoids redundant object creation and improves performance by deferring instantiation until the collection is accessed.
1 parent ab71fe3 commit 30f83ff

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

src/GitVersion.LibGit2Sharp/Git/GitRepository.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,19 @@ private IRepository RepositoryInstance
2323
public bool IsShallow => RepositoryInstance.Info.IsShallow;
2424
public IBranch Head => this.repositoryCache.GetOrWrap(RepositoryInstance.Head, RepositoryInstance.Diff);
2525

26-
public ITagCollection Tags => new TagCollection(RepositoryInstance.Tags, RepositoryInstance.Diff, this.repositoryCache);
26+
private ITagCollection? tags;
27+
public ITagCollection Tags => this.tags ??= new TagCollection(RepositoryInstance.Tags, RepositoryInstance.Diff, this.repositoryCache);
28+
2729
public IBranchCollection Branches => new BranchCollection(RepositoryInstance.Branches, RepositoryInstance.Diff, this.repositoryCache);
28-
public ICommitCollection Commits => new CommitCollection(RepositoryInstance.Commits, RepositoryInstance.Diff, this.repositoryCache);
29-
public IRemoteCollection Remotes => new RemoteCollection(RepositoryInstance.Network.Remotes, this.repositoryCache);
30-
public IReferenceCollection References => new ReferenceCollection(RepositoryInstance.Refs, this.repositoryCache);
30+
31+
private ICommitCollection? commits;
32+
public ICommitCollection Commits => this.commits ??= new CommitCollection(RepositoryInstance.Commits, RepositoryInstance.Diff, this.repositoryCache);
33+
34+
private IRemoteCollection? remotes;
35+
public IRemoteCollection Remotes => this.remotes ??= new RemoteCollection(RepositoryInstance.Network.Remotes, this.repositoryCache);
36+
37+
private IReferenceCollection? references;
38+
public IReferenceCollection References => this.references ??= new ReferenceCollection(RepositoryInstance.Refs, this.repositoryCache);
3139

3240
public void DiscoverRepository(string? gitDirectory)
3341
{

0 commit comments

Comments
 (0)