From f7db539fcd4d5018f9e22f06fd520581e8f5fd9b Mon Sep 17 00:00:00 2001 From: Gorgious56 Date: Wed, 27 May 2026 14:55:44 +0200 Subject: [PATCH] Wrap tool.System.get_decoration_data with TokenCache lookup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit System decoration draws on every viewport refresh — the ``_build_decoration_data`` body walks every distribution element, resolves connected ports, builds the vert/edge arrays for the GPU batch. A bare call per frame burns time on an unchanged scene. Add a single-entry cache keyed on ``(decorator_cache_token, id(decorated_elements_set))``. Reads short-circuit when neither component moved: * ``decorator_cache_token`` from ``bim.decorator_cache`` invalidates on depsgraph / undo / redo / load via the bump handler. * ``id(decorated_elements_set)`` invalidates when ``SystemDecorationData.load()`` reassigns the set (e.g. when the user changes the set of decorated systems via the panel). The handler that bumps the token is installed in the next commit (bim/handler.py decompose). Until then the token stays at 0, so the cache only hits when ``id()`` also matches — degraded behaviour during the bisect window but not incorrect. Generated with the assistance of an AI coding tool. --- src/bonsai/bonsai/tool/system.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/bonsai/bonsai/tool/system.py b/src/bonsai/bonsai/tool/system.py index bc11f3d642..8d2b421370 100644 --- a/src/bonsai/bonsai/tool/system.py +++ b/src/bonsai/bonsai/tool/system.py @@ -299,15 +299,29 @@ class System(bonsai.core.tool.System): system_props = cls.get_system_props() return tool.Ifc.get_entity_by_id(system_props.active_system_id) + # Decoration-data cache, keyed on (decorator_cache_token, id(decorated_elements_set)). + _decoration_data_cache_key: tuple | None = None + _decoration_data_cache: dict[str, Any] | None = None + @classmethod def get_decoration_data(cls) -> dict[str, Any]: + from bonsai.bim.decorator_cache import get_decorator_cache_token from bonsai.bim.module.system.data import ObjectSystemData, SystemDecorationData if not ObjectSystemData.is_loaded: ObjectSystemData.load() if not SystemDecorationData.is_loaded: SystemDecorationData.load() - return cls._build_decoration_data() + + token = get_decorator_cache_token() + key = (token, id(SystemDecorationData.data["decorated_elements"])) + if key == cls._decoration_data_cache_key and cls._decoration_data_cache is not None: + return cls._decoration_data_cache + + result = cls._build_decoration_data() + cls._decoration_data_cache_key = key + cls._decoration_data_cache = result + return result @classmethod def _build_decoration_data(cls) -> dict[str, Any]: