Skip to content

Commit

Permalink
perf: Adjust Grid, StoryBoard and VisualStatGroup enumerations
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Sep 13, 2021
1 parent e86c5d3 commit 5b112e9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 23 deletions.
23 changes: 15 additions & 8 deletions src/Uno.UI/UI/Xaml/Controls/Grid/Grid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,12 @@ void ValidateDefinitions(
bool treatStarAsAuto)
{
//for (auto & cdo : definitions)
foreach(DefinitionBase def in definitions.GetItems())
var itemsEnumerator = definitions.GetItems().GetEnumerator();

while(itemsEnumerator.MoveNext())
{
//var def = (DefinitionBase)(cdo);
var def = itemsEnumerator.Current;

bool useLayoutRounding = GetUseLayoutRounding();
var userSize = double.PositiveInfinity;
var userMinSize = useLayoutRounding
Expand Down Expand Up @@ -1066,10 +1069,12 @@ private XSIZEF InnerMeasureOverride(XSIZEF availableSize)
var children = (GetChildren());
if (children is { })
{
//for (auto & cdo : (children))
foreach (var currentChild in children)
// This block is a manual enumeration to avoid the foreach pattern
// See https://github.com/dotnet/runtime/issues/56309 for details
var childrenEnumerator = children.GetEnumerator();
while (childrenEnumerator.MoveNext())
{
//var currentChild = (UIElement)(cdo);
var currentChild = childrenEnumerator.Current;
ASSERT(currentChild is { });

//currentChild.Measure(innerAvailableSize);
Expand Down Expand Up @@ -1357,10 +1362,12 @@ private XSIZEF InnerArrangeOverride(XSIZEF finalSize)
var children = GetChildren();
if (children is { })
{
//for (auto & cdo : (children))
foreach (var cdo in children)
// This block is a manual enumeration to avoid the foreach pattern
// See https://github.com/dotnet/runtime/issues/56309 for details
var childrenEnumerator = children.GetEnumerator();
while (childrenEnumerator.MoveNext())
{
var currentChild = (UIElement)(cdo);
var currentChild = childrenEnumerator.Current;
ASSERT(currentChild is { });

//currentChild.EnsureLayoutStorage();
Expand Down
40 changes: 29 additions & 11 deletions src/Uno.UI/UI/Xaml/Media/Animation/Storyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ private void Play()
{
if (Children != null && Children.Count > 0)
{
foreach (ITimeline child in Children)
for (int i = 0; i < Children.Count; i++)
{
ITimeline child = Children[i];

DisposeChildRegistrations(child);

_runningChildren++;
Expand Down Expand Up @@ -167,8 +169,10 @@ public void Stop()

if (Children != null)
{
foreach (ITimeline child in Children)
for (int i = 0; i < Children.Count; i++)
{
ITimeline child = Children[i];

child.Stop();
DisposeChildRegistrations(child);
}
Expand All @@ -192,8 +196,10 @@ public void Resume()
{
State = TimelineState.Active;

foreach (ITimeline child in Children)
for (int i = 0; i < Children.Count; i++)
{
ITimeline child = Children[i];

child.Resume();
}
}
Expand All @@ -215,8 +221,10 @@ public void Pause()

if (Children != null)
{
foreach (ITimeline child in Children)
for (int i = 0; i < Children.Count; i++)
{
ITimeline child = Children[i];

child.Pause();
}
}
Expand All @@ -226,8 +234,10 @@ public void Seek(TimeSpan offset)
{
if (Children != null)
{
foreach (ITimeline child in Children)
for (int i = 0; i < Children.Count; i++)
{
ITimeline child = Children[i];

child.Seek(offset);
}
}
Expand All @@ -237,8 +247,10 @@ public void SeekAlignedToLastTick(TimeSpan offset)
{
if (Children != null)
{
foreach (ITimeline child in Children)
for (int i = 0; i < Children.Count; i++)
{
ITimeline child = Children[i];

child.SeekAlignedToLastTick(offset);
}
}
Expand All @@ -247,8 +259,10 @@ public void SkipToFill()
{
if (Children != null)
{
foreach (ITimeline child in Children)
for (int i = 0; i < Children.Count; i++)
{
ITimeline child = Children[i];

child.SkipToFill();
}
}
Expand All @@ -261,8 +275,10 @@ internal void Deactivate()

if (Children != null)
{
foreach (ITimeline child in Children)
for (int i = 0; i < Children.Count; i++)
{
ITimeline child = Children[i];

child.Deactivate();
DisposeChildRegistrations(child);
}
Expand All @@ -275,8 +291,10 @@ internal void TurnOverAnimationsTo(Storyboard storyboard)
{
var affectedProperties = storyboard.Children.TargetedProperties;

foreach (var child in Children)
for (int i = 0; i < Children.Count; i++)
{
var child = Children[i];

var id = child.GetTimelineTargetFullName();

if (affectedProperties.Contains(id))
Expand Down Expand Up @@ -361,9 +379,9 @@ protected override void Dispose(bool disposing)

if (Children != null)
{
foreach (var child in Children)
for (int i = 0; i < Children.Count; i++)
{
child.Dispose();
Children[i].Dispose();
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/Uno.UI/UI/Xaml/UIElementCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,13 @@ public void Clear()
{
var items = ClearCore();

foreach (var item in items)
// 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();
while (itemsEnumerator.MoveNext())
{
var item = itemsEnumerator.Current;

if (item is IDependencyObjectStoreProvider provider)
{
item.SetParent(null);
Expand Down
15 changes: 12 additions & 3 deletions src/Uno.UI/UI/Xaml/VisualStateGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,13 @@ internal void GoToState(
// the value is applied only at the end of the transition
if (current.setters is { } currentSetters)
{
foreach (var setter in currentSetters.OfType<Setter>())
// This block is a manual enumeration to avoid the foreach pattern
// See https://github.com/dotnet/runtime/issues/56309 for details
var settersEnumerator = currentSetters.OfType<Setter>().GetEnumerator();
while(settersEnumerator.MoveNext())
{
var setter = settersEnumerator.Current;

if (element != null && (target.setters?.OfType<Setter>().Any(o => o.HasSameTarget(setter, DependencyPropertyValuePrecedences.Animations, element)) ?? false))
{
// We clear the value of the current setter only if there isn't any setter in the target state
Expand Down Expand Up @@ -333,9 +338,13 @@ void ApplyTargetStateSetters()
// We need to invoke them using the right resource context.
ResourceResolver.PushNewScope(_xamlScope);

foreach (var setter in target.setters.OfType<Setter>())
// 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();

while(settersEnumerator.MoveNext())
{
setter.ApplyValue(DependencyPropertyValuePrecedences.Animations, element);
settersEnumerator.Current.ApplyValue(DependencyPropertyValuePrecedences.Animations, element);
}
}
#if !HAS_EXPENSIVE_TRYFINALLY
Expand Down

0 comments on commit 5b112e9

Please sign in to comment.