optimize<WorkingCopy>: improve loading time for large number of local changes

This commit is contained in:
leo 2022-11-11 15:01:44 +08:00
parent 4cd07d90a5
commit 5434629f4c
3 changed files with 154 additions and 87 deletions

View file

@ -324,9 +324,10 @@ namespace SourceGit.Views.Widgets {
Task.Run(() => { Task.Run(() => {
var changes = new Commands.LocalChanges(repo.Path, Models.Preference.Instance.Git.IncludeUntrackedInWC).Result(); var changes = new Commands.LocalChanges(repo.Path, Models.Preference.Instance.Git.IncludeUntrackedInWC).Result();
(pages.Get("working_copy") as WorkingCopy).SetData(changes);
Dispatcher.Invoke(() => { Dispatcher.Invoke(() => {
badgeLocalChanges.Label = $"{changes.Count}"; badgeLocalChanges.Label = $"{changes.Count}";
(pages.Get("working_copy") as WorkingCopy).SetData(changes);
UpdateMergeBar(changes); UpdateMergeBar(changes);
}); });
}); });

View file

@ -43,6 +43,7 @@ namespace SourceGit.Views.Widgets {
} }
} }
Dispatcher.Invoke(() => {
unstagedContainer.SetData(unstagedChanges); unstagedContainer.SetData(unstagedChanges);
stagedContainer.SetData(stagedChanges); stagedContainer.SetData(stagedChanges);
@ -73,6 +74,7 @@ namespace SourceGit.Views.Widgets {
} else { } else {
diffViewer.Reload(); diffViewer.Reload();
} }
});
} }
public void TryLoadMergeMessage() { public void TryLoadMergeMessage() {

View file

@ -173,7 +173,40 @@ namespace SourceGit.Views.Widgets {
public void SetData(List<Models.Change> changes) { public void SetData(List<Models.Change> changes) {
isLoadingData = true; isLoadingData = true;
var isDefaultExpand = changes.Count <= 50;
if (changes.Count == 0) {
Changes.Clear();
Nodes.Clear();
} else if (Changes.Count == 0) {
var nodesMap = new Dictionary<string, ChangeNode>();
foreach (var c in changes) {
Changes.Add(c);
int sepIdx = c.Path.IndexOf("/", StringComparison.Ordinal);
if (sepIdx < 0) {
var n = AddTreeNode(Nodes, c.Path, c, false, true);
nodesMap.Add(c.Path, n);
} else {
ObservableCollection<ChangeNode> last = Nodes;
do {
ChangeNode parent = null;
var path = c.Path.Substring(0, sepIdx);
if (!nodesMap.TryGetValue(path, out parent)) {
parent = AddTreeNode(last, path, null, isDefaultExpand, true);
nodesMap.Add(path, parent);
}
last = parent.Children;
sepIdx = c.Path.IndexOf('/', sepIdx + 1);
} while (sepIdx > 0);
var n = AddTreeNode(last, c.Path, c, false, true);
nodesMap.Add(c.Path, n);
}
}
nodesMap.Clear();
} else {
var oldSet = new Dictionary<string, Models.Change>(); var oldSet = new Dictionary<string, Models.Change>();
var newSet = new Dictionary<string, Models.Change>(); var newSet = new Dictionary<string, Models.Change>();
foreach (var c in changes) newSet.Add(c.Path, c); foreach (var c in changes) newSet.Add(c.Path, c);
@ -182,7 +215,7 @@ namespace SourceGit.Views.Widgets {
var old = Changes[i]; var old = Changes[i];
if (!newSet.ContainsKey(old.Path)) { if (!newSet.ContainsKey(old.Path)) {
Changes.RemoveAt(i); Changes.RemoveAt(i);
RemoveTreeNode(Nodes, old); RemoveTreeNode(Nodes, old.Path);
if (modeTree.Selected.Contains(old)) modeTree.Selected.Remove(old); if (modeTree.Selected.Contains(old)) modeTree.Selected.Remove(old);
if (DiffTarget == old) DiffTarget = null; if (DiffTarget == old) DiffTarget = null;
continue; continue;
@ -191,7 +224,7 @@ namespace SourceGit.Views.Widgets {
var cur = newSet[old.Path]; var cur = newSet[old.Path];
if (cur.Index != old.Index || cur.WorkTree != old.WorkTree) { if (cur.Index != old.Index || cur.WorkTree != old.WorkTree) {
Changes.RemoveAt(i); Changes.RemoveAt(i);
RemoveTreeNode(Nodes, old); RemoveTreeNode(Nodes, old.Path);
if (modeTree.Selected.Contains(old)) modeTree.Selected.Remove(old); if (modeTree.Selected.Contains(old)) modeTree.Selected.Remove(old);
if (DiffTarget == old) DiffTarget = null; if (DiffTarget == old) DiffTarget = null;
continue; continue;
@ -200,43 +233,52 @@ namespace SourceGit.Views.Widgets {
oldSet.Add(old.Path, old); oldSet.Add(old.Path, old);
} }
var isDefaultExpand = changes.Count <= 50; var nodesMap = new Dictionary<string, ChangeNode>();
foreach (var c in changes) { GetTreeNodes(nodesMap, Nodes);
for (int i = 0; i < changes.Count; i++) {
var c = changes[i];
if (oldSet.ContainsKey(c.Path)) continue; if (oldSet.ContainsKey(c.Path)) continue;
bool added = false;
for (int i = 0; i < Changes.Count; i++) {
if (c.Path.CompareTo(Changes[i].Path) < 0) {
Changes.Insert(i, c); Changes.Insert(i, c);
added = true;
break;
}
}
if (!added) Changes.Add(c);
int sepIdx = c.Path.IndexOf("/", StringComparison.Ordinal); int sepIdx = c.Path.IndexOf("/", StringComparison.Ordinal);
if (sepIdx < 0) { if (sepIdx < 0) {
GetOrAddTreeNode(Nodes, c.Path, c, false); var n = AddTreeNode(Nodes, c.Path, c, false, false);
nodesMap.Add(c.Path, n);
} else { } else {
ObservableCollection<ChangeNode> last = Nodes; ObservableCollection<ChangeNode> last = Nodes;
do { do {
ChangeNode parent = null;
var path = c.Path.Substring(0, sepIdx); var path = c.Path.Substring(0, sepIdx);
last = GetOrAddTreeNode(last, path, null, isDefaultExpand).Children; if (!nodesMap.TryGetValue(path, out parent)) {
parent = AddTreeNode(last, path, null, isDefaultExpand, false);
nodesMap.Add(path, parent);
}
last = parent.Children;
sepIdx = c.Path.IndexOf('/', sepIdx + 1); sepIdx = c.Path.IndexOf('/', sepIdx + 1);
} while (sepIdx > 0); } while (sepIdx > 0);
GetOrAddTreeNode(last, c.Path, c, false);
var n = AddTreeNode(last, c.Path, c, false, false);
nodesMap.Add(c.Path, n);
} }
} }
nodesMap.Clear();
}
isLoadingData = false; isLoadingData = false;
return;
} }
private ChangeNode GetOrAddTreeNode(ObservableCollection<ChangeNode> nodes, string path, Models.Change change, bool isExpand) { private void GetTreeNodes(Dictionary<string, ChangeNode> map, ObservableCollection<ChangeNode> nodes) {
foreach (var n in nodes) { foreach (var n in nodes) {
if (n.Path == path) return n; map.Add(n.Path, n);
if (n.IsFolder) GetTreeNodes(map, n.Children);
}
} }
private ChangeNode AddTreeNode(ObservableCollection<ChangeNode> nodes, string path, Models.Change change, bool isExpand, bool isSorted = false) {
var node = new ChangeNode(); var node = new ChangeNode();
node.Path = path; node.Path = path;
node.Change = change; node.Change = change;
@ -245,40 +287,62 @@ namespace SourceGit.Views.Widgets {
var added = false; var added = false;
if (change == null) { if (change == null) {
for (int i = 0; i < nodes.Count; i++) { for (int i = 0; i < nodes.Count; i++) {
if (!nodes[i].IsFolder || nodes[i].Path.CompareTo(path) > 0) { var n = nodes[i];
if (!n.IsFolder) {
added = true; added = true;
nodes.Add(node); nodes.Insert(i, node);
break;
}
if (isSorted) continue;
if (n.Path.CompareTo(path) > 0) {
added = true;
nodes.Insert(i, node);
break; break;
} }
} }
} else { } else {
for (int i = 0; i < nodes.Count; i++) { if (isSorted) {
if (nodes[i].IsFolder) continue;
if (nodes[i].Path.CompareTo(path) > 0) {
added = true; added = true;
nodes.Add(node); nodes.Add(node);
} else {
for (int i = 0; i < nodes.Count; i++) {
var n = nodes[i];
if (n.IsFolder) continue;
if (n.Path.CompareTo(path) > 0) {
added = true;
nodes.Insert(i, node);
break; break;
} }
} }
} }
}
if (!added) nodes.Add(node); if (!added) nodes.Add(node);
return node; return node;
} }
private bool RemoveTreeNode(ObservableCollection<ChangeNode> nodes, Models.Change change) { private bool RemoveTreeNode(ObservableCollection<ChangeNode> nodes, string path) {
for (int i = nodes.Count - 1; i >= 0; i--) { for (int i = nodes.Count - 1; i >= 0; i--) {
var node = nodes[i]; var node = nodes[i];
if (node.Change == null) { if (node.IsFolder) {
if (RemoveTreeNode(node.Children, change)) { if (path.IndexOf(node.Path, StringComparison.Ordinal) < 0) {
continue;
}
if (RemoveTreeNode(node.Children, path)) {
if (node.Children.Count == 0) nodes.RemoveAt(i); if (node.Children.Count == 0) nodes.RemoveAt(i);
return true; return true;
} }
} else if (node.Change.Path == change.Path) { } else {
if (path.Equals(node.Path, StringComparison.Ordinal)) {
nodes.RemoveAt(i); nodes.RemoveAt(i);
return true; return true;
} }
} }
}
return false; return false;
} }