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

Remove disposable allocations from routed event AddHandler in the common case. #3651

Merged
merged 3 commits into from
Mar 11, 2020

Conversation

MarchingCube
Copy link
Collaborator

What does the pull request do?

AddHandler was always returning IDisposable allowing for easy unsubscribe for the user. This PR changes this behavior to not do this by default and adds a separate function with this behavior.

What is the current behavior?

Most of the AddHandler callers don't even use returned value (all event implementations in AvaloniaObject just discard returned IDisposable). There are only a few call sites that use this feature.

Method Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
CreateCalendar 19.29 ms 0.1664 ms 0.1557 ms 625.0000 281.2500 31.2500 3.61 MB

What is the updated/expected behavior with this PR?

Adds a separate AddDisposableHandler that can returns IDisposable that can be used to unsubscribe from the handler.

Perf gains in calendar case are tiny but we can clearly see that we collect less often now as AddHandler is not forced to allocate all the time. But we shouldn't prevent users from not being able have non-allocating AddHandler to support 1% niche use case.

Method Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
CreateCalendar 19.18 ms 0.2813 ms 0.2631 ms 593.7500 250.0000 - 3.6 MB

How was the solution implemented (if it's not obvious)?

Checklist

Breaking changes

Users that are utilizing AddHandler return value should use AddDisposableHandler instead.

@MarchingCube MarchingCube requested a review from grokys March 9, 2020 22:22
Copy link
Member

@grokys grokys left a comment

Choose a reason for hiding this comment

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

👍 WPF's AddHandler doesn't return an IDisposable, so removing it and adding an extension method that does makes sense from an API standpoint.

@grokys grokys merged commit 22e3195 into AvaloniaUI:master Mar 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants