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
Speed up reconnects by caching state serialize #93050
Conversation
This will help with: - mobile app reconnects (still uses get_states) - frontend reconnects (using subscribe_entities) - third party reconnects (most still using get_states)
Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
Been using this for a bit now. No more delay waiting for the reconnect when opening HA on my phone after the phone has gone to sleep |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
) -> None: | ||
"""Send handle get states response.""" | ||
connection.send_message( | ||
_HANDLE_GET_STATES_TEMPLATE.replace( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a future PR I think we can make this a bit more efficient by making a function and can construct event and result messages without replace. I'm going to explore that and if successful will do a PR that does that for every place we do this. I am not 100% sure it will work, just an idea at this point
@@ -1260,6 +1263,8 @@ def __init__( | |||
self.domain, self.object_id = split_entity_id(self.entity_id) | |||
self._as_dict: ReadOnlyDict[str, Collection[Any]] | None = None | |||
self._as_compressed_state: dict[str, Any] | None = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can drop the unserialized cache in a future PR to save a small bit of RAM but need to verify it's always covered by the serialized cache.
Thanks |
This also eliminated the spikes I would see in my cpu graphs when the desktop or iOS app would reconnect |
thank you. |
Proposed change
This will help with:
This solves one of the last performance bottlenecks on the list. This makes a noticeable difference on how fast the frontend can be usable on reconnects. The goal is to be able to load the frontend as fast as possible from phone pickup / unlock / control device. The bulk of the waiting is now on the frontend code (this continues to improve)
This is what the flame graph looks like for restoring a frontend connection before:
After
Type of change
Additional information
Checklist
black --fast homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest
.requirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
..coveragerc
.To help with the load of incoming pull requests: