Use slots for recorder tasks to reduce memory (#90387)

pull/90410/head
J. Nick Koston 2023-03-28 00:24:12 -10:00 committed by GitHub
parent 6fbdcac323
commit be5714e3fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 29 deletions

View File

@ -26,7 +26,8 @@ if TYPE_CHECKING:
from .core import Recorder
class RecorderTask(abc.ABC):
@dataclass(slots=True)
class RecorderTask:
"""ABC for recorder tasks."""
commit_before = True
@ -36,7 +37,7 @@ class RecorderTask(abc.ABC):
"""Handle the task."""
@dataclass
@dataclass(slots=True)
class ChangeStatisticsUnitTask(RecorderTask):
"""Object to store statistics_id and unit to convert unit of statistics."""
@ -54,7 +55,7 @@ class ChangeStatisticsUnitTask(RecorderTask):
)
@dataclass
@dataclass(slots=True)
class ClearStatisticsTask(RecorderTask):
"""Object to store statistics_ids which for which to remove statistics."""
@ -65,7 +66,7 @@ class ClearStatisticsTask(RecorderTask):
statistics.clear_statistics(instance, self.statistic_ids)
@dataclass
@dataclass(slots=True)
class UpdateStatisticsMetadataTask(RecorderTask):
"""Object to store statistics_id and unit for update of statistics metadata."""
@ -83,7 +84,7 @@ class UpdateStatisticsMetadataTask(RecorderTask):
)
@dataclass
@dataclass(slots=True)
class UpdateStatesMetadataTask(RecorderTask):
"""Task to update states metadata."""
@ -99,7 +100,7 @@ class UpdateStatesMetadataTask(RecorderTask):
)
@dataclass
@dataclass(slots=True)
class PurgeTask(RecorderTask):
"""Object to store information about purge task."""
@ -125,7 +126,7 @@ class PurgeTask(RecorderTask):
)
@dataclass
@dataclass(slots=True)
class PurgeEntitiesTask(RecorderTask):
"""Object to store entity information about purge task."""
@ -140,7 +141,7 @@ class PurgeEntitiesTask(RecorderTask):
instance.queue_task(PurgeEntitiesTask(self.entity_filter, self.purge_before))
@dataclass
@dataclass(slots=True)
class PerodicCleanupTask(RecorderTask):
"""An object to insert into the recorder to trigger cleanup tasks.
@ -152,7 +153,7 @@ class PerodicCleanupTask(RecorderTask):
periodic_db_cleanups(instance)
@dataclass
@dataclass(slots=True)
class StatisticsTask(RecorderTask):
"""An object to insert into the recorder queue to run a statistics task."""
@ -167,7 +168,7 @@ class StatisticsTask(RecorderTask):
instance.queue_task(StatisticsTask(self.start, self.fire_events))
@dataclass
@dataclass(slots=True)
class CompileMissingStatisticsTask(RecorderTask):
"""An object to insert into the recorder queue to run a compile missing statistics."""
@ -179,7 +180,7 @@ class CompileMissingStatisticsTask(RecorderTask):
instance.queue_task(CompileMissingStatisticsTask())
@dataclass
@dataclass(slots=True)
class ImportStatisticsTask(RecorderTask):
"""An object to insert into the recorder queue to run an import statistics task."""
@ -199,7 +200,7 @@ class ImportStatisticsTask(RecorderTask):
)
@dataclass
@dataclass(slots=True)
class AdjustStatisticsTask(RecorderTask):
"""An object to insert into the recorder queue to run an adjust statistics task."""
@ -229,7 +230,7 @@ class AdjustStatisticsTask(RecorderTask):
)
@dataclass
@dataclass(slots=True)
class WaitTask(RecorderTask):
"""An object to insert into the recorder queue.
@ -243,7 +244,7 @@ class WaitTask(RecorderTask):
instance._queue_watch.set() # pylint: disable=[protected-access]
@dataclass
@dataclass(slots=True)
class DatabaseLockTask(RecorderTask):
"""An object to insert into the recorder queue to prevent writes to the database."""
@ -256,7 +257,7 @@ class DatabaseLockTask(RecorderTask):
instance._lock_database(self) # pylint: disable=[protected-access]
@dataclass
@dataclass(slots=True)
class StopTask(RecorderTask):
"""An object to insert into the recorder queue to stop the event handler."""
@ -267,7 +268,7 @@ class StopTask(RecorderTask):
instance.stop_requested = True
@dataclass
@dataclass(slots=True)
class EventTask(RecorderTask):
"""An event to be processed."""
@ -280,7 +281,7 @@ class EventTask(RecorderTask):
instance._process_one_event(self.event)
@dataclass
@dataclass(slots=True)
class KeepAliveTask(RecorderTask):
"""A keep alive to be sent."""
@ -292,7 +293,7 @@ class KeepAliveTask(RecorderTask):
instance._send_keep_alive()
@dataclass
@dataclass(slots=True)
class CommitTask(RecorderTask):
"""Commit the event session."""
@ -304,7 +305,7 @@ class CommitTask(RecorderTask):
instance._commit_event_session_or_retry()
@dataclass
@dataclass(slots=True)
class AddRecorderPlatformTask(RecorderTask):
"""Add a recorder platform."""
@ -321,7 +322,7 @@ class AddRecorderPlatformTask(RecorderTask):
platforms[domain] = platform
@dataclass
@dataclass(slots=True)
class SynchronizeTask(RecorderTask):
"""Ensure all pending data has been committed."""
@ -335,7 +336,7 @@ class SynchronizeTask(RecorderTask):
instance.hass.loop.call_soon_threadsafe(self.event.set)
@dataclass
@dataclass(slots=True)
class PostSchemaMigrationTask(RecorderTask):
"""Post migration task to update schema."""
@ -349,7 +350,7 @@ class PostSchemaMigrationTask(RecorderTask):
)
@dataclass
@dataclass(slots=True)
class StatisticsTimestampMigrationCleanupTask(RecorderTask):
"""An object to insert into the recorder queue to run a statistics migration cleanup task."""
@ -360,7 +361,7 @@ class StatisticsTimestampMigrationCleanupTask(RecorderTask):
instance.queue_task(StatisticsTimestampMigrationCleanupTask())
@dataclass
@dataclass(slots=True)
class AdjustLRUSizeTask(RecorderTask):
"""An object to insert into the recorder queue to adjust the LRU size."""
@ -371,7 +372,7 @@ class AdjustLRUSizeTask(RecorderTask):
instance._adjust_lru_size() # pylint: disable=[protected-access]
@dataclass
@dataclass(slots=True)
class StatesContextIDMigrationTask(RecorderTask):
"""An object to insert into the recorder queue to migrate states context ids."""
@ -386,7 +387,7 @@ class StatesContextIDMigrationTask(RecorderTask):
instance.queue_task(StatesContextIDMigrationTask())
@dataclass
@dataclass(slots=True)
class EventsContextIDMigrationTask(RecorderTask):
"""An object to insert into the recorder queue to migrate events context ids."""
@ -401,7 +402,7 @@ class EventsContextIDMigrationTask(RecorderTask):
instance.queue_task(EventsContextIDMigrationTask())
@dataclass
@dataclass(slots=True)
class EventTypeIDMigrationTask(RecorderTask):
"""An object to insert into the recorder queue to migrate event type ids."""
@ -417,7 +418,7 @@ class EventTypeIDMigrationTask(RecorderTask):
instance.queue_task(EventTypeIDMigrationTask())
@dataclass
@dataclass(slots=True)
class EntityIDMigrationTask(RecorderTask):
"""An object to insert into the recorder queue to migrate entity_ids to StatesMeta."""
@ -440,7 +441,7 @@ class EntityIDMigrationTask(RecorderTask):
instance.queue_task(EntityIDPostMigrationTask())
@dataclass
@dataclass(slots=True)
class EntityIDPostMigrationTask(RecorderTask):
"""An object to insert into the recorder queue to cleanup after entity_ids migration."""
@ -453,7 +454,7 @@ class EntityIDPostMigrationTask(RecorderTask):
instance.queue_task(EntityIDPostMigrationTask())
@dataclass
@dataclass(slots=True)
class EventIdMigrationTask(RecorderTask):
"""An object to insert into the recorder queue to cleanup legacy event_ids in the states table.