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

Performance issue - Subscribing\Unsubscribing to events #3557

Closed
alexserov opened this issue Nov 5, 2020 · 6 comments
Closed

Performance issue - Subscribing\Unsubscribing to events #3557

alexserov opened this issue Nov 5, 2020 · 6 comments

Comments

@alexserov
Copy link

alexserov commented Nov 5, 2020

Describe the bug
When profiling our test application, we've seen that every event subscription takes a noticeable time.
In our case, this slows down the application startup for about 200ms.

I made a small sample that shows the issue. I've also prepared the WPF test application so you can compare performance.

Steps to reproduce the bug

Steps to reproduce the behavior:

  1. Download from the attachments and open the FrameworkEventsTest solution;
  2. Set the following solution properties:
    • Configuration - "Release"
    • Platform - "x64"
    • Startup Project - "FrameworkEventsTest (Package)"
  3. Run the application
  4. Click on the "Click Me" button
  5. Wait until the result is shown in the text editor (this may take up to 40 seconds)
  6. Repeat steps 4 and 5 for more accurate results

In the table below, you can see the difference between WPF and WinUI performance when subscribing\unsibscribing to the IsEnabledChanged event.
I've also tried the Loaded and the KeyDown event and got the same results.

Case # Description WPF WinUI pre-2 WinUI pre-3
1 Subscribe to 1 button event with 1 method 1000 times 0 0 0
2 Subscribe and Unsubscribe to 1 button event with 1 method 1000 times 0 26 7
3 Subscribe to 1 button event with 1000 methods 1 time 406 269 341
4 Subscribe and Unsubscribe to 1 button event with 1000 methods 1 time 170 23365 5685
5 Subscribe to 1000 buttons event with 1 method 1 time 0 336 217
6 Subscribe and Unsubscribe to 1000 buttons event with 1 method 1 time 0 326 217

Expected behavior
I expect that cases 5 and 6 should be instant (like in WPF) since this is very close to a real scenario: in our app, we have lots of controls with subscriptions to such events as Loaded, Unloaded, IsEnabledChanged, LayoutUpdated.

I'm also surprised at the poor performance of scenario 4; however, I understand that this is synthetic and may not exist in the real application.

Version Info

NuGet package version:
[Microsoft.WinUI 3.0.0-preview2.200713.0]
[Microsoft.WinUI 3.0.0-preview3.201113.0]

Windows app type:

UWP Win32
Yes
Windows 10 version Saw the problem?
Insider Build (xxxxx)
May 2020 Update (19041) Yes
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop Yes
Xbox
Surface Hub
IoT

Additional context

FrameworkEventsTest.zip

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Nov 5, 2020
@chrisglein
Copy link
Member

@alexserov WinUI3 is more similar to UWP XAML than WPF. Are you seeing similar behavior in an app built off of inbox XAML?
@Austin-Lamb Here someone is looking at performance of subscribe/unsubscribe of events of WinUI3. Is this something you or @bartekk8 have looked into?

@chrisglein chrisglein added needs-author-feedback Asked author to supply more information. wpf-vs-winui-mismatch labels Nov 11, 2020
@alexserov
Copy link
Author

alexserov commented Nov 12, 2020

Hello @chrisglein , I've just measured my tests for UWP XAML and WinUI3 (Preview2) for UWP, and here are the results:

Case # Description UWP XAML WinUI for UWP
1 Subscribe to 1 button event with 1 method 1000 times 2 2
2 Subscribe and Unsubscribe to 1 button event with 1 method 1000 times 27 23
3 Subscribe to 1 button event with 1000 methods 1 time 44834 49764
4 Subscribe and Unsubscribe to 1 button event with 1000 methods 1 time 7264 7448
5 Subscribe to 1000 buttons event with 1 method 1 time 5 5
6 Subscribe and Unsubscribe to 1000 buttons event with 1 method 1 time 35 34

The code is the same for every project. You can find it in my attachment to the original post.

As I mentioned earlier, my main interest is in cases 5 and 6 (WinUI for Win32) since we experience the same performance issue in our application.

upd: we have our custom controls in the app; they mostly derive from Microsoft.UI.Xaml.Controls.Control. The ctor looks like follows:

public BaseControl() {
    LayoutUpdated += OnLayoutUpdated;
    Loaded += OnLoaded;
    Unloaded += OnUnloaded;
    IsEnabledChanged += OnIsEnabledChanged;
}

Since we reuse our code from our UWP project, I expect the performance should be the same or even better. Unfortunately, it is not.

@ghost ghost removed the needs-author-feedback Asked author to supply more information. label Nov 12, 2020
@alexserov
Copy link
Author

I've updated my initial post with the measurements for the WinUI3 Preview3. I see the progress; however, the issue still exists.

Case # Description WinUI 3 pre-3
1 Subscribe to 1 button event with 1 method 1000 times 0
2 Subscribe and Unsubscribe to 1 button event with 1 method 1000 times 7
3 Subscribe to 1 button event with 1000 methods 1 time 341
4 Subscribe and Unsubscribe to 1 button event with 1000 methods 1 time 5685
5 Subscribe to 1000 buttons event with 1 method 1 time 217
6 Subscribe and Unsubscribe to 1000 buttons event with 1 method 1 time 217

@chrisglein
Copy link
Member

Thanks for the extra data, we really appreciate it.

For what it's worth this isn't necessarily true:

I expect the performance should be the same or even better

WinUI3 isn't guaranteed to have the same performance profile as UWP XAML. There are places where it may be better, places where it'll be worse, and places where it is the same. But moving the platform out of the OS into separate infrastructure will cause changes. We're working on measuring this and working to make those differences minimal/positive, but it will take time.

@chrisglein chrisglein removed the needs-triage Issue needs to be triaged by the area owners label Jan 27, 2021
@chrisglein
Copy link
Member

One addition to the stack in this case is going to be CsWinRT. There's still analysis of that happening elsewhere, which could uncover potential improvements with events specifically (e.g. this issue)

@github-actions
Copy link

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 5 days.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants