Small performance improvement in tracking template results (#110622)

- Avoid inner function creation each refresh
- remove extra unneeded checks from ratelimit
pull/107600/head^2
J. Nick Koston 2024-02-17 11:08:24 -06:00 committed by GitHub
parent 094fd3d918
commit 664285b9d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 20 deletions

View File

@ -1106,6 +1106,24 @@ class TrackTemplateResultInfo:
return result_as_boolean(result)
@callback
def _apply_update(
self,
updates: list[TrackTemplateResult],
update: bool | TrackTemplateResult,
template: Template,
) -> bool:
"""Handle updates of a tracked template."""
if not update:
return False
self._setup_time_listener(template, self._info[template].has_time)
if isinstance(update, TrackTemplateResult):
updates.append(update)
return True
@callback
def _refresh(
self,
@ -1129,20 +1147,6 @@ class TrackTemplateResultInfo:
info_changed = False
now = event.time_fired if not replayed and event else dt_util.utcnow()
def _apply_update(
update: bool | TrackTemplateResult, template: Template
) -> bool:
"""Handle updates of a tracked template."""
if not update:
return False
self._setup_time_listener(template, self._info[template].has_time)
if isinstance(update, TrackTemplateResult):
updates.append(update)
return True
block_updates = False
super_template = self._track_templates[0] if self._has_super_template else None
@ -1151,7 +1155,7 @@ class TrackTemplateResultInfo:
# Update the super template first
if super_template is not None:
update = self._render_template_if_ready(super_template, now, event)
info_changed |= _apply_update(update, super_template.template)
info_changed |= self._apply_update(updates, update, super_template.template)
if isinstance(update, TrackTemplateResult):
super_result = update.result
@ -1182,7 +1186,9 @@ class TrackTemplateResultInfo:
continue
update = self._render_template_if_ready(track_template_, now, event)
info_changed |= _apply_update(update, track_template_.template)
info_changed |= self._apply_update(
updates, update, track_template_.template
)
if info_changed:
assert self._track_state_changes

View File

@ -30,9 +30,7 @@ class KeyedRateLimit:
@callback
def async_has_timer(self, key: Hashable) -> bool:
"""Check if a rate limit timer is running."""
if not self._rate_limit_timers:
return False
return key in self._rate_limit_timers
return bool(self._rate_limit_timers and key in self._rate_limit_timers)
@callback
def async_triggered(self, key: Hashable, now: datetime | None = None) -> None:
@ -43,7 +41,7 @@ class KeyedRateLimit:
@callback
def async_cancel_timer(self, key: Hashable) -> None:
"""Cancel a rate limit time that will call the action."""
if not self._rate_limit_timers or not self.async_has_timer(key):
if not self._rate_limit_timers or key not in self._rate_limit_timers:
return
self._rate_limit_timers.pop(key).cancel()