Skip to content

Commit

Permalink
perf: Reduce foreach/linq usage in pointers management
Browse files Browse the repository at this point in the history
Improves the performance of UIElement.Visibility changes
  • Loading branch information
jeromelaban committed May 6, 2021
1 parent 0f64934 commit d5b622a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
16 changes: 16 additions & 0 deletions src/Uno.UI/UI/Xaml/DependencyObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,22 @@ internal static IEnumerable<object> GetParents(this object dependencyObject)
}
}

internal static bool HasParent(this object dependencyObject, DependencyObject searchedParent)
{
var parent = dependencyObject.GetParent();
while (parent != null)
{
if(ReferenceEquals(parent, searchedParent))
{
return true;
}

parent = parent.GetParent();
}

return false;
}

/// <summary>
/// Set the parent of the specified dependency object
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI/UI/Xaml/UIElement.PointerCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static PointerCapture GetOrCreate(Pointer pointer)
public static bool TryGet(Pointer pointer, out PointerCapture capture)
=> _actives.TryGetValue(pointer, out capture);

public static bool Any(out IEnumerable<PointerCapture> cloneOfAllCaptures)
public static bool Any(out List<PointerCapture> cloneOfAllCaptures)
{
if (_actives.Any())
{
Expand Down
12 changes: 9 additions & 3 deletions src/Uno.UI/UI/Xaml/UIElement.Pointers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,17 @@ private void InitializePointers()
&& visibility != Visibility.Visible
&& PointerCapture.Any(out var captures))
{
foreach (var capture in captures)
for (var captureIndex = 0; captureIndex < captures.Count; captureIndex++)
{
foreach (var target in capture.Targets.ToList())
var capture = captures[captureIndex];
var targets = capture.Targets.ToList();
for (var targetIndex = 0; targetIndex < targets.Count; targetIndex++)
{
if (target.Element.GetParents().Contains(sender))
var target = targets[targetIndex];
if (target.Element.HasParent(sender))
{
target.Element.Release(capture, PointerCaptureKind.Any);
}
Expand Down

0 comments on commit d5b622a

Please sign in to comment.