Skip to content

Commit bd16a0a

Browse files
perf(git): keep status refresh off UI path
Agent edits to already-dirty files left porcelain output unchanged yet still stalled the UI: main-thread parse, modelReset + expandAll, and full numstat re-emission on every refresh, wiping selection along the way. - Parse porcelain v2 on the pool; drop identical bytes and superseded generations before touching the model - Apply entry lists in place via path-ordered edit scripts so selection, expansion, and numstat survive small diffs - Wake content consumers on every refresh, not only when the status set itself changes
1 parent 42bbba6 commit bd16a0a

20 files changed

Lines changed: 1301 additions & 337 deletions

‎src/CMakeLists.txt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ qt_add_executable(NotepadAI
438438
git/GitStatusModel.h
439439
git/GitStatusParser.cpp
440440
git/GitStatusParser.h
441+
git/GitStatusSync.cpp
442+
git/GitStatusSync.h
441443
git/GitTabWidget.cpp
442444
git/GitTabWidget.h
443445
git/GitTypes.cpp

‎src/docks/FolderAsWorkspaceDock.cpp‎

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include <QFileInfo>
5252
#include <QFont>
5353
#include <QFrame>
54+
#include <QFutureWatcher>
5455
#include <QGuiApplication>
5556
#include <QHBoxLayout>
5657
#include <QHelpEvent>
@@ -69,6 +70,7 @@
6970
#include <QTimer>
7071
#include <QToolTip>
7172
#include <QVBoxLayout>
73+
#include <QtConcurrent/QtConcurrentRun>
7274

7375
#include <utility>
7476

@@ -1170,8 +1172,8 @@ void FolderAsWorkspaceDock::ensureGitTab()
11701172
// for the controller pointer to be non-null. Using a queued one-shot
11711173
// captures the controller post-init.
11721174
if (GitController *c = gitTab->controller()) {
1173-
connect(c, &GitController::statusUpdated,
1174-
this, &FolderAsWorkspaceDock::onStatusUpdated);
1175+
connect(c, &GitController::statusRefreshed,
1176+
this, &FolderAsWorkspaceDock::onStatusRefreshed);
11751177
}
11761178

11771179
gitTab->initializeIfNeeded();
@@ -1181,25 +1183,28 @@ void FolderAsWorkspaceDock::ensureGitTab()
11811183
if (GitController *c = gitTab->controller()) {
11821184
// Guard against double-connect (Qt deduplicates the same method ptr +
11831185
// sender + receiver tuple by default when UniqueConnection is set).
1184-
connect(c, &GitController::statusUpdated,
1185-
this, &FolderAsWorkspaceDock::onStatusUpdated,
1186+
connect(c, &GitController::statusRefreshed,
1187+
this, &FolderAsWorkspaceDock::onStatusRefreshed,
11861188
Qt::UniqueConnection);
11871189
}
11881190

11891191
// Drain any status the controller has cached at this point (rare, but
11901192
// covers the case where the new workspace inherits an in-flight refresh
11911193
// from a quick re-open of the same path).
1192-
onStatusUpdated();
1194+
onStatusRefreshed();
11931195
}
11941196

1195-
void FolderAsWorkspaceDock::onStatusUpdated()
1197+
void FolderAsWorkspaceDock::onStatusRefreshed()
11961198
{
1197-
PROFILE_SCOPE("FolderAsWorkspaceDock::onStatusUpdated");
1199+
PROFILE_SCOPE("FolderAsWorkspaceDock::onStatusRefreshed");
11981200

11991201
if (!gitTab || !gitTab->controller() || !gitTab->controller()->statusModel()) {
12001202
// Nothing to read yet. If we previously had entries, clear them so
12011203
// stale decorations disappear (e.g. controller errored out).
1204+
m_decoratedStatusSerial = std::uint64_t(-1);
1205+
m_submodulePaths.clear();
12021206
if (!m_pathStatus.isEmpty()) {
1207+
++m_pathStatusGeneration; // a rebuild in flight must not resurrect them
12031208
const QSet<QString> stale = m_pathStatus.allIndexedPaths();
12041209
m_pathStatus.clear();
12051210
if (model) model->notifyPathsChanged(stale);
@@ -1208,24 +1213,35 @@ void FolderAsWorkspaceDock::onStatusUpdated()
12081213
}
12091214

12101215
const GitController *c = gitTab->controller();
1211-
const GitStatusEntries parentEntries = c->statusModel()->allEntries();
1216+
const GitStatusModel *status = c->statusModel();
12121217
const QString repoTop = c->currentRepo();
1218+
const bool parentChanged = status->changeSerial() != m_decoratedStatusSerial;
1219+
// Same parent status and no submodules: nothing can have changed.
1220+
if (!parentChanged && m_submodulePaths.isEmpty()) return;
1221+
1222+
const GitStatusEntries parentEntries = status->allEntries();
1223+
if (parentChanged) {
1224+
m_decoratedStatusSerial = status->changeSerial();
1225+
// Stage 1: paint with parent entries only. The submodule folder itself
1226+
// is coloured here (its entry has isSubmodule=true → PathStatusIndex
1227+
// registers it as a folder), but files inside the submodule remain
1228+
// undecorated until the async sub-status returns.
1229+
applyMergedEntries(parentEntries, repoTop);
1230+
m_submodulePaths.clear();
1231+
for (const GitStatusEntry &e : parentEntries) {
1232+
if (e.isSubmodule && !e.relPath.isEmpty() && !m_submodulePaths.contains(e.relPath))
1233+
m_submodulePaths.append(e.relPath);
1234+
}
1235+
}
12131236

1214-
// Stage 1: paint immediately with parent entries only. The submodule
1215-
// folder itself is coloured here (its entry has isSubmodule=true →
1216-
// PathStatusIndex registers it as a folder), but files inside the
1217-
// submodule remain undecorated until the async sub-status returns.
1218-
applyMergedEntries(parentEntries, repoTop);
1219-
1220-
// Stage 2: collect submodule paths from the parent's entries and kick off
1221-
// a fan-out fetch. The fetcher cancels any prior in-flight round, so
1222-
// back-to-back refreshes coalesce safely.
1237+
// Stage 2: fan out a status fetch per submodule. The fetcher cancels any
1238+
// prior in-flight round, so back-to-back refreshes coalesce safely.
12231239
QVector<SubmoduleStatusFetcher::Submodule> submodules;
1224-
for (const GitStatusEntry &e : parentEntries) {
1225-
if (!e.isSubmodule || e.relPath.isEmpty()) continue;
1240+
submodules.reserve(m_submodulePaths.size());
1241+
for (const QString &rel : std::as_const(m_submodulePaths)) {
12261242
SubmoduleStatusFetcher::Submodule s;
1227-
s.absPath = QDir::cleanPath(repoTop + QLatin1Char('/') + e.relPath);
1228-
s.relFromRoot = e.relPath;
1243+
s.absPath = QDir::cleanPath(repoTop + QLatin1Char('/') + rel);
1244+
s.relFromRoot = rel;
12291245
submodules.append(s);
12301246
}
12311247

@@ -1259,19 +1275,38 @@ void FolderAsWorkspaceDock::onSubmoduleEntriesReady(const GitStatusEntries &entr
12591275
m_pendingRepoTop.clear();
12601276
}
12611277

1262-
void FolderAsWorkspaceDock::applyMergedEntries(const GitStatusEntries &merged,
1278+
void FolderAsWorkspaceDock::applyMergedEntries(GitStatusEntries merged,
12631279
const QString &repoTop)
12641280
{
1265-
PathStatusIndex next;
1266-
next.rebuild(merged, repoTop);
1267-
1268-
const QSet<QString> delta = next.deltaPaths(m_pathStatus);
1269-
m_pathStatus = std::move(next);
1270-
if (model) {
1271-
model->notifyPathsChanged(delta);
1272-
} else if (auto *rm = qobject_cast<remote::RemoteFileSystemModel *>(fsModel ? fsModel->asModel() : nullptr)) {
1273-
rm->notifyPathsChanged(delta);
1274-
}
1281+
struct Rebuilt {
1282+
PathStatusIndex index;
1283+
QSet<QString> delta;
1284+
};
1285+
// The delta is taken against the index as it is now. A newer launch
1286+
// supersedes this one, so whichever result applies was computed against
1287+
// the m_pathStatus it replaces (synchronous changes bump the generation too).
1288+
const std::uint64_t gen = ++m_pathStatusGeneration;
1289+
auto *watcher = new QFutureWatcher<Rebuilt>(this);
1290+
connect(watcher, &QFutureWatcher<Rebuilt>::finished, this, [this, watcher, gen]() {
1291+
watcher->deleteLater();
1292+
if (gen != m_pathStatusGeneration) return;
1293+
Rebuilt r = watcher->future().takeResult();
1294+
m_pathStatus = std::move(r.index);
1295+
if (model) {
1296+
model->notifyPathsChanged(r.delta);
1297+
} else if (auto *rm = qobject_cast<remote::RemoteFileSystemModel *>(fsModel ? fsModel->asModel() : nullptr)) {
1298+
rm->notifyPathsChanged(r.delta);
1299+
}
1300+
});
1301+
// PathStatusIndex is a pair of implicitly shared hashes: the worker reads
1302+
// its own snapshot while the UI keeps painting from m_pathStatus.
1303+
watcher->setFuture(QtConcurrent::run(
1304+
[merged = std::move(merged), repoTop, previous = m_pathStatus]() {
1305+
Rebuilt r;
1306+
r.index.rebuild(merged, repoTop);
1307+
r.delta = r.index.deltaPaths(previous);
1308+
return r;
1309+
}));
12751310
}
12761311

12771312
SubmoduleStatusFetcher *FolderAsWorkspaceDock::ensureSubmoduleFetcher()

‎src/docks/FolderAsWorkspaceDock.h‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "GitStatusEntry.h"
3232
#include "PathStatusIndex.h"
3333

34+
#include <cstdint>
35+
3436
namespace Ui {
3537
class FolderAsWorkspaceDock;
3638
}
@@ -224,7 +226,10 @@ private slots:
224226
void onDirectoryLoaded(const QString &loadedPath);
225227
void onTreeExpanded(const QModelIndex &index);
226228
void onTreeCollapsed(const QModelIndex &index);
227-
void onStatusUpdated();
229+
// Every git refresh: rebuilds the parent-repo decorations only when the
230+
// status model changed, and re-reads submodule status either way (files
231+
// inside a dirty submodule change without changing the parent status).
232+
void onStatusRefreshed();
228233
void onSubmoduleEntriesReady(const GitStatusEntries &entries);
229234
// Relay from GitTabWidget. If a git-diff fetch is still in flight, the
230235
// editor open is queued so it fires AFTER the diff tab activation —
@@ -280,6 +285,14 @@ private slots:
280285
// dock's lifetime. Pointer handed to `model` via setStatusIndex; cleared
281286
// in destructor before the index dies.
282287
PathStatusIndex m_pathStatus;
288+
// Rebuilds of m_pathStatus run on the thread pool; only the newest one
289+
// applies. Bumped by every rebuild launch and every synchronous change.
290+
std::uint64_t m_pathStatusGeneration = 0;
291+
// GitStatusModel::changeSerial() the parent decorations were built from.
292+
std::uint64_t m_decoratedStatusSerial = std::uint64_t(-1);
293+
// Submodule paths (repo-relative) of the last parent status; their status
294+
// is re-fetched on every refresh.
295+
QStringList m_submodulePaths;
283296
// Async fetcher for per-file `git status` inside each detected submodule.
284297
// Lazily created on first need; nullptr means no submodule status has
285298
// been requested for this dock yet.
@@ -371,8 +384,10 @@ private slots:
371384
SubmoduleStatusFetcher *ensureSubmoduleFetcher();
372385
// Apply a fully merged GitStatusEntries vector (parent + sub-status if
373386
// available) to m_pathStatus, computing the delta against the previous
374-
// snapshot and asking the model to repaint only the affected rows.
375-
void applyMergedEntries(const GitStatusEntries &merged, const QString &repoTop);
387+
// snapshot and asking the model to repaint only the affected rows. The
388+
// rebuild and delta run on the thread pool; the swap and repaint happen
389+
// back on the UI thread.
390+
void applyMergedEntries(GitStatusEntries merged, const QString &repoTop);
376391

377392
// Wire the new FolderAsWorkspaceFsModel to the app's theme signal +
378393
// ApplicationSettings/FileTreeGitColors setting + this dock's

‎src/git/ChangesPanel.cpp‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ void ChangesPanel::buildUi()
179179

180180
void ChangesPanel::setStatusModel(GitStatusModel *model)
181181
{
182+
if (m_statusModel) disconnect(m_statusModel, nullptr, this, nullptr);
182183
m_statusModel = model;
183184
m_tree->setModel(model);
184185
// QTreeView::setModel rebuilds the selection model, so we have to (re)wire
@@ -187,6 +188,19 @@ void ChangesPanel::setStatusModel(GitStatusModel *model)
187188
connect(sel, &QItemSelectionModel::selectionChanged,
188189
this, &ChangesPanel::onSelectionChanged, Qt::UniqueConnection);
189190
}
191+
if (model) {
192+
// Sections open when they appear. Refreshes update rows in place, so
193+
// a section the user collapsed stays collapsed and nothing re-lays
194+
// out the whole tree per refresh.
195+
connect(model, &QAbstractItemModel::rowsInserted, this,
196+
[this](const QModelIndex &parent, int first, int last) {
197+
if (parent.isValid() || !m_statusModel) return;
198+
for (int r = first; r <= last; ++r)
199+
m_tree->expand(m_statusModel->index(r, 0));
200+
});
201+
connect(model, &QAbstractItemModel::modelReset, m_tree, &QTreeView::expandAll);
202+
m_tree->expandAll();
203+
}
190204
refreshActionEnabled();
191205
}
192206

@@ -212,14 +226,14 @@ void ChangesPanel::refreshActionEnabled()
212226
// Stage / Unstage are selection-driven: enabled iff the current selection
213227
// resolves to at least one path on that side. Prevents the dead-click case
214228
// where the button is enabled but a click does nothing.
229+
// Range-based: selecting thousands of rows must not build path lists on
230+
// every selectionChanged.
215231
bool hasUnstagedSel = false;
216232
bool hasStagedSel = false;
217233
if (m_statusModel && m_tree->selectionModel()) {
218-
const QModelIndexList idxs = m_tree->selectionModel()->selectedIndexes();
219-
if (!idxs.isEmpty()) {
220-
hasUnstagedSel = !m_statusModel->unstagedSelectionPaths(idxs).isEmpty();
221-
hasStagedSel = !m_statusModel->stagedSelectionPaths(idxs).isEmpty();
222-
}
234+
const QItemSelection sel = m_tree->selectionModel()->selection();
235+
hasUnstagedSel = m_statusModel->selectionHasUnstaged(sel);
236+
hasStagedSel = m_statusModel->selectionHasStaged(sel);
223237
}
224238

225239
m_stageBtn->setEnabled(m_hasRepo && hasUnstagedSel);
@@ -233,11 +247,6 @@ void ChangesPanel::refreshActionEnabled()
233247
if (m_composer) m_composer->setSubmitEnabled(canCommit);
234248
}
235249

236-
void ChangesPanel::expandTree()
237-
{
238-
m_tree->expandAll();
239-
}
240-
241250
void ChangesPanel::onStageClicked()
242251
{
243252
if (!m_statusModel) return;

‎src/git/ChangesPanel.h‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ class ChangesPanel : public QWidget
6666
void updateActionsEnabled(bool hasRepo, bool hasConflicts,
6767
bool anyStaged, bool anyEntries);
6868

69-
// Expand all top-level sections — called after the status model is
70-
// refreshed so user immediately sees per-file rows.
71-
void expandTree();
72-
7369
// Persist / restore the splitter position between the tree and composer.
7470
void saveSplitterState(const QString &settingsKey);
7571
void restoreSplitterState(const QString &settingsKey);

0 commit comments

Comments
 (0)