From 2663901603d62f267f61a46f04882ff6c1ebf49d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 21 Apr 2023 22:27:23 -0500 Subject: [PATCH] Speed up LazyEventPartialState for logbook (#91840) * Speed up LazyEventPartialState for logbook We should avoid the getattr call since every row would have to call the sqlalchemy key not found implemention if we blindly getattr * Speed up LazyEventPartialState for logbook We should avoid the getattr call since every row would have to call the sqlalchemy key not found implemention if we blindly getattr --- homeassistant/components/logbook/models.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/logbook/models.py b/homeassistant/components/logbook/models.py index e894b19c4d9..a7d3c517309 100644 --- a/homeassistant/components/logbook/models.py +++ b/homeassistant/components/logbook/models.py @@ -64,10 +64,12 @@ class LazyEventPartialState: self.context_id_bin: bytes | None = self.row.context_id_bin self.context_user_id_bin: bytes | None = self.row.context_user_id_bin self.context_parent_id_bin: bytes | None = self.row.context_parent_id_bin - if data := getattr(row, "data", None): + # We need to explicitly check for the row is EventAsRow as the unhappy path + # to fetch row.data for Row is very expensive + if type(row) is EventAsRow: # pylint: disable=unidiomatic-typecheck # If its an EventAsRow we can avoid the whole # json decode process as we already have the data - self.data = data + self.data = row.data return source = cast(str, self.row.shared_data or self.row.event_data) if not source: