perf(interval_pool): run GC after touch-only interval updates

Touch operations create dead intervals in old fetch groups, but GC only
ran when new intervals were added. Dead intervals accumulated until
the next fetch with genuinely new data.

Now run GC after touch-only paths and schedule a save if data changed.

Impact: Reduces memory usage by cleaning up stale fetch groups promptly
instead of letting dead intervals accumulate between API fetches.
This commit is contained in:
Julian Pawlowski 2026-04-17 12:00:48 +00:00
parent ebcb9cfe77
commit 361498b7f5

View file

@ -725,13 +725,20 @@ class TibberPricesIntervalPool:
if intervals_to_touch:
self._touch_intervals(intervals_to_touch, fetch_time_dt)
if not new_intervals:
if intervals_to_touch:
_LOGGER_DETAILS.debug(
"All %d intervals already cached for home %s (touched only)",
len(intervals),
self._home_id,
)
# Run GC after touch even if no new intervals — touching creates dead
# intervals in old fetch groups that should be cleaned up promptly.
if intervals_to_touch and not new_intervals:
gc_changed_data = self._gc.run_gc()
_LOGGER_DETAILS.debug(
"All %d intervals already cached for home %s (touched only, GC ran: %s)",
len(intervals),
self._home_id,
gc_changed_data,
)
if (intervals_to_touch or gc_changed_data) and self._hass is not None and self._entry_id is not None:
self._schedule_debounced_save()
return
# Sort new intervals by startsAt