Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize GetVisualTreeElementsWindowsInternal #19984

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/Core/src/Core/Extensions/VisualTreeElementExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Maui.Graphics;
Expand Down Expand Up @@ -145,14 +145,27 @@ static List<IVisualTreeElement> GetVisualTreeElementsWindowsInternal(IVisualTree

if (uiElement != null)
{
var uniqueElements = findChildren(uiElement).Distinct();
var viewTree = visualElement.GetVisualTreeDescendants().Where(n => n is IView view && view.Handler is not null).Select(n => new Tuple<IView, object?>((IView)n, ((IView)n).ToPlatform()));
var testList = viewTree.Where(n => uniqueElements.Contains(n.Item2)).Select(n => n.Item1);
if (testList != null && testList.Any())
visualElements.AddRange(testList.Select(n => (IVisualTreeElement)n));
var uniqueElements = findChildren(uiElement).ToHashSet();

var descendants = visualElement.GetVisualTreeDescendants();

// Add in reverse order
for (int i = descendants.Count - 1; i >= 0; i--)
{
var descendant = descendants[i];

if (descendant is not IView view || view.Handler is null)
{
continue;
}

if (uniqueElements.Contains(view.ToPlatform()))
{
visualElements.Add(descendant);
}
}
}

visualElements.Reverse();
return visualElements;
}
#endif
Expand Down
49 changes: 49 additions & 0 deletions src/Core/tests/Benchmarks/Benchmarks/VisualTreeBenchmarker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using BenchmarkDotNet.Attributes;
using Microsoft.Maui.Controls;

namespace Microsoft.Maui.Benchmarks
{
[MemoryDiagnoser]
public class VisualTreeBenchmarker
{
static readonly View[] Views = [
new Border(), new BoxView(), new CarouselView(), new Grid(), new Entry(), new Picker(), new CollectionView(),
new CheckBox(), new DatePicker(), new Stepper(), new Slider(), new ActivityIndicator(), new Frame(),
new ContentView(), new ProgressBar(), new SearchBar(), new Switch(), new TimePicker(), new WebView(), new Button(),
];

private const int Iterations = 100;

[Benchmark]
public void GetVisualTreeElements()
{
var layout = new VerticalStackLayout();

for (int i = 0; i < Iterations; i++)
{
var childLayout = new VerticalStackLayout();

foreach (var view in Views)
{
childLayout.Add(view);

var grandchildLayout = new VerticalStackLayout();

foreach (var view2 in Views)
{
grandchildLayout.Add(view);
grandchildLayout.GetVisualTreeElements(grandchildLayout.Frame);
}

layout.Add(grandchildLayout);

childLayout.GetVisualTreeElements(childLayout.Frame);
}

layout.Add(childLayout);

layout.GetVisualTreeElements(layout.Frame);
}
}
}
}
Loading