Skip to content

Commit

Permalink
perf: Improve height calculation performance (#99)
Browse files Browse the repository at this point in the history
Changed algorithm to compare commit with previous one,
instead of always comparing with HEAD. This way complexity is
dropping from O(n^2) to O(n), as each change is processed only once.

* Remove rename detection to speed up program
* Eliminate unnecessary reversal to speed up code
  • Loading branch information
zvirja authored and Kieranties committed Dec 18, 2019
1 parent 2fc7e0b commit fc7bac0
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/SimpleVersion.Core/Pipeline/ConfigurationContextProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,33 @@ public void Apply(IVersionContext context)

private void PopulateHeight(VersionContext context)
{
// Get the state of this tree to compare for diffs
var tipTree = context.Repository.Head.Tip.Tree;

// Initialize count - The current commit counts, include offset
var height = 1 + context.Configuration.OffSet;

// skip the first commit as that is our baseline
var commits = GetReachableCommits(context.Repository).Skip(1).GetEnumerator();
var commits = GetReachableCommits(context.Repository).Skip(1);

while (commits.MoveNext())
// Get the state of this tree to compare for diffs
var prevTree = context.Repository.Head.Tip.Tree;
foreach (var commit in commits)
{
// Get the current tree
var next = commits.Current.Tree;
var currentTree = commit.Tree;

// Perform a diff
var diff = context.Repository.Diff.Compare<TreeChanges>(next, tipTree);
var diff = context.Repository.Diff.Compare<TreeChanges>(currentTree, prevTree, new CompareOptions { Similarity = SimilarityOptions.None });

// If a change to the file is found, stop counting
if (HasVersionChange(diff, commits.Current, context))
if (HasVersionChange(diff, commit, context))
break;

// Increment height
height++;

// Use current commit as a base for next iteration, instead of accessing the tip.
// This way we don't re-check same commit multiple times.
// Must make no difference as changes have accumulative nature.
prevTree = currentTree;
}

context.Result.Height = height;
Expand All @@ -86,10 +90,10 @@ private static IEnumerable<Commit> GetReachableCommits(IRepository repo)
{
FirstParentOnly = true,
IncludeReachableFrom = repo.Head,
SortBy = CommitSortStrategies.Reverse
SortBy = CommitSortStrategies.Topological
};

return repo.Commits.QueryBy(filter).Reverse();
return repo.Commits.QueryBy(filter);
}

private static SVM.Configuration GetConfiguration(Commit commit, VersionContext context)
Expand Down

0 comments on commit fc7bac0

Please sign in to comment.