Skip to content

Commit 5bc64f6

Browse files
claudebertyhell
authored andcommitted
enhance: generate commit graph off the UI thread
`CommitGraph.Generate` for up to `MaxHistoryCommits` (20000 by default) commits used to run on the UI thread after every history refresh (and twice when graph highlighting depends on selection), and again on every selection change when highlighting selected commits only. - `Repository.RefreshCommits` now builds the graph in its background task and publishes commits and graph together via `Histories.SetCommits`, so the list and the graph always describe the same commits - Selection-dependent regeneration runs in the background; stale results are dropped and the previous graph stays visible until the new one is ready - `CommitGraph.Generate` no longer sets `Commit.IsHighlightedInGraph` (an observable property bound by visible rows); it is applied from the generated dots on the UI thread instead Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KCxH1zwDXLd9kfAAkcf6PZ
1 parent 3e65576 commit 5bc64f6

3 files changed

Lines changed: 60 additions & 19 deletions

File tree

‎src/Models/CommitGraph.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,6 @@ public static CommitGraph Generate(List<Commit> commits, bool firstParentOnlyEna
174174
break;
175175
}
176176
}
177-
commit.IsHighlightedInGraph = isHighlighted;
178177

179178
// If no path found, create new curve for branch head
180179
// Otherwise, create new curve for new merged commit

‎src/ViewModels/Histories.cs‎

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Threading;
45
using System.Threading.Tasks;
56

67
using Avalonia.Collections;
@@ -110,12 +111,6 @@ public bool IsCommitTimeColumnVisible
110111
public List<Models.Commit> Commits
111112
{
112113
get => _commits;
113-
set
114-
{
115-
GenerateGraph(value);
116-
if (SetProperty(ref _commits, value))
117-
PostCommitsChanged();
118-
}
119114
}
120115

121116
public Models.CommitGraph Graph
@@ -132,7 +127,7 @@ public Models.CommitGraphHighlighting GraphHighlighting
132127
if (_repo.UIStates.GraphHighlighting != value)
133128
{
134129
_repo.UIStates.GraphHighlighting = value;
135-
GenerateGraph(_commits);
130+
RefreshGraph();
136131
}
137132
}
138133
}
@@ -254,6 +249,32 @@ public void ToggleShowFlag(Models.HistoryShowFlags flag)
254249
_repo.RefreshCommits();
255250
}
256251

252+
public void SetCommits(List<Models.Commit> commits, Models.CommitGraph graph)
253+
{
254+
if (_cancellationRefreshGraph is { IsCancellationRequested: false })
255+
_cancellationRefreshGraph.Cancel();
256+
257+
ApplyGraph(commits, graph);
258+
259+
if (SetProperty(ref _commits, commits, nameof(Commits)))
260+
PostCommitsChanged();
261+
}
262+
263+
public Models.CommitGraph GenerateGraph(List<Models.Commit> commits)
264+
{
265+
var firstParentOnly = _repo.UIStates.HistoryShowFlags.HasFlag(Models.HistoryShowFlags.FirstParentOnly);
266+
var highlighting = _repo.UIStates.GraphHighlighting;
267+
var extraHeads = new HashSet<string>();
268+
269+
if (highlighting >= Models.CommitGraphHighlighting.SelectedCommitsOnly)
270+
{
271+
foreach (var c in _selectedCommits)
272+
extraHeads.Add(c.SHA);
273+
}
274+
275+
return Models.CommitGraph.Generate(commits, firstParentOnly, highlighting, extraHeads);
276+
}
277+
257278
public Models.BisectState UpdateBisectInfo()
258279
{
259280
var test = Path.Combine(_repo.GitDir, "BISECT_START");
@@ -564,22 +585,37 @@ private void PostSelectedCommitsChanged()
564585
}
565586

566587
if (_repo.UIStates.GraphHighlighting >= Models.CommitGraphHighlighting.SelectedCommitsOnly)
567-
GenerateGraph(_commits);
588+
RefreshGraph();
568589
}
569590

570-
private void GenerateGraph(List<Models.Commit> commits)
591+
private void RefreshGraph()
571592
{
572-
var firstParentOnly = _repo.UIStates.HistoryShowFlags.HasFlag(Models.HistoryShowFlags.FirstParentOnly);
573-
var highlighting = _repo.UIStates.GraphHighlighting;
574-
var extraHeads = new HashSet<string>();
593+
if (_cancellationRefreshGraph is { IsCancellationRequested: false })
594+
_cancellationRefreshGraph.Cancel();
575595

576-
if (highlighting >= Models.CommitGraphHighlighting.SelectedCommitsOnly)
596+
_cancellationRefreshGraph = new CancellationTokenSource();
597+
var token = _cancellationRefreshGraph.Token;
598+
var commits = _commits;
599+
600+
Task.Run(() =>
577601
{
578-
foreach (var c in _selectedCommits)
579-
extraHeads.Add(c.SHA);
580-
}
602+
var graph = GenerateGraph(commits);
603+
Dispatcher.UIThread.Post(() =>
604+
{
605+
if (!token.IsCancellationRequested)
606+
ApplyGraph(commits, graph);
607+
});
608+
}, token);
609+
}
610+
611+
private void ApplyGraph(List<Models.Commit> commits, Models.CommitGraph graph)
612+
{
613+
// There is exactly one dot for each commit (in the same order). Update `IsHighlightedInGraph` here
614+
// so that the property-changed notifications of displayed commits are raised on the UI thread.
615+
for (var i = 0; i < commits.Count; i++)
616+
commits[i].IsHighlightedInGraph = graph.Dots[i].IsHighlighted;
581617

582-
Graph = Models.CommitGraph.Generate(commits, firstParentOnly, highlighting, extraHeads);
618+
Graph = graph;
583619
}
584620

585621
private Repository _repo = null;
@@ -589,6 +625,7 @@ private void GenerateGraph(List<Models.Commit> commits)
589625
private bool _isLoading = true;
590626
private List<Models.Commit> _commits = [];
591627
private Models.CommitGraph _graph = null;
628+
private CancellationTokenSource _cancellationRefreshGraph = null;
592629
private List<Models.Commit> _selectedCommits = [];
593630
private Models.Bisect _bisect = null;
594631
private object _detailContext = Models.Null.Instance;

‎src/ViewModels/Repository.cs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,11 @@ public void RefreshCommits()
12921292
}
12931293
}
12941294

1295+
if (token.IsCancellationRequested)
1296+
return;
1297+
1298+
var graph = _histories.GenerateGraph(commits);
1299+
12951300
Dispatcher.UIThread.Invoke(() =>
12961301
{
12971302
if (token.IsCancellationRequested)
@@ -1300,7 +1305,7 @@ public void RefreshCommits()
13001305
if (_histories != null)
13011306
{
13021307
_histories.IsLoading = false;
1303-
_histories.Commits = commits;
1308+
_histories.SetCommits(commits, graph);
13041309
BisectState = _histories.UpdateBisectInfo();
13051310

13061311
if (!string.IsNullOrEmpty(_navigateToCommitDelayed))

0 commit comments

Comments
 (0)