Skip to content

Commit ec25813

Browse files
committed
attempt to fix performance issues with a cache
1 parent dad5d2c commit ec25813

File tree

7 files changed

+62
-24
lines changed

7 files changed

+62
-24
lines changed

src/GitVersion.Core/Core/RepositoryStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public IReadOnlyList<ICommit> GetCommitLog(ICommit? baseVersionSource, ICommit c
219219
SortBy = CommitSortStrategies.Topological | CommitSortStrategies.Time
220220
};
221221

222-
var commits = this.repository.Commits.QueryBy(filter).ToArray();
222+
var commits = this.repository.Commits.QueryBy(filter);
223223

224224
return ignore.Filter(commits).ToList();
225225
}

src/GitVersion.LibGit2Sharp/Git/BranchCollection.cs

Lines changed: 8 additions & 5 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,12 +31,11 @@ public IBranch? this[string name]
2731

2832
public IEnumerable<IBranch> ExcludeBranches(IEnumerable<IBranch> branchesToExclude)
2933
{
30-
branchesToExclude = branchesToExclude.NotNull();
34+
var toExclude = branchesToExclude as IBranch[] ?? branchesToExclude.ToArray();
3135

3236
return this.Where(BranchIsNotExcluded);
3337

34-
bool BranchIsNotExcluded(IBranch branch)
35-
=> branchesToExclude.All(branchToExclude => !branch.Equals(branchToExclude));
38+
bool BranchIsNotExcluded(IBranch branch) => toExclude.All(branchToExclude => !branch.Equals(branchToExclude));
3639
}
3740

3841
public void UpdateTrackedBranch(IBranch branch, string remoteTrackingReferenceName)

src/GitVersion.LibGit2Sharp/Git/CommitCollection.cs

Lines changed: 14 additions & 3 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

@@ -21,7 +26,13 @@ public IEnumerable<ICommit> QueryBy(CommitFilter commitFilter)
2126
{
2227
var includeReachableFrom = GetReacheableFrom(commitFilter.IncludeReachableFrom);
2328
var excludeReachableFrom = GetReacheableFrom(commitFilter.ExcludeReachableFrom);
24-
var filter = new LibGit2Sharp.CommitFilter { IncludeReachableFrom = includeReachableFrom, ExcludeReachableFrom = excludeReachableFrom, FirstParentOnly = commitFilter.FirstParentOnly, SortBy = (LibGit2Sharp.CommitSortStrategies)commitFilter.SortBy };
29+
var filter = new LibGit2Sharp.CommitFilter
30+
{
31+
IncludeReachableFrom = includeReachableFrom,
32+
ExcludeReachableFrom = excludeReachableFrom,
33+
FirstParentOnly = commitFilter.FirstParentOnly,
34+
SortBy = (LibGit2Sharp.CommitSortStrategies)commitFilter.SortBy
35+
};
2536
var commitLog = ((IQueryableCommitLog)this.innerCollection).QueryBy(filter);
2637
return new CommitCollection(commitLog);
2738

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)