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

[WinUI] Perf when creating non-trivial grids (and other visual controls) #21818

Closed

Conversation

MartyIX
Copy link
Contributor

@MartyIX MartyIX commented Apr 13, 2024

Description of Change

Notes:

Master

851 ms

image

PR:

676 ms

image

-> 20% faster.

Issues Fixed

Contributes to #21787

Footnotes

  1. I hope he doesn't mind me copying it here.

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Apr 13, 2024
@MartyIX
Copy link
Contributor Author

MartyIX commented Apr 13, 2024

@PureWeen What do you think please?

@bcaceiro
Copy link

This looks interesting! performance should be on top of the list, and changes like this, as well as to other platforms should be really a priority. Since it is in the core, would love to see this going forward. Not developing anything for Windows, but super interested for Android and iOS

@MartyIX
Copy link
Contributor Author

MartyIX commented Apr 14, 2024

  • Clicking "Batch Generate Grid" multiple times lead to ever increasing time to finish the operation. I'm not sure why.

@AdamEssenmacher Can the slowdown be caused by #21809 please?

@AdamEssenmacher
Copy link

AdamEssenmacher commented Apr 14, 2024

@MartyIX I don't think so. Your Labels are getting GC'ed.

It does seem like there's a memory leak in this sample though, so the slowdown could be related to that in some way. It's just not the propagating leak I describe in #21809

I'll further observe that the major leak appearing in this sample goes away and the time to run each batch becomes very consistent if you modify the batch generation method like:

	private async void BatchGenerate_Clicked(object sender, EventArgs e)
	{
		Stopwatch sw = Stopwatch.StartNew();		

		int batchSize = int.Parse(BatchSize.Text);

		for (int i = 0; i < batchSize; i++)
		{
			contentGrid.Clear();

			for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
			{
				for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
				{
					Label label = new Label() { Text = $"[{columnIndex}x{rowIndex}]" };
					contentGrid.Add(label, column: columnIndex, row: rowIndex);
				}
			}

			await Task.Delay(500);
		}

		sw.Stop();
		info.Text = $"Grid was created {batchSize} times and it took {sw.ElapsedMilliseconds} ms in total. Avg run took {Math.Round(sw.ElapsedMilliseconds / (double)batchSize, 2)} ms";
	}

So, I suspect this batch test has something else entirely going on, most likely related to UI elements being added to the visual tree that never get a chance to be fully flushed out to the screen.

@jsuarezruiz jsuarezruiz added platform/windows 🪟 legacy-area-perf Startup / Runtime performance labels Apr 15, 2024
Comment on lines +14 to +23
// TranslationX does the work that is necessary to make other properties work.
nameof(IView.TranslationY),
nameof(IView.Scale),
nameof(IView.ScaleX),
nameof(IView.ScaleY),
nameof(IView.Rotation),
nameof(IView.RotationX),
nameof(IView.RotationY),
nameof(IView.AnchorX),
nameof(IView.AnchorY),
Copy link
Member

Choose a reason for hiding this comment

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

Overall, this seems like it could be a temporary solution if it doesn't change any behavior.

Does this make ordering matter? What if you have code such as:

view.TranslateX = 10;
view.TranslateY = 10;
// set the other properties *after* TranslateX

For this related change for Android:

I believe it changed some behavior with handlers, so we put it in the next .NET release. So this change might be more suitable for .NET 9, so it could get more testing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Overall, this seems like it could be a temporary solution if it doesn't change any behavior.

Mostly I was hoping that this PR would nudge the MAUI team to recognize that there is a lot of performance on the table and even with medium effort, it seems like one can make it substantially faster. I don't want really to put too much effort in this because this is not trivial for me and there is too much space for "it's nice but we decided we'll do X instead". This simply requires some coordination from the code owners. I'm not even the best to implement something like this.

For the record, I believe that your idea with default values it much much better because it would save (especially on Windows) a ton of time. But again, how can I evaluate the idea? Maybe it has been on the table before and it was rejected because who know what would it lead to.

So all in all this PR is just a demonstration that "it looks like that with a rather simple change, it can be about 20% faster". Unfortunately, for me it's still not sufficient because I need something like up to 100ms to draw a complex grid ... :-(.

Also it's not like I'm lazy. Today, for example, I spent whole day fixing a bug that spans somewhere between MAUI and WinUI and I'm on the verge of giving up ...

Copy link
Member

Choose a reason for hiding this comment

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

I need something like up to 100ms to draw a complex grid

Have you thought about using SkiaSharp for this one piece of UI? You likely don't need all the rows & columns to be actual MAUI (& WinUI) Labels?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I need something like up to 100ms to draw a complex grid

Have you thought about using SkiaSharp for this one piece of UI? You likely don't need all the rows & columns to be actual MAUI (& WinUI) Labels?

I haven't mostly because it feels hard to create a data grid in 2D graphics. I don't know much about SkiaSharp. Is it somehow easy to create such data grid? I mean it sounds like a lot of low-level computing on my side to make it work.

Moreover, values in "labels" would not be easy to copy & paste, or would it?

Maybe a custom layout would be a way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jonathanpeppers I have come up with a different approach in #23987 (the main idea is: https://github.com/dotnet/maui/pull/23987/files#diff-fe5090f560d95c536e43dfc5f39442add6ad2d8eb555d675bdb4a0f82a30793dR35). Could you please take a look?

@jsuarezruiz
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@Eilon Eilon added the t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf) label May 10, 2024
@PureWeen PureWeen added area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter layout-grid labels May 15, 2024
@MartyIX MartyIX closed this Aug 24, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Sep 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter community ✨ Community Contribution layout-grid legacy-area-perf Startup / Runtime performance platform/windows 🪟 t/perf The issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants