Skip to content

Commit

Permalink
Use array instead of List<RevisionGraphRevision>
Browse files Browse the repository at this point in the history
  • Loading branch information
mstv committed Mar 31, 2021
1 parent d1bc1c8 commit ab29481
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions GitUI/UserControls/RevisionGrid/Graph/RevisionGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class RevisionGraph : IRevisionGraphRowProvider
/// This is used so we can draw commits before the graph building is complete.
/// </summary>
/// <remarks>This cache is very cheap to build.</remarks>
private List<RevisionGraphRevision>? _orderedNodesCache;
private RevisionGraphRevision[]? _orderedNodesCache;
private bool _reorder = true;
private int _orderedUntilScore = -1;

Expand Down Expand Up @@ -89,7 +89,7 @@ public int GetCachedCount()
public void CacheTo(int currentRowIndex, int lastToCacheRowIndex)
{
currentRowIndex += _straightenLanesLookAhead;
List<RevisionGraphRevision> orderedNodesCache = BuildOrderedNodesCache(currentRowIndex);
RevisionGraphRevision[] orderedNodesCache = BuildOrderedNodesCache(currentRowIndex);

BuildOrderedRowCache(orderedNodesCache, currentRowIndex, lastToCacheRowIndex);
}
Expand Down Expand Up @@ -123,15 +123,15 @@ public bool TryGetRowIndex(ObjectId objectId, out int index)
return false;
}

index = BuildOrderedNodesCache(Count).IndexOf(revision);
index = Array.IndexOf(BuildOrderedNodesCache(Count), revision);
return index >= 0;
}

public RevisionGraphRevision? GetNodeForRow(int row)
{
// Use a local variable, because the cached list can be reset
var localOrderedNodesCache = BuildOrderedNodesCache(row);
if (row >= localOrderedNodesCache.Count)
if (row >= localOrderedNodesCache.Length)
{
return null;
}
Expand Down Expand Up @@ -231,12 +231,12 @@ public void Add(GitRevision revision, RevisionNodeFlags types)
/// is not in the same index in the orderednodecache, the order has been changed. Only then rebuilding is
/// required. If the order is changed after this revision, we do not care since it wasn't processed yet.
/// </summary>
private bool CheckRowCacheIsDirty(IList<RevisionGraphRow> orderedRowCache, IList<RevisionGraphRevision> orderedNodesCache)
private bool CheckRowCacheIsDirty(IList<RevisionGraphRow> orderedRowCache, RevisionGraphRevision[] orderedNodesCache)
{
// We need bounds checking on orderedNodesCache. It should be always larger then the rowcache,
// but another thread could clear the orderedNodesCache while another is building orderedRowCache.
// This is not a problem, since all methods use local instances of those caches. We do need to invalidate.
if (orderedRowCache.Count > orderedNodesCache.Count)
if (orderedRowCache.Count > orderedNodesCache.Length)
{
return true;
}
Expand All @@ -250,7 +250,7 @@ private bool CheckRowCacheIsDirty(IList<RevisionGraphRow> orderedRowCache, IList
return orderedRowCache[indexToCompare].Revision != orderedNodesCache[indexToCompare];
}

private void BuildOrderedRowCache(IList<RevisionGraphRevision> orderedNodesCache, int currentRowIndex, int lastToCacheRowIndex)
private void BuildOrderedRowCache(RevisionGraphRevision[] orderedNodesCache, int currentRowIndex, int lastToCacheRowIndex)
{
// Ensure we keep using the same instance of the rowcache from here on
IList<RevisionGraphRow>? localOrderedRowCache = _orderedRowCache;
Expand All @@ -260,7 +260,7 @@ private void BuildOrderedRowCache(IList<RevisionGraphRevision> orderedNodesCache
localOrderedRowCache = new List<RevisionGraphRow>(currentRowIndex);
}

lastToCacheRowIndex = Math.Min(lastToCacheRowIndex, orderedNodesCache.Count - 1);
lastToCacheRowIndex = Math.Min(lastToCacheRowIndex, orderedNodesCache.Length - 1);
int startIndex = localOrderedRowCache.Count;
if (startIndex > lastToCacheRowIndex)
{
Expand Down Expand Up @@ -375,9 +375,9 @@ static void StraightenLanes(int startIndex, int lastIndex, IList<RevisionGraphRo
}
}

private List<RevisionGraphRevision> BuildOrderedNodesCache(int currentRowIndex)
private RevisionGraphRevision[] BuildOrderedNodesCache(int currentRowIndex)
{
if (_orderedNodesCache is not null && !_reorder && _orderedNodesCache.Count >= Math.Min(Count, currentRowIndex))
if (_orderedNodesCache is not null && !_reorder && _orderedNodesCache.Length >= Math.Min(Count, currentRowIndex))
{
return _orderedNodesCache;
}
Expand All @@ -388,10 +388,10 @@ private List<RevisionGraphRevision> BuildOrderedNodesCache(int currentRowIndex)
_reorder = false;

// Use a local variable, because the cached list can be reset
var localOrderedNodesCache = _nodes.ToList();
localOrderedNodesCache.Sort((x, y) => x.Score.CompareTo(y.Score));
var localOrderedNodesCache = _nodes.ToArray();
Array.Sort(localOrderedNodesCache, (x, y) => x.Score.CompareTo(y.Score));
_orderedNodesCache = localOrderedNodesCache;
if (localOrderedNodesCache.Count > 0)
if (localOrderedNodesCache.Length > 0)
{
_orderedUntilScore = localOrderedNodesCache.Last().Score;
}
Expand Down

0 comments on commit ab29481

Please sign in to comment.