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

Conversation

jonathanpeppers
Copy link
Member

Context: https://github.com/unoplatform/performance/tree/master/src/dopes/DopeTestMaui

Reviewing dotnet trace output of the "dopes" test sample, 7% of CPU
time is spent in LayoutExtensions.OrderByZIndex():

image

3.42s (16%)  microsoft.maui!Microsoft.Maui.Handlers.LayoutHandler.Add(Microsoft.Maui.IView)
1.52s (7.3%) microsoft.maui!Microsoft.Maui.Handlers.LayoutExtensions.GetLayoutHandlerIndex(Microsoft.Maui.ILayout,Microsoft.Maui.IVie...
1.50s (7.2%) microsoft.maui!Microsoft.Maui.Handlers.LayoutExtensions.OrderByZIndex(Microsoft.Maui.ILayout)

Reviewing the code in OrderByZIndex():

  • Makes a record ViewAndIndex(IView View, int Index)
    for each child
  • Makes a ViewAndIndex[] the size of the number of children
  • Call Array.Sort()
  • Makes a IView[] the size of the number of children
  • Iterating twice over the arrays in the process

Then if you look at the GetLayoutHandlerIndex() method, it does all
the same work to create an IView[], get the index, then throws the
array away.

I removed the above code and used a System.Linq OrderBy() with
faster paths for 0 or 1 children. I also made an internal
EnumerateByZIndex() method for use by foreach loops. This avoids
creating arrays in those cases. The ZIndexTests still pass for me,
which proves out OrderBy()'s stable sort:

https://docs.microsoft.com/dotnet/api/system.linq.enumerable.orderby

After this change, the % time spent in these methods went down:

  1.84s (13%)    microsoft.maui!Microsoft.Maui.Handlers.LayoutHandler.Add(Microsoft.Maui.IView)
352.24ms (2.50%) microsoft.maui!Microsoft.Maui.Handlers.LayoutExtensions.GetLayoutHandlerIndex(Microsoft.Maui.ILayout,Microsoft.Maui.IVie...
181.27ms (1.30%) microsoft.maui!Microsoft.Maui.Handlers.LayoutExtensions.<>c.<EnumerateByZIndex>b__0_0(Microsoft.Maui.IView)
  2.78ms (0.02%) microsoft.maui!Microsoft.Maui.Handlers.LayoutExtensions.EnumerateByZIndex(Microsoft.Maui.ILayout)

This kind of goes against the guidance "avoid System.Linq".
But OrderBy() generally seems to be fine?

Results

A Release build on a Pixel 5 device, I was getting:

Before:
103.04 Dopes/s
After:
230.87 Dopes/s

I suspect that was a lot of IView[]'s before!

image

@jonathanpeppers jonathanpeppers added the legacy-area-perf Startup / Runtime performance label Jun 22, 2022
Comment on lines -10 to -12
class ZIndexComparer : IComparer<ViewAndIndex>
{
public int Compare(ViewAndIndex? x, ViewAndIndex? y)
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.

@jonathanpeppers jonathanpeppers marked this pull request as ready for review June 22, 2022 21:12
Copy link
Contributor

@hartez hartez left a comment

Choose a reason for hiding this comment

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

Wondering if we can just drop the OrderByZIndexMethod.

src/Core/src/Handlers/Layout/LayoutExtensions.cs Outdated Show resolved Hide resolved
return layout.OrderByZIndex().IndexOf(view);
switch (layout.Count)
{
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.

Context: https://github.com/unoplatform/performance/tree/master/src/dopes/DopeTestMaui

Reviewing `dotnet trace` output of the "dopes" test sample, 7% of CPU
time is spent in `LayoutExtensions.OrderByZIndex()`:

    3.42s (16%)  microsoft.maui!Microsoft.Maui.Handlers.LayoutHandler.Add(Microsoft.Maui.IView)
    1.52s (7.3%) microsoft.maui!Microsoft.Maui.Handlers.LayoutExtensions.GetLayoutHandlerIndex(Microsoft.Maui.ILayout,Microsoft.Maui.IVie...
    1.50s (7.2%) microsoft.maui!Microsoft.Maui.Handlers.LayoutExtensions.OrderByZIndex(Microsoft.Maui.ILayout)

Reviewing the code in `OrderByZIndex()`:

* Makes a `record ViewAndIndex(IView View, int Index)`
  for each child
* Makes a `ViewAndIndex[]` the size of the number of children
* Call `Array.Sort()`
* Makes a `IView[]` the size of the number of children
* Iterating twice over the arrays in the process

Then if you look at the `GetLayoutHandlerIndex()` method, it does all
the same work to create an `IView[]`, get the index, then throws the
array away.

I removed the above code and used a System.Linq `OrderBy()` with
faster paths for 0 or 1 children in `GetLayoutHandlerIndex()`. I also
made `OrderByZIndex()` return `IOrderedEnumerable` for use by
`foreach` loops. This avoids creating arrays in those cases. The
`ZIndexTests` still pass for me, which proves out `OrderBy()`'s stable
sort:

https://docs.microsoft.com/dotnet/api/system.linq.enumerable.orderby

After this change, the % time spent in these methods went down:

      1.84s (13%)    microsoft.maui!Microsoft.Maui.Handlers.LayoutHandler.Add(Microsoft.Maui.IView)
    352.24ms (2.50%) microsoft.maui!Microsoft.Maui.Handlers.LayoutExtensions.GetLayoutHandlerIndex(Microsoft.Maui.ILayout,Microsoft.Maui.IVie...
    181.27ms (1.30%) microsoft.maui!Microsoft.Maui.Handlers.LayoutExtensions.<>c.<EnumerateByZIndex>b__0_0(Microsoft.Maui.IView)
      2.78ms (0.02%) microsoft.maui!Microsoft.Maui.Handlers.LayoutExtensions.EnumerateByZIndex(Microsoft.Maui.ILayout)

This kind of goes against the guidance "avoid System.Linq".
But `OrderBy()` generally seems to be fine?

~~ Results ~~

A `Release` build on a Pixel 5 device, I was getting:

    Before:
    103.04 Dopes/s
    After:
    230.87 Dopes/s

I suspect that was a lot of `IView[]`'s before!
{
indexedViews[n] = new ViewAndIndex(layout[n], n);
case 0:
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.

@hartez hartez merged commit 3871851 into dotnet:main Jun 23, 2022
@jonathanpeppers jonathanpeppers deleted the OrderByZIndex branch June 23, 2022 17:14
@github-actions github-actions bot locked and limited conversation to collaborators Dec 20, 2023
@Eilon Eilon added the t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.) label May 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
legacy-area-perf Startup / Runtime performance t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.)
Projects
Status: Done & Blogged
Development

Successfully merging this pull request may close these issues.

None yet

4 participants