From 1b34e440ab807d92f7bb1dcd35f823b38e7e56bc Mon Sep 17 00:00:00 2001 From: haven1433 Date: Mon, 31 Jul 2023 20:50:46 -0500 Subject: [PATCH] performance improvement: cache data change Only notify property HasDataChange if the value actually changed. Use a cache to store the old value. Calculating HasDataChange is fast, but updating from HasDataChange notifications can be slow. --- src/HexManiac.Core/ViewModels/ChangeHistory.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/HexManiac.Core/ViewModels/ChangeHistory.cs b/src/HexManiac.Core/ViewModels/ChangeHistory.cs index 6a7458c2..868453db 100644 --- a/src/HexManiac.Core/ViewModels/ChangeHistory.cs +++ b/src/HexManiac.Core/ViewModels/ChangeHistory.cs @@ -136,6 +136,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels { public void TagAsSaved() { ChangeCompleted(); + hasDataChangeCache = false; if (TryUpdate(ref undoStackSizeAtSaveTag, undoStack.Count, nameof(IsSaved))) { NotifyPropertyChanged(nameof(HasDataChange)); } @@ -149,9 +150,14 @@ namespace HavenSoft.HexManiac.Core.ViewModels { return new StubDisposable { Dispose = () => continueCurrentTransaction = previousValue }; } + private bool hasDataChangeCache; private void OnCurrentTokenDataChanged(object sender, EventArgs e) { if (undoStack.Count == 0) undo.RaiseCanExecuteChanged(); - NotifyPropertyChanged(nameof(HasDataChange)); + var hasDataChange = HasDataChange; + if (hasDataChange != hasDataChangeCache) { + hasDataChangeCache = hasDataChange; + NotifyPropertyChanged(nameof(HasDataChange)); + } } private void UndoExecuted() { @@ -191,7 +197,7 @@ namespace HavenSoft.HexManiac.Core.ViewModels { if (previouslyHadDataChanged != HasDataChange) NotifyPropertyChanged(nameof(HasDataChange)); } - private void VerifyRevertNotInProgress([CallerMemberName]string caller = null) { + private void VerifyRevertNotInProgress([CallerMemberName] string caller = null) { if (!revertInProgress) return; throw new InvalidOperationException($"Cannot execute member {caller} while a revert is in progress."); }