Small speed up to creating stats database rows (#124587)

* Small speed up to creating stats database rows

Calling .timestamp() directly is faster on new cpython

* more cleanup
This commit is contained in:
J. Nick Koston 2024-08-26 23:18:12 -10:00 committed by GitHub
parent 902d76da36
commit 4bc19876ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 5 deletions

View File

@ -693,13 +693,13 @@ class StatisticsBase:
@classmethod
def from_stats(cls, metadata_id: int, stats: StatisticData) -> Self:
"""Create object from a statistics with datatime objects."""
"""Create object from a statistics with datetime objects."""
return cls( # type: ignore[call-arg]
metadata_id=metadata_id,
created=None,
created_ts=time.time(),
start=None,
start_ts=dt_util.utc_to_timestamp(stats["start"]),
start_ts=stats["start"].timestamp(),
mean=stats.get("mean"),
min=stats.get("min"),
max=stats.get("max"),

View File

@ -65,9 +65,7 @@ def process_datetime_to_timestamp(ts: datetime) -> float:
def datetime_to_timestamp_or_none(dt: datetime | None) -> float | None:
"""Convert a datetime to a timestamp."""
if dt is None:
return None
return dt_util.utc_to_timestamp(dt)
return None if dt is None else dt.timestamp()
def timestamp_to_datetime_or_none(ts: float | None) -> datetime | None: