Switch utc_to_timestamp to .timestamp() where possible (#109729)

* Switch utc_to_timestamp to .timestamp()

.timestamp() is faster now in newer cpython

```
from homeassistant.util.dt import utc_to_timestamp, utcnow
import timeit
now = utcnow()
print(timeit.timeit('utc_to_timestamp(now)',globals={"now":now,"utc_to_timestamp":utc_to_timestamp}))
print(timeit.timeit('now.timestamp()',globals={"now":now}))
```

utc_to_timestamp = 0.18721245788037777
timestamp = 0.11421508435159922

* compat

* revert

* revert

* revert

* revert

* revert
This commit is contained in:
J. Nick Koston 2024-02-05 16:04:52 -06:00 committed by GitHub
parent e399bebbcd
commit f73431ac06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 12 additions and 17 deletions

View File

@ -179,8 +179,8 @@ def _generate_stream_message(
"""Generate a history stream message response."""
return {
"states": states,
"start_time": dt_util.utc_to_timestamp(start_day),
"end_time": dt_util.utc_to_timestamp(end_day),
"start_time": start_day.timestamp(),
"end_time": end_day.timestamp(),
}

View File

@ -9,7 +9,6 @@ from sqlalchemy.sql.lambdas import StatementLambdaElement
from homeassistant.components.recorder.filters import Filters
from homeassistant.components.recorder.models import ulid_to_bytes_or_none
from homeassistant.helpers.json import json_dumps
from homeassistant.util import dt as dt_util
from .all import all_stmt
from .devices import devices_stmt
@ -28,8 +27,8 @@ def statement_for_request(
context_id: str | None = None,
) -> StatementLambdaElement:
"""Generate the logbook statement for a logbook request."""
start_day = dt_util.utc_to_timestamp(start_day_dt)
end_day = dt_util.utc_to_timestamp(end_day_dt)
start_day = start_day_dt.timestamp()
end_day = end_day_dt.timestamp()
# No entities: logbook sends everything for the timeframe
# limited by the context_id and the yaml configured filter
if not entity_ids and not device_ids:

View File

@ -184,8 +184,8 @@ def _generate_stream_message(
"""Generate a logbook stream message response."""
return {
"events": events,
"start_time": dt_util.utc_to_timestamp(start_day),
"end_time": dt_util.utc_to_timestamp(end_day),
"start_time": start_day.timestamp(),
"end_time": end_day.timestamp(),
}

View File

@ -10,8 +10,6 @@ from typing import TYPE_CHECKING
from sqlalchemy.orm.session import Session
import homeassistant.util.dt as dt_util
from .db_schema import Events, States, StatesMeta
from .models import DatabaseEngine
from .queries import (
@ -251,7 +249,7 @@ def _select_state_attributes_ids_to_purge(
state_ids = set()
attributes_ids = set()
for state_id, attributes_id in session.execute(
find_states_to_purge(dt_util.utc_to_timestamp(purge_before), max_bind_vars)
find_states_to_purge(purge_before.timestamp(), max_bind_vars)
).all():
state_ids.add(state_id)
if attributes_id:
@ -271,7 +269,7 @@ def _select_event_data_ids_to_purge(
event_ids = set()
data_ids = set()
for event_id, data_id in session.execute(
find_events_to_purge(dt_util.utc_to_timestamp(purge_before), max_bind_vars)
find_events_to_purge(purge_before.timestamp(), max_bind_vars)
).all():
event_ids.add(event_id)
if data_id:
@ -464,7 +462,7 @@ def _select_legacy_detached_state_and_attributes_and_data_ids_to_purge(
"""
states = session.execute(
find_legacy_detached_states_and_attributes_to_purge(
dt_util.utc_to_timestamp(purge_before), max_bind_vars
purge_before.timestamp(), max_bind_vars
)
).all()
_LOGGER.debug("Selected %s state ids to remove", len(states))
@ -489,7 +487,7 @@ def _select_legacy_event_state_and_attributes_and_data_ids_to_purge(
"""
events = session.execute(
find_legacy_event_state_and_attributes_and_data_ids_to_purge(
dt_util.utc_to_timestamp(purge_before), max_bind_vars
purge_before.timestamp(), max_bind_vars
)
).all()
_LOGGER.debug("Selected %s event ids to remove", len(events))

View File

@ -1077,9 +1077,7 @@ class Event:
self.origin = origin
self.time_fired = time_fired or dt_util.utcnow()
if not context:
context = Context(
id=ulid_at_time(dt_util.utc_to_timestamp(self.time_fired))
)
context = Context(id=ulid_at_time(self.time_fired.timestamp()))
self.context = context
if not context.origin_event:
context.origin_event = self

View File

@ -1442,7 +1442,7 @@ def async_track_point_in_utc_time(
"""
# Ensure point_in_time is UTC
utc_point_in_time = dt_util.as_utc(point_in_time)
expected_fire_timestamp = dt_util.utc_to_timestamp(utc_point_in_time)
expected_fire_timestamp = utc_point_in_time.timestamp()
job = (
action
if isinstance(action, HassJob)