question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Performance issue - Subscribing\Unsubscribing to events

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
alexserovcommented, 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.

0reactions
github-actions[bot]commented, Jul 28, 2023

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Unsubscribing from events - performance hit?
Are there performance considerations related to (un)subscribing events? Is there an explanation to why this would cause such a performance hit?
Read more >
Prevent resubscribing to an event
Various scenes in my game subscribe to events conditionally. A problem occurs when I enter the scene again and it subscribes to the...
Read more >
Event subscribing and unsubscribing, and putting that into ...
so lets say I have a Player class that sends/sets the subscription: public event Action Attacking;. and I have an Enemy class that...
Read more >
To unsubscribe, or not to unsubscribe, that is the question.
In this article, I will try to demystify the topic of the Subscription. ... your app runs for longer, you will experience memory...
Read more >
When to unsubscribe from Event?
Another potential pitfall is when a method subscribes to an event. If you do not unsubscribe first, then you could end up with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found