Skip to content

Commit

Permalink
Merge pull request #17403 from unoplatform/dev/youssef/perf-allocations
Browse files Browse the repository at this point in the history
perf: Avoid OfTypeIterator allocations
  • Loading branch information
Youssef1313 committed Jul 8, 2024
2 parents 1c3aece + e2d3afc commit 5cadd6a
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Uno.UI/UI/Xaml/VisualStateGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,14 @@ void ApplyTargetStateSetters()

// This block is a manual enumeration to avoid the foreach pattern
// See https://github.com/dotnet/runtime/issues/56309 for details
var settersEnumerator = target.setters.OfType<Setter>().GetEnumerator();
var settersEnumerator = target.setters.GetEnumerator();

while (settersEnumerator.MoveNext())
{
settersEnumerator.Current.ApplyValue(element);
if (settersEnumerator.Current is Setter setter)
{
setter.ApplyValue(element);
}
}
}
finally
Expand Down Expand Up @@ -407,9 +410,12 @@ private static void ClearPropertiesNotSetInNextState(

if (nextSetters is { })
{
foreach (var setter in nextSetters.OfType<Setter>())
foreach (var setterBase in nextSetters)
{
propertiesNotToClear[propsLength++] = setter.TryGetOrCreateBindingPath(element);
if (setterBase is Setter setter)
{
propertiesNotToClear[propsLength++] = setter.TryGetOrCreateBindingPath(element);
}
}
}

Expand All @@ -423,10 +429,11 @@ private static void ClearPropertiesNotSetInNextState(

if (prevSetters is { })
{
foreach (var setter in prevSetters.OfType<Setter>())
foreach (var setterBase in prevSetters)
{
// Setters with a null path are always cleared.
if (setter.TryGetOrCreateBindingPath(element) is not { } prevPath || !propertiesNotToClear.Any(path => prevPath.PrefixOfOrEqualTo(path)))
if (setterBase is Setter setter &&
(setter.TryGetOrCreateBindingPath(element) is not { } prevPath || !propertiesNotToClear.Any(path => prevPath.PrefixOfOrEqualTo(path))))
{
setter.ClearValue();
}
Expand Down

0 comments on commit 5cadd6a

Please sign in to comment.