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

Significantly speed up single use callback dispatchers #117934

Merged
merged 5 commits into from
May 24, 2024

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented May 22, 2024

Proposed change

We have many cases where we only use a dispatcher once. For example, in MQTT we use
the dispatchers for discovery and once the first one fires, we disconnect and
setup a different one. During startup this can quickly reach 10000+ dispatcher
connect and disconnects. Because we wrapped each job with catch_log_exceptions
which calls wraps() the cost of connecting up the dispatcher vastly exceeded
the cost of firing the dispatcher. This made the dispatcher a poor choice for
single use signals. To mitigate this, we no longer wrap callback dispatchers
in catch_log_exceptions and catch the exception inline to ensure the behavior
is the same but avoids the wraps() overhead.

Screenshot 2024-05-22 at 7 23 12 AM

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

bdraco added 2 commits May 22, 2024 07:20
We have many cases where we only use a dispatcher once. For example, in MQTT we use
the dispatchers for discovery and once the first one fires, we disconnect and
setup a different one. During startup this can quickly reach 10000+ dispatcher
connect and disconnects. Because we wrapped each job with catch_log_exceptions
which calls wraps() the cost of connecting up the dispatcher vastly exceeded
the cost of firing the dispatcher. This made the dispatcher a poor choice for
single use signals. To mitigate this, we no longer wrap callback dispatchers
in catch_log_exceptions and catch the exception inline.
We have many cases where we only use a dispatcher once. For example, in MQTT we use
the dispatchers for discovery and once the first one fires, we disconnect and
setup a different one. During startup this can quickly reach 10000+ dispatcher
connect and disconnects. Because we wrapped each job with catch_log_exceptions
which calls wraps() the cost of connecting up the dispatcher vastly exceeded
the cost of firing the dispatcher. This made the dispatcher a poor choice for
single use signals. To mitigate this, we no longer wrap callback dispatchers
in catch_log_exceptions and catch the exception inline.
@home-assistant home-assistant bot added cla-signed core small-pr PRs with less than 30 lines. labels May 22, 2024
@bdraco
Copy link
Member Author

bdraco commented May 22, 2024

looks like we need to fix some stuff in mqtt first.

@bdraco bdraco closed this May 22, 2024
@github-actions github-actions bot locked and limited conversation to collaborators May 23, 2024
@home-assistant home-assistant unlocked this conversation May 24, 2024
@bdraco bdraco reopened this May 24, 2024
@bdraco
Copy link
Member Author

bdraco commented May 24, 2024

Screenshot 2024-05-23 at 11 02 11 PM

At 66 entities per device its quite the speed up #109030

@bdraco
Copy link
Member Author

bdraco commented May 24, 2024

Another option would be to not use the dispatcher in mqtt since its rare that we have these single use high volume use cases

@bdraco
Copy link
Member Author

bdraco commented May 24, 2024

I think I might be overthinking that. The maint burden is low, and this change is strait-forward so better to not develop something one off for mqtt.

@bdraco
Copy link
Member Author

bdraco commented May 24, 2024

It does also nicely speed up mobile app setup as well

@bdraco
Copy link
Member Author

bdraco commented May 24, 2024

I was hoping to make catch_log_exception faster instead but I don't see a path do that.

@bdraco
Copy link
Member Author

bdraco commented May 24, 2024

I'm not sure what the solution to replace catch_log_exception in async_subscribe should be. I was hoping whatever we come up with there could be reused here but its probably going to be something different so might not be relevant at all.

Copy link
Contributor

@jbouwh jbouwh left a comment

Choose a reason for hiding this comment

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

Nice, looks good to me!
Thnx @bdraco 👍

@bdraco
Copy link
Member Author

bdraco commented May 24, 2024

This looks solid, but will run some more profiles in the morning just to be sure since its too late here

@bdraco
Copy link
Member Author

bdraco commented May 24, 2024

Even if we can make catch_log_exception faster its likely going to be 10000 partials in memory (and another 20000 from mqtt). 10 more lines of code here vs 10000 partials in memory is a reasonable trade off.

I'm happy with this after sleeping on it.

@bdraco bdraco marked this pull request as ready for review May 24, 2024 21:21
@bdraco bdraco requested a review from a team as a code owner May 24, 2024 21:21
@bdraco bdraco merged commit cf73a47 into dev May 24, 2024
38 checks passed
@bdraco bdraco deleted the single_use_dispatcher_too_slow branch May 24, 2024 21:21
@github-actions github-actions bot locked and limited conversation to collaborators May 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla-signed core small-pr PRs with less than 30 lines.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants