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.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
namespace CalculatorApp
{
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)
{
Add(item);

View file

@ -949,6 +949,8 @@ namespace CalculatorApp.ViewModel
}
void BuildUnitList(CalculatorList<UCM.Unit> modelUnitList)
{
using (m_Units.BatchUpdate())
{
m_Units.Clear();
foreach (UCM.Unit modelUnit in modelUnitList)
@ -964,6 +966,7 @@ namespace CalculatorApp.ViewModel
m_Units.Append(EMPTY_UNIT);
}
}
}
Unit FindUnitInList(UCM.Unit target)
{
@ -1283,6 +1286,8 @@ namespace CalculatorApp.ViewModel
}
void InitializeView()
{
using (m_Categories.BatchUpdate())
{
CalculatorList<UCM.Category> categories = m_model.GetCategories();
for (uint i = 0; i < categories.Size(); i++)
@ -1290,6 +1295,7 @@ namespace CalculatorApp.ViewModel
Category category = new Category(categories[i]);
m_Categories.Append(category);
}
}
RestoreUserPreferences();
CurrentCategory = new Category(m_model.GetCurrentCategory());
@ -1585,6 +1591,7 @@ namespace CalculatorApp.ViewModel
void RefreshSupplementaryResults()
{
lock (m_cacheMutex)
using(m_SupplementaryResults.BatchUpdate())
{
m_SupplementaryResults.Clear();
@ -1607,7 +1614,6 @@ namespace CalculatorApp.ViewModel
{
m_SupplementaryResults.Append(whimsicals[0]);
}
}
RaisePropertyChanged(SupplementaryResultsPropertyName);