Skip to content

Commit 825f62c

Browse files
arturcicdavid-driscoll
authored andcommitted
attempt to fix performance issues with a cache
1 parent 59556b6 commit 825f62c

File tree

6 files changed

+54
-23
lines changed

6 files changed

+54
-23
lines changed

src/GitVersion.LibGit2Sharp/Git/BranchCollection.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ namespace GitVersion.Git;
66
internal sealed class BranchCollection : IBranchCollection
77
{
88
private readonly LibGit2Sharp.BranchCollection innerCollection;
9+
private readonly Lazy<IReadOnlyCollection<IBranch>> branches;
910

1011
internal BranchCollection(LibGit2Sharp.BranchCollection collection)
11-
=> this.innerCollection = collection.NotNull();
12+
{
13+
this.innerCollection = collection.NotNull();
14+
this.branches = new Lazy<IReadOnlyCollection<IBranch>>(() => this.innerCollection.Select(branch => new Branch(branch)).ToArray());
15+
}
1216

1317
public IEnumerator<IBranch> GetEnumerator()
14-
=> this.innerCollection.Select(branch => new Branch(branch)).GetEnumerator();
18+
=> this.branches.Value.GetEnumerator();
1519

1620
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
1721

@@ -27,10 +31,8 @@ public IBranch? this[string name]
2731

2832
public IEnumerable<IBranch> ExcludeBranches(IEnumerable<IBranch> branchesToExclude)
2933
{
30-
branchesToExclude = branchesToExclude.NotNull();
31-
32-
bool BranchIsNotExcluded(IBranch branch)
33-
=> branchesToExclude.All(branchToExclude => !branch.Equals(branchToExclude));
34+
var toExclude = branchesToExclude as IBranch[] ?? branchesToExclude.ToArray();
35+
bool BranchIsNotExcluded(IBranch branch) => toExclude.All(branchToExclude => !branch.Equals(branchToExclude));
3436

3537
return this.Where(BranchIsNotExcluded);
3638
}

src/GitVersion.LibGit2Sharp/Git/CommitCollection.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ namespace GitVersion.Git;
66
internal sealed class CommitCollection : ICommitCollection
77
{
88
private readonly ICommitLog innerCollection;
9+
private readonly Lazy<IReadOnlyCollection<ICommit>> commits;
910

10-
internal CommitCollection(ICommitLog collection) => this.innerCollection = collection.NotNull();
11+
internal CommitCollection(ICommitLog collection)
12+
{
13+
this.innerCollection = collection.NotNull();
14+
this.commits = new Lazy<IReadOnlyCollection<ICommit>>(() => this.innerCollection.Select(commit => new Commit(commit)).ToArray());
15+
}
1116

1217
public IEnumerator<ICommit> GetEnumerator()
13-
=> this.innerCollection.Select(commit => new Commit(commit)).GetEnumerator();
18+
=> this.commits.Value.GetEnumerator();
1419

1520
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
1621

src/GitVersion.LibGit2Sharp/Git/RefSpecCollection.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ namespace GitVersion.Git;
44

55
internal sealed class RefSpecCollection : IRefSpecCollection
66
{
7-
private readonly LibGit2Sharp.RefSpecCollection innerCollection;
7+
private readonly Lazy<IReadOnlyCollection<IRefSpec>> refSpecs;
88

99
internal RefSpecCollection(LibGit2Sharp.RefSpecCollection collection)
10-
=> this.innerCollection = collection.NotNull();
10+
{
11+
collection = collection.NotNull();
12+
this.refSpecs = new Lazy<IReadOnlyCollection<IRefSpec>>(() => collection.Select(tag => new RefSpec(tag)).ToArray());
13+
}
1114

12-
public IEnumerator<IRefSpec> GetEnumerator() => this.innerCollection.Select(tag => new RefSpec(tag)).GetEnumerator();
15+
public IEnumerator<IRefSpec> GetEnumerator() => this.refSpecs.Value.GetEnumerator();
1316
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
1417
}

src/GitVersion.LibGit2Sharp/Git/ReferenceCollection.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@ namespace GitVersion.Git;
55
internal sealed class ReferenceCollection : IReferenceCollection
66
{
77
private readonly LibGit2Sharp.ReferenceCollection innerCollection;
8+
private IReadOnlyCollection<IReference>? references;
89

9-
internal ReferenceCollection(LibGit2Sharp.ReferenceCollection collection)
10-
=> this.innerCollection = collection.NotNull();
10+
internal ReferenceCollection(LibGit2Sharp.ReferenceCollection collection) => this.innerCollection = collection.NotNull();
1111

12-
public IEnumerator<IReference> GetEnumerator() => this.innerCollection.Select(reference => new Reference(reference)).GetEnumerator();
12+
public IEnumerator<IReference> GetEnumerator()
13+
{
14+
this.references ??= this.innerCollection.Select(reference => new Reference(reference)).ToArray();
15+
return this.references.GetEnumerator();
16+
}
1317

1418
public void Add(string name, string canonicalRefNameOrObject, bool allowOverwrite = false) => this.innerCollection.Add(name, canonicalRefNameOrObject, allowOverwrite);
1519

16-
public void UpdateTarget(IReference directRef, IObjectId targetId) => RepositoryExtensions.RunSafe(() => this.innerCollection.UpdateTarget((Reference)directRef, (ObjectId)targetId));
20+
public void UpdateTarget(IReference directRef, IObjectId targetId)
21+
{
22+
RepositoryExtensions.RunSafe(() => this.innerCollection.UpdateTarget((Reference)directRef, (ObjectId)targetId));
23+
this.references = null;
24+
}
1725

1826
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
1927

src/GitVersion.LibGit2Sharp/Git/RemoteCollection.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ namespace GitVersion.Git;
55
internal sealed class RemoteCollection : IRemoteCollection
66
{
77
private readonly LibGit2Sharp.RemoteCollection innerCollection;
8+
private IReadOnlyCollection<IRemote>? remotes;
89

9-
internal RemoteCollection(LibGit2Sharp.RemoteCollection collection)
10-
=> this.innerCollection = collection.NotNull();
10+
internal RemoteCollection(LibGit2Sharp.RemoteCollection collection) => this.innerCollection = collection.NotNull();
1111

1212
public IEnumerator<IRemote> GetEnumerator()
13-
=> this.innerCollection.Select(reference => new Remote(reference)).GetEnumerator();
13+
{
14+
this.remotes ??= this.innerCollection.Select(reference => new Remote(reference)).ToArray();
15+
return this.remotes.GetEnumerator();
16+
}
1417

1518
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
1619

@@ -23,8 +26,15 @@ public IRemote? this[string name]
2326
}
2427
}
2528

26-
public void Remove(string remoteName) => this.innerCollection.Remove(remoteName);
29+
public void Remove(string remoteName)
30+
{
31+
this.innerCollection.Remove(remoteName);
32+
this.remotes = null;
33+
}
2734

2835
public void Update(string remoteName, string refSpec)
29-
=> this.innerCollection.Update(remoteName, r => r.FetchRefSpecs.Add(refSpec));
36+
{
37+
this.innerCollection.Update(remoteName, r => r.FetchRefSpecs.Add(refSpec));
38+
this.remotes = null;
39+
}
3040
}

src/GitVersion.LibGit2Sharp/Git/TagCollection.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ namespace GitVersion.Git;
44

55
internal sealed class TagCollection : ITagCollection
66
{
7-
private readonly LibGit2Sharp.TagCollection innerCollection;
7+
private readonly Lazy<IReadOnlyCollection<ITag>> tags;
88

99
internal TagCollection(LibGit2Sharp.TagCollection collection)
10-
=> this.innerCollection = collection.NotNull();
10+
{
11+
collection = collection.NotNull();
12+
this.tags = new Lazy<IReadOnlyCollection<ITag>>(() => collection.Select(tag => new Tag(tag)).ToArray());
13+
}
1114

1215
public IEnumerator<ITag> GetEnumerator()
13-
=> this.innerCollection.Select(tag => new Tag(tag)).GetEnumerator();
16+
=> this.tags.Value.GetEnumerator();
1417

1518
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
1619
}

0 commit comments

Comments
 (0)