From f883f721c8e75f91a6b34bbc671ef19d82672bef Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 24 Jan 2024 08:52:34 -1000 Subject: [PATCH] Avoid copying translations for single components (#108645) --- homeassistant/helpers/translation.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/homeassistant/helpers/translation.py b/homeassistant/helpers/translation.py index e20a290a4e2..ab9d5f576fe 100644 --- a/homeassistant/helpers/translation.py +++ b/homeassistant/helpers/translation.py @@ -218,8 +218,14 @@ class _TranslationCache: if components_to_load := components - loaded: await self._async_load(language, components_to_load) - result: dict[str, str] = {} category_cache = self.cache.get(language, {}).get(category, {}) + # If only one component was requested, return it directly + # to avoid merging the dictionaries and keeping additional + # copies of the same data in memory. + if len(components) == 1 and (component := next(iter(components))): + return category_cache.get(component, {}) + + result: dict[str, str] = {} for component in components.intersection(category_cache): result.update(category_cache[component]) return result @@ -298,7 +304,6 @@ class _TranslationCache: """Extract resources into the cache.""" resource: dict[str, Any] | str cached = self.cache.setdefault(language, {}) - categories: set[str] = set() for resource in translation_strings.values(): categories.update(resource)