From 361498b7f58f964d6b671ff527e8c6cf2e79f801 Mon Sep 17 00:00:00 2001 From: Julian Pawlowski Date: Fri, 17 Apr 2026 12:00:48 +0000 Subject: [PATCH] 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. --- .../tibber_prices/interval_pool/manager.py | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/custom_components/tibber_prices/interval_pool/manager.py b/custom_components/tibber_prices/interval_pool/manager.py index 97a4eb3..c13a7a5 100644 --- a/custom_components/tibber_prices/interval_pool/manager.py +++ b/custom_components/tibber_prices/interval_pool/manager.py @@ -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