-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
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
Logbook speedup #12566
Logbook speedup #12566
Conversation
homeassistant/components/logbook.py
Outdated
@@ -189,9 +189,6 @@ def humanify(events): | |||
if event.event_type == EVENT_STATE_CHANGED: | |||
entity_id = event.data.get('entity_id') | |||
|
|||
if entity_id is None: | |||
continue | |||
|
|||
if entity_id.startswith(tuple('{}.'.format( |
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.
Creating a tuple for each event is also slow. We should move this out of the for loop.
if hidden: | ||
continue | ||
|
||
domain = to_state.domain | ||
entity_id = to_state.entity_id | ||
|
||
elif event.event_type == EVENT_LOGBOOK_ENTRY: | ||
domain = event.data.get(ATTR_DOMAIN) |
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.
It's confusing that this var name is the same as the one used above.
Will also mess with if statement below??
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.
It is the same variable. We look for domain
and entity_id
in each kind of event and do the include/exclude filtering if one is found.
if hidden: | ||
continue | ||
|
||
domain = to_state.domain | ||
entity_id = to_state.entity_id | ||
|
||
elif event.event_type == EVENT_LOGBOOK_ENTRY: | ||
domain = event.data.get(ATTR_DOMAIN) | ||
entity_id = event.data.get(ATTR_ENTITY_ID) |
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.
We should migrate the filters below to helpers/entity_filter.py
, as that one returns an optimized function for the passed in configuration.
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.
All the excluded_domains
etc. filters? Agreed, though that seems like a separate opportunity that could fit in a separate PR :)
So if you want to be real scientific, and actually come up with measurements to proof your awesome work, consider writing a benchmark. (optional) |
I will try to add a benchmark tonight. |
Added a benchmark that is 35% faster for events that we keep and 90% faster for dropped events. I don't think we should treat this as too scientific but it definitely seems worth the trouble. |
Description:
The logbook often turns event data into a
State
object twice before dropping an event.This PR optimizes that down to zero
State
constructions for discarded events:State
constructionState
construction completelyThis gives me a solid 45% reduction on logbook loading so I wanted to get it out for comments even if I have not tested it much yet.
Credits to @OverloadUT for pointing me in this direction.
Related issue (if applicable): fixes #Checklist:
If the code does not interact with devices:
tox
run successfully.