From 4fd92c17f077bf842d054a48c2af6ecc48722b74 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 4 Aug 2024 17:06:32 -0500 Subject: [PATCH] Optimize logbook row matching (#123127) --- homeassistant/components/logbook/processor.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/logbook/processor.py b/homeassistant/components/logbook/processor.py index 05566e52509..426f33fadb2 100644 --- a/homeassistant/components/logbook/processor.py +++ b/homeassistant/components/logbook/processor.py @@ -332,7 +332,7 @@ class ContextAugmenter: if not (context_row := self._get_context_row(context_id_bin, row)): return - if _rows_match(row, context_row): + if row is context_row or _rows_ids_match(row, context_row): # This is the first event with the given ID. Was it directly caused by # a parent event? context_parent_id_bin = row[CONTEXT_PARENT_ID_BIN_POS] @@ -348,7 +348,7 @@ class ContextAugmenter: return # Ensure the (parent) context_event exists and is not the root cause of # this log entry. - if _rows_match(row, context_row): + if row is context_row or _rows_ids_match(row, context_row): return event_type = context_row[EVENT_TYPE_POS] # State change @@ -396,13 +396,9 @@ class ContextAugmenter: data[CONTEXT_ENTITY_ID_NAME] = self.entity_name_cache.get(attr_entity_id) -def _rows_match(row: Row | EventAsRow, other_row: Row | EventAsRow) -> bool: +def _rows_ids_match(row: Row | EventAsRow, other_row: Row | EventAsRow) -> bool: """Check of rows match by using the same method as Events __hash__.""" - return bool( - row is other_row - or (row_id := row[ROW_ID_POS]) - and row_id == other_row[ROW_ID_POS] - ) + return bool((row_id := row[ROW_ID_POS]) and row_id == other_row[ROW_ID_POS]) class EntityNameCache: