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

[Perf] Avoid the materialization of list of handlers when raising events #16320

Open
dr1rrb opened this issue Apr 16, 2024 · 1 comment
Open
Labels
area/performance 📈 Categorizes an issue or PR as relevant to performance difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/enhancement New feature or request triage/untriaged Indicates an issue requires triaging or verification

Comments

@dr1rrb
Copy link
Member

dr1rrb commented Apr 16, 2024

What would you like to be added

When we raise routed events args, we are creating a new array (not even pooled!) which contains the list of all event handlers for the given event:

foreach (var handler in handlers.ToArray())
. This can be a hot-path and can be really expensive for nothing, we can improve this in multiple ways:

  • Most of the time we will probably have only one handler. In such case no needs enumerate the handlers collection!
  • The clone is made to allow the removal of an handler during the enumeration. This is not the common scenario, we could imagine to have a dedicated collection implementation that will clone the collection only if an element is being removed during enumeration / effectively removes the element only once enumeration is completed / ...

Why is this needed

No response

For which platform

No response

Anything else we need to know?

No response

@dr1rrb dr1rrb added kind/enhancement New feature or request triage/untriaged Indicates an issue requires triaging or verification difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. labels Apr 16, 2024
@dr1rrb dr1rrb changed the title Avoid the materialization of list of handlers when raising events [Perf] Avoid the materialization of list of handlers when raising events Apr 16, 2024
@Youssef1313
Copy link
Member

The clone is made to allow the removal of an handler during the enumeration. This is not the common scenario, we could imagine to have a dedicated collection implementation that will clone the collection only if an element is being removed during enumeration / effectively removes the element only once enumeration is completed / ...

I attempted a quite similar thing in #14069

@Youssef1313 Youssef1313 added the area/performance 📈 Categorizes an issue or PR as relevant to performance label Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/performance 📈 Categorizes an issue or PR as relevant to performance difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. kind/enhancement New feature or request triage/untriaged Indicates an issue requires triaging or verification
Projects
None yet
Development

No branches or pull requests

2 participants