Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 88adbf1

Browse files
Merge pull request #119 from github-for-unity/fixes/changes-view-performance
Fix to improve performance of ChangesTreeView
2 parents 4aeda82 + cb54c5e commit 88adbf1

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/GitHub.Api/UI/TreeBuilder.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace GitHub.Unity
66
{
77
static class TreeBuilder
88
{
9-
internal static void BuildChildNode(FileTreeNode parent, FileTreeNode node, List<string> foldedTreeEntries)
9+
internal static void BuildChildNode(FileTreeNode parent, FileTreeNode node, HashSet<string> foldedTreeSet)
1010
{
1111
if (String.IsNullOrEmpty(node.Label))
1212
{
@@ -15,7 +15,7 @@ internal static void BuildChildNode(FileTreeNode parent, FileTreeNode node, List
1515
}
1616

1717
node.RepositoryPath = parent.RepositoryPath.ToNPath().Combine(node.Label);
18-
parent.Open = !foldedTreeEntries.Contains(parent.RepositoryPath);
18+
parent.Open = !foldedTreeSet.Contains(parent.RepositoryPath);
1919

2020
// Is this node inside a folder?
2121
var nodePath = node.Label.ToNPath();
@@ -33,7 +33,7 @@ internal static void BuildChildNode(FileTreeNode parent, FileTreeNode node, List
3333
if (child.Label.Equals(root))
3434
{
3535
found = true;
36-
BuildChildNode(child, node, foldedTreeEntries);
36+
BuildChildNode(child, node, foldedTreeSet);
3737
break;
3838
}
3939
}
@@ -42,20 +42,21 @@ internal static void BuildChildNode(FileTreeNode parent, FileTreeNode node, List
4242
if (!found)
4343
{
4444
var p = parent.RepositoryPath.ToNPath().Combine(root);
45-
BuildChildNode(parent.Add(new FileTreeNode(root) { RepositoryPath = p }), node, foldedTreeEntries);
45+
BuildChildNode(parent.Add(new FileTreeNode(root) { RepositoryPath = p }), node, foldedTreeSet);
4646
}
4747
}
4848
else if (nodePath.ExtensionWithDot == ".meta")
4949
{
5050
// Look for a branch matching our root in the existing children
5151
var found = false;
52+
var searchLabel = nodePath.Parent.Combine(nodePath.FileNameWithoutExtension);
5253
foreach (var child in parent.Children)
5354
{
5455
// If we found the branch, continue building from that branch
55-
if (child.Label.Equals(nodePath.Parent.Combine(nodePath.FileNameWithoutExtension)))
56+
if (child.Label.Equals(searchLabel))
5657
{
5758
found = true;
58-
BuildChildNode(child, node, foldedTreeEntries);
59+
BuildChildNode(child, node, foldedTreeSet);
5960
break;
6061
}
6162
}
@@ -75,10 +76,13 @@ internal static FileTreeNode BuildTreeRoot(IList<GitStatusEntry> newEntries, Lis
7576
{
7677
Guard.ArgumentNotNullOrEmpty(newEntries, "newEntries");
7778

79+
var newEntriesSetByPath = new HashSet<string>(newEntries.Select(entry => entry.Path));
80+
var gitStatusEntriesSetByPath = new HashSet<string>(gitStatusEntries.Select(entry => entry.Path));
81+
7882
// Remove what got nuked
7983
for (var index = 0; index < gitStatusEntries.Count;)
8084
{
81-
if (!newEntries.Contains(gitStatusEntries[index]))
85+
if (!newEntriesSetByPath.Contains(gitStatusEntries[index].Path))
8286
{
8387
gitStatusEntries.RemoveAt(index);
8488
gitCommitTargets.RemoveAt(index);
@@ -92,7 +96,7 @@ internal static FileTreeNode BuildTreeRoot(IList<GitStatusEntry> newEntries, Lis
9296
// Remove folding state of nuked items
9397
for (var index = 0; index < foldedTreeEntries.Count;)
9498
{
95-
if (!newEntries.Any(e => e.Path.IndexOf(foldedTreeEntries[index]) == 0))
99+
if (newEntries.All(e => e.Path.IndexOf(foldedTreeEntries[index], StringComparison.CurrentCulture) != 0))
96100
{
97101
foldedTreeEntries.RemoveAt(index);
98102
}
@@ -102,11 +106,13 @@ internal static FileTreeNode BuildTreeRoot(IList<GitStatusEntry> newEntries, Lis
102106
}
103107
}
104108

109+
var foldedTreeSet = new HashSet<string>(foldedTreeEntries);
110+
105111
// Add new stuff
106112
for (var index = 0; index < newEntries.Count; ++index)
107113
{
108114
var entry = newEntries[index];
109-
if (!gitStatusEntries.Contains(entry))
115+
if (!gitStatusEntriesSetByPath.Contains(entry.Path))
110116
{
111117
gitStatusEntries.Add(entry);
112118
gitCommitTargets.Add(new GitCommitTarget());
@@ -132,7 +138,7 @@ internal static FileTreeNode BuildTreeRoot(IList<GitStatusEntry> newEntries, Lis
132138
node.Icon = iconLoaderFunc?.Invoke(gitStatusEntry.ProjectPath);
133139
}
134140

135-
BuildChildNode(tree, node, foldedTreeEntries);
141+
BuildChildNode(tree, node, foldedTreeSet);
136142
}
137143

138144
return tree;

0 commit comments

Comments
 (0)