Reduce overhead to save the larger registries (#113462)
We save the device and entity registry to disk quite often, and the cost of serializing them to the storage can block the event loop for >100ms. Add a cache to reduce the change the loop is blocked at an inopportune time at run time. The first write after startup will still be a little slow but we do have to serialize the bulk of it at least once as there is no way to avoid this ``` 2024-03-14 11:28:19.765 WARNING (MainThread) [homeassistant.helpers.storage] Writing data with data_func: core.device_registry 2024-03-14 11:28:20.020 WARNING (MainThread) [homeassistant.helpers.storage] Writing data with data_func: core.entity_registry 2024-03-14 11:28:20.178 WARNING (MainThread) [asyncio] Executing <TimerHandle cancelled when=2319925.760294916 Store._async_schedule_callback_delayed_write() created at /Users/bdraco/home-assistant/homeassistant/helpers/storage.py:328> took 0.159 seconds ```
This commit is contained in:
parent
28836be3eb
commit
5b80eb4c3d
2 changed files with 110 additions and 86 deletions
|
@ -31,7 +31,7 @@ from .deprecation import (
|
|||
dir_with_deprecated_constants,
|
||||
)
|
||||
from .frame import report
|
||||
from .json import JSON_DUMP, find_paths_unserializable_data, json_bytes
|
||||
from .json import JSON_DUMP, find_paths_unserializable_data, json_bytes, json_fragment
|
||||
from .registry import BaseRegistry
|
||||
from .typing import UNDEFINED, UndefinedType
|
||||
|
||||
|
@ -301,8 +301,35 @@ class DeviceEntry:
|
|||
)
|
||||
return None
|
||||
|
||||
@cached_property
|
||||
def as_storage_fragment(self) -> json_fragment:
|
||||
"""Return a json fragment for storage."""
|
||||
return json_fragment(
|
||||
json_bytes(
|
||||
{
|
||||
"area_id": self.area_id,
|
||||
"config_entries": list(self.config_entries),
|
||||
"configuration_url": self.configuration_url,
|
||||
"connections": list(self.connections),
|
||||
"disabled_by": self.disabled_by,
|
||||
"entry_type": self.entry_type,
|
||||
"hw_version": self.hw_version,
|
||||
"id": self.id,
|
||||
"identifiers": list(self.identifiers),
|
||||
"labels": list(self.labels),
|
||||
"manufacturer": self.manufacturer,
|
||||
"model": self.model,
|
||||
"name_by_user": self.name_by_user,
|
||||
"name": self.name,
|
||||
"serial_number": self.serial_number,
|
||||
"sw_version": self.sw_version,
|
||||
"via_device_id": self.via_device_id,
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
@attr.s(slots=True, frozen=True)
|
||||
|
||||
@attr.s(frozen=True)
|
||||
class DeletedDeviceEntry:
|
||||
"""Deleted Device Registry Entry."""
|
||||
|
||||
|
@ -328,6 +355,21 @@ class DeletedDeviceEntry:
|
|||
is_new=True,
|
||||
)
|
||||
|
||||
@cached_property
|
||||
def as_storage_fragment(self) -> json_fragment:
|
||||
"""Return a json fragment for storage."""
|
||||
return json_fragment(
|
||||
json_bytes(
|
||||
{
|
||||
"config_entries": list(self.config_entries),
|
||||
"connections": list(self.connections),
|
||||
"identifiers": list(self.identifiers),
|
||||
"id": self.id,
|
||||
"orphaned_timestamp": self.orphaned_timestamp,
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@lru_cache(maxsize=512)
|
||||
def format_mac(mac: str) -> str:
|
||||
|
@ -904,44 +946,14 @@ class DeviceRegistry(BaseRegistry):
|
|||
self._device_data = devices.data
|
||||
|
||||
@callback
|
||||
def _data_to_save(self) -> dict[str, list[dict[str, Any]]]:
|
||||
def _data_to_save(self) -> dict[str, Any]:
|
||||
"""Return data of device registry to store in a file."""
|
||||
data: dict[str, list[dict[str, Any]]] = {}
|
||||
|
||||
data["devices"] = [
|
||||
{
|
||||
"area_id": entry.area_id,
|
||||
"config_entries": list(entry.config_entries),
|
||||
"configuration_url": entry.configuration_url,
|
||||
"connections": list(entry.connections),
|
||||
"disabled_by": entry.disabled_by,
|
||||
"entry_type": entry.entry_type,
|
||||
"hw_version": entry.hw_version,
|
||||
"id": entry.id,
|
||||
"identifiers": list(entry.identifiers),
|
||||
"labels": list(entry.labels),
|
||||
"manufacturer": entry.manufacturer,
|
||||
"model": entry.model,
|
||||
"name_by_user": entry.name_by_user,
|
||||
"name": entry.name,
|
||||
"serial_number": entry.serial_number,
|
||||
"sw_version": entry.sw_version,
|
||||
"via_device_id": entry.via_device_id,
|
||||
}
|
||||
for entry in self.devices.values()
|
||||
]
|
||||
data["deleted_devices"] = [
|
||||
{
|
||||
"config_entries": list(entry.config_entries),
|
||||
"connections": list(entry.connections),
|
||||
"identifiers": list(entry.identifiers),
|
||||
"id": entry.id,
|
||||
"orphaned_timestamp": entry.orphaned_timestamp,
|
||||
}
|
||||
for entry in self.deleted_devices.values()
|
||||
]
|
||||
|
||||
return data
|
||||
return {
|
||||
"devices": [entry.as_storage_fragment for entry in self.devices.values()],
|
||||
"deleted_devices": [
|
||||
entry.as_storage_fragment for entry in self.deleted_devices.values()
|
||||
],
|
||||
}
|
||||
|
||||
@callback
|
||||
def async_clear_config_entry(self, config_entry_id: str) -> None:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue