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

[layout] avoid IView[] creation in LayoutExtensions #8250

Merged
merged 1 commit into from
Jun 23, 2022
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
56 changes: 10 additions & 46 deletions src/Core/src/Handlers/Layout/LayoutExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.Maui.Handlers
{
internal static class LayoutExtensions
{
record ViewAndIndex(IView View, int Index);
public static IOrderedEnumerable<IView> OrderByZIndex(this ILayout layout) => layout.OrderBy(v => v.ZIndex);

class ZIndexComparer : IComparer<ViewAndIndex>
{
public int Compare(ViewAndIndex? x, ViewAndIndex? y)
Comment on lines -10 to -12
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that a regular OrderBy() is the same as this logic. Let me know if I'm missing something, thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I only wrote it the hard way because otherwise I'd have to argue with people about the Linq :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found other places where performance matters, that used OrderBy():

https://github.com/dotnet/msbuild/blob/c2ec5c98a76a5496a36025b3f5e790cca6ea3b2f/src/Build/Evaluation/LazyItemEvaluator.cs#L511-L515

This is used when MSBuild evaluates a project, for every type of item group in a build. It would be called when VS loads a project.

{
if (x == null || y == null)
{
return 0;
}

var zIndexCompare = x.View.ZIndex.CompareTo(y.View.ZIndex);

if (zIndexCompare == 0)
{
return x.Index.CompareTo(y.Index);
}

return zIndexCompare;
}
}

static ZIndexComparer s_comparer = new();

public static IView[] OrderByZIndex(this ILayout layout)
public static int GetLayoutHandlerIndex(this ILayout layout, IView view)
{
var count = layout.Count;
var indexedViews = new ViewAndIndex[count];

for (int n = 0; n < count; n++)
switch (layout.Count)
{
indexedViews[n] = new ViewAndIndex(layout[n], n);
case 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I bet this is a huge chunk of the performance gain.

return -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was it returning -1 before ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.

case 1:
return view == layout[0] ? 0 : -1;
default:
return layout.OrderByZIndex().IndexOf(view);
}

Array.Sort(indexedViews, s_comparer);

var ordered = new IView[count];

for (int n = 0; n < count; n++)
{
ordered[n] = indexedViews[n].View;
}

return ordered;
}

public static int GetLayoutHandlerIndex(this ILayout layout, IView view)
{
return layout.OrderByZIndex().IndexOf(view);
}
}
}
9 changes: 5 additions & 4 deletions src/Core/tests/UnitTests/Layouts/ZIndexTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Primitives;
Expand Down Expand Up @@ -209,7 +210,7 @@ public void ItemsOrderByZIndex()
layout.Add(view0);
layout.Add(view1);

var zordered = layout.OrderByZIndex();
var zordered = layout.OrderByZIndex().ToArray();
Assert.Equal(view1, zordered[0]);
Assert.Equal(view0, zordered[1]);
}
Expand All @@ -228,7 +229,7 @@ public void ZIndexUpdatePreservesAddOrderForEqualZIndexes()
layout.Add(view2);
layout.Add(view3);

var zordered = layout.OrderByZIndex();
var zordered = layout.OrderByZIndex().ToArray();
Assert.Equal(view0, zordered[0]);
Assert.Equal(view1, zordered[1]);
Assert.Equal(view2, zordered[2]);
Expand All @@ -237,7 +238,7 @@ public void ZIndexUpdatePreservesAddOrderForEqualZIndexes()
// Fake an update
view3.ZIndex.Returns(5);

zordered = layout.OrderByZIndex();
zordered = layout.OrderByZIndex().ToArray();
Assert.Equal(view0, zordered[0]);
Assert.Equal(view1, zordered[1]);
Assert.Equal(view2, zordered[2]);
Expand Down Expand Up @@ -267,7 +268,7 @@ public void ZIndexUpdatePreservesAddOrderLotsOfEqualZIndexes()

for (int i = 0; i < iterations; i++)
{
var zordered = layout.OrderByZIndex();
var zordered = layout.OrderByZIndex().ToArray();

for (int n = 0; n < zordered.Length; n++)
{
Expand Down