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
This commit is contained in:
J. Nick Koston 2024-05-01 11:04:20 -05:00 committed by GitHub
parent 25df41475a
commit 0b08ae7e44
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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.