Skip to content

Commit

Permalink
Merge pull request unoplatform#15357 from ramezgerges/uielement_inval…
Browse files Browse the repository at this point in the history
…idation_perf

perf: reduce layout invalidations when clearing children when there're none
  • Loading branch information
jeromelaban authored Feb 6, 2024
2 parents 3872ca8 + 55cce74 commit 3c188c0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Uno.UI/UI/Xaml/UIElement.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ internal UIElement ReplaceChild(int index, UIElement child)

internal void ClearChildren()
{
if (_children.Count == 0)
{
return;
}

foreach (var child in _children.ToArray())
{
InnerRemoveChild(child);
Expand Down
7 changes: 7 additions & 0 deletions src/Uno.UI/UI/Xaml/UIElementCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ public void Clear()
// This block is a manual enumeration to avoid the foreach pattern
// See https://github.com/dotnet/runtime/issues/56309 for details
var itemsEnumerator = items.GetEnumerator();
var hasItems = false;
while (itemsEnumerator.MoveNext())
{
hasItems = true;
var item = itemsEnumerator.Current;

if (item is IDependencyObjectStoreProvider provider)
Expand All @@ -85,6 +87,11 @@ public void Clear()
fe.InvalidateMeasure();
}

if (!hasItems)
{
return;
}

OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, items.ToList()));
}

Expand Down

0 comments on commit 3c188c0

Please sign in to comment.