From 0b08ae7e44738cef5ac1443bdc122c900820cedd Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 1 May 2024 11:04:20 -0500 Subject: [PATCH] Reduce timestamp function call overhead in core states (#116517) * Reduce timestamp function call overhead in core states The recorder or the websocket_api will always call the timestamps, so we will set the timestamp values when creating the State to avoid the function call overhead in the property we know will always be called. * Reduce timestamp function call overhead in core states The recorder or the websocket_api will always call the timestamps, so we will set the timestamp values when creating the State to avoid the function call overhead in the property we know will always be called. * reduce scope of change since last_reported is not called in websocket_api * reduce scope of change since last_reported is not called in websocket_api --- homeassistant/core.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index 73d0e82fa83..40d6a544713 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -1790,6 +1790,12 @@ class State: self.context = context or Context() self.state_info = state_info self.domain, self.object_id = split_entity_id(self.entity_id) + # The recorder or the websocket_api will always call the timestamps, + # so we will set the timestamp values here to avoid the overhead of + # the function call in the property we know will always be called. + self.last_updated_timestamp = self.last_updated.timestamp() + if self.last_changed == self.last_updated: + self.__dict__["last_changed_timestamp"] = self.last_updated_timestamp @cached_property def name(self) -> str: @@ -1801,8 +1807,6 @@ class State: @cached_property def last_changed_timestamp(self) -> float: """Timestamp of last change.""" - if self.last_changed == self.last_updated: - return self.last_updated_timestamp return self.last_changed.timestamp() @cached_property @@ -1812,11 +1816,6 @@ class State: return self.last_updated_timestamp return self.last_reported.timestamp() - @cached_property - def last_updated_timestamp(self) -> float: - """Timestamp of last update.""" - return self.last_updated.timestamp() - @cached_property def _as_dict(self) -> dict[str, Any]: """Return a dict representation of the State.