Skip to content

Commit 064d04f

Browse files
committed
enhance: improve QueryCommits performance
1 parent 1a18235 commit 064d04f

File tree

7 files changed

+72
-64
lines changed

7 files changed

+72
-64
lines changed

src/Commands/QueryCommits.cs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Text;
34

45
namespace SourceGit.Commands
56
{
@@ -31,15 +32,14 @@ protected override void OnReadline(string line)
3132
{
3233
case 0:
3334
_current = new Models.Commit() { SHA = line };
35+
_isSubjectSet = false;
3436
_commits.Add(_current);
3537
break;
3638
case 1:
37-
if (!string.IsNullOrEmpty(line))
38-
_current.Parents.AddRange(line.Split(' ', StringSplitOptions.RemoveEmptyEntries));
39+
ParseParent(line);
3940
break;
4041
case 2:
41-
if (!string.IsNullOrEmpty(line))
42-
ParseDecorators(line);
42+
ParseDecorators(line);
4343
break;
4444
case 3:
4545
_current.Author = Models.User.FindOrAdd(line);
@@ -57,24 +57,45 @@ protected override void OnReadline(string line)
5757
if (line.Equals(_endOfBodyToken, StringComparison.Ordinal))
5858
{
5959
_nextPartIdx = 0;
60-
if (!string.IsNullOrEmpty(_current.Message))
61-
_current.Message = _current.Message.Trim();
60+
_current.Message = _messageReader.ToString().Trim();
61+
_messageReader.Clear();
62+
}
63+
else if (!_isSubjectSet)
64+
{
65+
_isSubjectSet = true;
66+
_current.Subject = line;
6267
}
6368
else
6469
{
65-
if (string.IsNullOrEmpty(_current.Subject))
66-
_current.Subject = line;
67-
else
68-
_current.Message += (line + "\n");
70+
_messageReader.AppendLine(line);
6971
}
7072
return;
7173
}
7274

7375
_nextPartIdx++;
7476
}
7577

78+
private void ParseParent(string data)
79+
{
80+
if (data.Length < 8)
81+
return;
82+
83+
var idx = data.IndexOf(' ', StringComparison.Ordinal);
84+
if (idx == -1)
85+
{
86+
_current.Parents.Add(data);
87+
return;
88+
}
89+
90+
_current.Parents.Add(data.Substring(0, idx));
91+
_current.Parents.Add(data.Substring(idx + 1));
92+
}
93+
7694
private void ParseDecorators(string data)
7795
{
96+
if (data.Length < 3)
97+
return;
98+
7899
var subs = data.Split(',', StringSplitOptions.RemoveEmptyEntries);
79100
foreach (var sub in subs)
80101
{
@@ -172,5 +193,7 @@ private void MarkFirstMerged()
172193
private bool _isHeadFounded = false;
173194
private readonly bool _findFirstMerged = true;
174195
private int _nextPartIdx = 0;
196+
private bool _isSubjectSet = false;
197+
private StringBuilder _messageReader = new StringBuilder();
175198
}
176199
}

src/Models/CommitGraph.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,23 @@
22
using System.Collections.Generic;
33

44
using Avalonia;
5+
using Avalonia.Media;
56

67
namespace SourceGit.Models
78
{
89
public class CommitGraph
910
{
11+
public static readonly Pen[] Pens = [
12+
new Pen(Brushes.Orange, 2),
13+
new Pen(Brushes.ForestGreen, 2),
14+
new Pen(Brushes.Gold, 2),
15+
new Pen(Brushes.Magenta, 2),
16+
new Pen(Brushes.Red, 2),
17+
new Pen(Brushes.Gray, 2),
18+
new Pen(Brushes.Turquoise, 2),
19+
new Pen(Brushes.Olive, 2),
20+
];
21+
1022
public class Path
1123
{
1224
public List<Point> Points = new List<Point>();
@@ -101,12 +113,12 @@ public class Dot
101113
public List<Link> Links { get; set; } = new List<Link>();
102114
public List<Dot> Dots { get; set; } = new List<Dot>();
103115

104-
public static CommitGraph Parse(List<Commit> commits, double rowHeight, int colorCount)
116+
public static CommitGraph Parse(List<Commit> commits, int colorCount)
105117
{
106118
double UNIT_WIDTH = 12;
107119
double HALF_WIDTH = 6;
108-
double UNIT_HEIGHT = rowHeight;
109-
double HALF_HEIGHT = rowHeight / 2;
120+
double UNIT_HEIGHT = 28;
121+
double HALF_HEIGHT = 14;
110122

111123
var temp = new CommitGraph();
112124
var unsolved = new List<PathHelper>();

src/ViewModels/Histories.cs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4-
using System.Threading.Tasks;
54

65
using Avalonia.Controls;
76
using Avalonia.Platform.Storage;
8-
using Avalonia.Threading;
97

108
using CommunityToolkit.Mvvm.ComponentModel;
119

@@ -24,38 +22,16 @@ public bool IsLoading
2422
set => SetProperty(ref _isLoading, value);
2523
}
2624

27-
public double DataGridRowHeight
28-
{
29-
get => _dataGridRowHeight;
30-
}
31-
3225
public List<Models.Commit> Commits
3326
{
3427
get => _commits;
3528
set
3629
{
37-
var oldAutoSelectedCommitSHA = AutoSelectedCommit?.SHA;
30+
var lastSelected = AutoSelectedCommit;
3831
if (SetProperty(ref _commits, value))
3932
{
40-
Models.Commit newSelectedCommit = null;
41-
if (value.Count > 0 && oldAutoSelectedCommitSHA != null)
42-
{
43-
newSelectedCommit = value.Find(x => x.SHA == oldAutoSelectedCommitSHA);
44-
}
45-
if (newSelectedCommit != AutoSelectedCommit)
46-
{
47-
AutoSelectedCommit = newSelectedCommit;
48-
}
49-
50-
Graph = null;
51-
Task.Run(() =>
52-
{
53-
var graph = Models.CommitGraph.Parse(value, DataGridRowHeight, 8);
54-
Dispatcher.UIThread.Invoke(() =>
55-
{
56-
Graph = graph;
57-
});
58-
});
33+
if (value.Count > 0 && lastSelected != null)
34+
AutoSelectedCommit = value.Find(x => x.SHA == lastSelected.SHA);
5935
}
6036
}
6137
}
@@ -652,7 +628,6 @@ private void FillTagMenu(ContextMenu menu, Models.Tag tag)
652628
}
653629

654630
private Repository _repo = null;
655-
private readonly double _dataGridRowHeight = 28;
656631
private bool _isLoading = true;
657632
private List<Models.Commit> _commits = new List<Models.Commit>();
658633
private Models.CommitGraph _graph = null;

src/ViewModels/Repository.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,12 +606,15 @@ public void RefreshCommits()
606606
}
607607

608608
var commits = new Commands.QueryCommits(FullPath, limits).Result();
609+
var graph = Models.CommitGraph.Parse(commits, 8);
610+
609611
Dispatcher.UIThread.Invoke(() =>
610612
{
611613
if (_histories != null)
612614
{
613615
_histories.IsLoading = false;
614616
_histories.Commits = commits;
617+
_histories.Graph = graph;
615618
}
616619
});
617620
}

src/Views/Histories.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
IsReadOnly="True"
2424
HeadersVisibility="None"
2525
Focusable="False"
26-
RowHeight="{Binding DataGridRowHeight}"
26+
RowHeight="28"
2727
HorizontalScrollBarVisibility="Disabled"
2828
VerticalScrollBarVisibility="Auto"
2929
LayoutUpdated="OnCommitDataGridLayoutUpdated"

src/Views/Histories.axaml.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,6 @@ private void RefreshLayout()
6969

7070
public class CommitGraph : Control
7171
{
72-
public static readonly Pen[] Pens = [
73-
new Pen(Brushes.Orange, 2),
74-
new Pen(Brushes.ForestGreen, 2),
75-
new Pen(Brushes.Gold, 2),
76-
new Pen(Brushes.Magenta, 2),
77-
new Pen(Brushes.Red, 2),
78-
new Pen(Brushes.Gray, 2),
79-
new Pen(Brushes.Turquoise, 2),
80-
new Pen(Brushes.Olive, 2),
81-
];
82-
8372
public static readonly StyledProperty<Models.CommitGraph> GraphProperty =
8473
AvaloniaProperty.Register<CommitGraph, Models.CommitGraph>(nameof(Graph));
8574

@@ -151,7 +140,7 @@ public override void Render(DrawingContext context)
151140
if (dot.Center.Y > bottom)
152141
break;
153142

154-
context.DrawEllipse(dotFill, Pens[dot.Color], dot.Center, 3, 3);
143+
context.DrawEllipse(dotFill, Models.CommitGraph.Pens[dot.Color], dot.Center, 3, 3);
155144
}
156145
}
157146

@@ -168,7 +157,7 @@ private void DrawCurves(DrawingContext context, double top, double bottom)
168157
continue;
169158

170159
var geo = new StreamGeometry();
171-
var pen = Pens[line.Color];
160+
var pen = Models.CommitGraph.Pens[line.Color];
172161
using (var ctx = geo.Open())
173162
{
174163
var started = false;
@@ -238,7 +227,7 @@ private void DrawCurves(DrawingContext context, double top, double bottom)
238227
ctx.QuadraticBezierTo(link.Control, link.End);
239228
}
240229

241-
context.DrawGeometry(null, Pens[link.Color], geo);
230+
context.DrawGeometry(null, Models.CommitGraph.Pens[link.Color], geo);
242231
}
243232
}
244233
}

src/Views/TextDiffView.axaml.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,25 +1166,31 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
11661166
{
11671167
base.OnPropertyChanged(change);
11681168

1169+
var data = TextDiff;
1170+
if (data == null)
1171+
{
1172+
Content = null;
1173+
SyncScrollOffset = Vector.Zero;
1174+
return;
1175+
}
1176+
11691177
if (change.Property == TextDiffProperty)
11701178
{
1171-
if (TextDiff == null)
1172-
Content = null;
1173-
else if (UseSideBySideDiff)
1179+
if (UseSideBySideDiff)
11741180
Content = new ViewModels.TwoSideTextDiff(TextDiff);
11751181
else
11761182
Content = TextDiff;
1183+
1184+
SetCurrentValue(SyncScrollOffsetProperty, TextDiff.SyncScrollOffset);
11771185
}
11781186
else if (change.Property == UseSideBySideDiffProperty)
11791187
{
1180-
SyncScrollOffset = Vector.Zero;
1181-
1182-
if (TextDiff == null)
1183-
Content = null;
1184-
else if (UseSideBySideDiff)
1188+
if (UseSideBySideDiff)
11851189
Content = new ViewModels.TwoSideTextDiff(TextDiff);
11861190
else
11871191
Content = TextDiff;
1192+
1193+
SetCurrentValue(SyncScrollOffsetProperty, Vector.Zero);
11881194
}
11891195
}
11901196

0 commit comments

Comments
 (0)