Skip to content

Commit

Permalink
attempt to fix performance issues with a cache
Browse files Browse the repository at this point in the history
  • Loading branch information
arturcic committed Oct 15, 2024
1 parent dad5d2c commit ec25813
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/GitVersion.Core/Core/RepositoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public IReadOnlyList<ICommit> GetCommitLog(ICommit? baseVersionSource, ICommit c
SortBy = CommitSortStrategies.Topological | CommitSortStrategies.Time
};

var commits = this.repository.Commits.QueryBy(filter).ToArray();
var commits = this.repository.Commits.QueryBy(filter);

return ignore.Filter(commits).ToList();
}
Expand Down
13 changes: 8 additions & 5 deletions src/GitVersion.LibGit2Sharp/Git/BranchCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ namespace GitVersion.Git;
internal sealed class BranchCollection : IBranchCollection
{
private readonly LibGit2Sharp.BranchCollection innerCollection;
private readonly Lazy<IReadOnlyCollection<IBranch>> branches;

internal BranchCollection(LibGit2Sharp.BranchCollection collection)
=> this.innerCollection = collection.NotNull();
{
this.innerCollection = collection.NotNull();
this.branches = new Lazy<IReadOnlyCollection<IBranch>>(() => this.innerCollection.Select(branch => new Branch(branch)).ToArray());
}

public IEnumerator<IBranch> GetEnumerator()
=> this.innerCollection.Select(branch => new Branch(branch)).GetEnumerator();
=> this.branches.Value.GetEnumerator();

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

Expand All @@ -27,12 +31,11 @@ public IBranch? this[string name]

public IEnumerable<IBranch> ExcludeBranches(IEnumerable<IBranch> branchesToExclude)
{
branchesToExclude = branchesToExclude.NotNull();
var toExclude = branchesToExclude as IBranch[] ?? branchesToExclude.ToArray();

return this.Where(BranchIsNotExcluded);

bool BranchIsNotExcluded(IBranch branch)
=> branchesToExclude.All(branchToExclude => !branch.Equals(branchToExclude));
bool BranchIsNotExcluded(IBranch branch) => toExclude.All(branchToExclude => !branch.Equals(branchToExclude));
}

public void UpdateTrackedBranch(IBranch branch, string remoteTrackingReferenceName)
Expand Down
17 changes: 14 additions & 3 deletions src/GitVersion.LibGit2Sharp/Git/CommitCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ namespace GitVersion.Git;
internal sealed class CommitCollection : ICommitCollection
{
private readonly ICommitLog innerCollection;
private readonly Lazy<IReadOnlyCollection<ICommit>> commits;

internal CommitCollection(ICommitLog collection) => this.innerCollection = collection.NotNull();
internal CommitCollection(ICommitLog collection)
{
this.innerCollection = collection.NotNull();
this.commits = new Lazy<IReadOnlyCollection<ICommit>>(() => this.innerCollection.Select(commit => new Commit(commit)).ToArray());
}

public IEnumerator<ICommit> GetEnumerator()
=> this.innerCollection.Select(commit => new Commit(commit)).GetEnumerator();
=> this.commits.Value.GetEnumerator();

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

Expand All @@ -21,7 +26,13 @@ public IEnumerable<ICommit> QueryBy(CommitFilter commitFilter)
{
var includeReachableFrom = GetReacheableFrom(commitFilter.IncludeReachableFrom);
var excludeReachableFrom = GetReacheableFrom(commitFilter.ExcludeReachableFrom);
var filter = new LibGit2Sharp.CommitFilter { IncludeReachableFrom = includeReachableFrom, ExcludeReachableFrom = excludeReachableFrom, FirstParentOnly = commitFilter.FirstParentOnly, SortBy = (LibGit2Sharp.CommitSortStrategies)commitFilter.SortBy };
var filter = new LibGit2Sharp.CommitFilter
{
IncludeReachableFrom = includeReachableFrom,
ExcludeReachableFrom = excludeReachableFrom,
FirstParentOnly = commitFilter.FirstParentOnly,
SortBy = (LibGit2Sharp.CommitSortStrategies)commitFilter.SortBy
};
var commitLog = ((IQueryableCommitLog)this.innerCollection).QueryBy(filter);
return new CommitCollection(commitLog);

Expand Down
9 changes: 6 additions & 3 deletions src/GitVersion.LibGit2Sharp/Git/RefSpecCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ namespace GitVersion.Git;

internal sealed class RefSpecCollection : IRefSpecCollection
{
private readonly LibGit2Sharp.RefSpecCollection innerCollection;
private readonly Lazy<IReadOnlyCollection<IRefSpec>> refSpecs;

internal RefSpecCollection(LibGit2Sharp.RefSpecCollection collection)
=> this.innerCollection = collection.NotNull();
{
collection = collection.NotNull();
this.refSpecs = new Lazy<IReadOnlyCollection<IRefSpec>>(() => collection.Select(tag => new RefSpec(tag)).ToArray());
}

public IEnumerator<IRefSpec> GetEnumerator() => this.innerCollection.Select(tag => new RefSpec(tag)).GetEnumerator();
public IEnumerator<IRefSpec> GetEnumerator() => this.refSpecs.Value.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
16 changes: 12 additions & 4 deletions src/GitVersion.LibGit2Sharp/Git/ReferenceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@ namespace GitVersion.Git;
internal sealed class ReferenceCollection : IReferenceCollection
{
private readonly LibGit2Sharp.ReferenceCollection innerCollection;
private IReadOnlyCollection<IReference>? references;

internal ReferenceCollection(LibGit2Sharp.ReferenceCollection collection)
=> this.innerCollection = collection.NotNull();
internal ReferenceCollection(LibGit2Sharp.ReferenceCollection collection) => this.innerCollection = collection.NotNull();

public IEnumerator<IReference> GetEnumerator() => this.innerCollection.Select(reference => new Reference(reference)).GetEnumerator();
public IEnumerator<IReference> GetEnumerator()
{
this.references ??= this.innerCollection.Select(reference => new Reference(reference)).ToArray();
return this.references.GetEnumerator();
}

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

public void UpdateTarget(IReference directRef, IObjectId targetId) => RepositoryExtensions.RunSafe(() => this.innerCollection.UpdateTarget((Reference)directRef, (ObjectId)targetId));
public void UpdateTarget(IReference directRef, IObjectId targetId)
{
RepositoryExtensions.RunSafe(() => this.innerCollection.UpdateTarget((Reference)directRef, (ObjectId)targetId));
this.references = null;
}

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

Expand Down
20 changes: 15 additions & 5 deletions src/GitVersion.LibGit2Sharp/Git/RemoteCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ namespace GitVersion.Git;
internal sealed class RemoteCollection : IRemoteCollection
{
private readonly LibGit2Sharp.RemoteCollection innerCollection;
private IReadOnlyCollection<IRemote>? remotes;

internal RemoteCollection(LibGit2Sharp.RemoteCollection collection)
=> this.innerCollection = collection.NotNull();
internal RemoteCollection(LibGit2Sharp.RemoteCollection collection) => this.innerCollection = collection.NotNull();

public IEnumerator<IRemote> GetEnumerator()
=> this.innerCollection.Select(reference => new Remote(reference)).GetEnumerator();
{
this.remotes ??= this.innerCollection.Select(reference => new Remote(reference)).ToArray();
return this.remotes.GetEnumerator();
}

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

Expand All @@ -23,8 +26,15 @@ public IRemote? this[string name]
}
}

public void Remove(string remoteName) => this.innerCollection.Remove(remoteName);
public void Remove(string remoteName)
{
this.innerCollection.Remove(remoteName);
this.remotes = null;
}

public void Update(string remoteName, string refSpec)
=> this.innerCollection.Update(remoteName, r => r.FetchRefSpecs.Add(refSpec));
{
this.innerCollection.Update(remoteName, r => r.FetchRefSpecs.Add(refSpec));
this.remotes = null;
}
}
9 changes: 6 additions & 3 deletions src/GitVersion.LibGit2Sharp/Git/TagCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ namespace GitVersion.Git;

internal sealed class TagCollection : ITagCollection
{
private readonly LibGit2Sharp.TagCollection innerCollection;
private readonly Lazy<IReadOnlyCollection<ITag>> tags;

internal TagCollection(LibGit2Sharp.TagCollection collection)
=> this.innerCollection = collection.NotNull();
{
collection = collection.NotNull();
this.tags = new Lazy<IReadOnlyCollection<ITag>>(() => collection.Select(tag => new Tag(tag)).ToArray());
}

public IEnumerator<ITag> GetEnumerator()
=> this.innerCollection.Select(tag => new Tag(tag)).GetEnumerator();
=> this.tags.Value.GetEnumerator();

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

0 comments on commit ec25813

Please sign in to comment.