Merge pull request #59 from nventive/dev/dr/PerfCollections

Improve perf by batching collection change updates
This commit is contained in:
David 2019-05-26 10:49:35 -04:00 committed by GitHub
commit 28c67c6abe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 14 deletions

View file

@ -1,11 +1,41 @@
using System; using System;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq; using System.Linq;
namespace CalculatorApp namespace CalculatorApp
{ {
public class CalculatorObservableCollection<TType> : ObservableCollection<TType> public class CalculatorObservableCollection<TType> : ObservableCollection<TType>
{ {
private int _batchUpdateCount;
public IDisposable BatchUpdate()
{
++_batchUpdateCount;
return Uno.Disposables.Disposable.Create(Release);
void Release()
{
if (--_batchUpdateCount <= 0)
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
}
/// <inheritdoc />
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (_batchUpdateCount > 0)
{
return;
}
base.OnCollectionChanged(e);
}
public void Append(TType item) public void Append(TType item)
{ {
Add(item); Add(item);
@ -16,4 +46,4 @@ namespace CalculatorApp
return this[index]; return this[index];
} }
} }
} }

View file

@ -950,18 +950,21 @@ namespace CalculatorApp.ViewModel
void BuildUnitList(CalculatorList<UCM.Unit> modelUnitList) void BuildUnitList(CalculatorList<UCM.Unit> modelUnitList)
{ {
m_Units.Clear(); using (m_Units.BatchUpdate())
foreach (UCM.Unit modelUnit in modelUnitList)
{ {
if (!modelUnit.isWhimsical) m_Units.Clear();
foreach (UCM.Unit modelUnit in modelUnitList)
{ {
m_Units.Append(new Unit(modelUnit)); if (!modelUnit.isWhimsical)
{
m_Units.Append(new Unit(modelUnit));
}
} }
}
if (m_Units.Count == 0) if (m_Units.Count == 0)
{ {
m_Units.Append(EMPTY_UNIT); m_Units.Append(EMPTY_UNIT);
}
} }
} }
@ -1284,11 +1287,14 @@ namespace CalculatorApp.ViewModel
void InitializeView() void InitializeView()
{ {
CalculatorList<UCM.Category> categories = m_model.GetCategories(); using (m_Categories.BatchUpdate())
for (uint i = 0; i < categories.Size(); i++)
{ {
Category category = new Category(categories[i]); CalculatorList<UCM.Category> categories = m_model.GetCategories();
m_Categories.Append(category); for (uint i = 0; i < categories.Size(); i++)
{
Category category = new Category(categories[i]);
m_Categories.Append(category);
}
} }
RestoreUserPreferences(); RestoreUserPreferences();
@ -1585,6 +1591,7 @@ namespace CalculatorApp.ViewModel
void RefreshSupplementaryResults() void RefreshSupplementaryResults()
{ {
lock (m_cacheMutex) lock (m_cacheMutex)
using(m_SupplementaryResults.BatchUpdate())
{ {
m_SupplementaryResults.Clear(); m_SupplementaryResults.Clear();
@ -1607,7 +1614,6 @@ namespace CalculatorApp.ViewModel
{ {
m_SupplementaryResults.Append(whimsicals[0]); m_SupplementaryResults.Append(whimsicals[0]);
} }
} }
RaisePropertyChanged(SupplementaryResultsPropertyName); RaisePropertyChanged(SupplementaryResultsPropertyName);