From 33b7c0a76fcdbb104789863b86e32e5b3868f7d4 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Fri, 1 Nov 2024 12:19:37 +0500 Subject: [PATCH] small optimizations for enum props --- src/bonsai/bonsai/bim/module/patch/prop.py | 4 ++-- src/bonsai/bonsai/bim/prop.py | 16 +++++++++------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/bonsai/bonsai/bim/module/patch/prop.py b/src/bonsai/bonsai/bim/module/patch/prop.py index 52ac9fd4b8..f491cb7ed4 100644 --- a/src/bonsai/bonsai/bim/module/patch/prop.py +++ b/src/bonsai/bonsai/bim/module/patch/prop.py @@ -46,7 +46,6 @@ def purge(): def get_ifcpatch_recipes(self, context): global ifcpatchrecipes_enum if len(ifcpatchrecipes_enum) < 1: - ifcpatchrecipes_enum.clear() # Have to add a blank entry because otherwise default recipe might be not loaded # properly (need to ensure bim.update_ifc_patch_arguments will be called). See #5540. ifcpatchrecipes_enum.append(("-", "-", "")) @@ -58,7 +57,8 @@ def get_ifcpatch_recipes(self, context): continue docs = ifcpatch.extract_docs(f, "Patcher", "__init__", ("src", "file", "logger", "args")) ifcpatchrecipes_enum.append((f, f, docs.get("description", "") if docs else "")) - return sorted(ifcpatchrecipes_enum, key=lambda x: x[0]) + ifcpatchrecipes_enum.sort(key=lambda x: x[0]) + return ifcpatchrecipes_enum def update_ifc_patch_recipe(self, context): diff --git a/src/bonsai/bonsai/bim/prop.py b/src/bonsai/bonsai/bim/prop.py index da8bbdfb23..9d5505c6b2 100644 --- a/src/bonsai/bonsai/bim/prop.py +++ b/src/bonsai/bonsai/bim/prop.py @@ -69,15 +69,16 @@ def update_global_tab(self: "BIMTabProperties", context: bpy.types.Context) -> N # https://blender.stackexchange.com/questions/216230/is-there-a-workaround-for-the-known-bug-in-dynamic-enumproperty # https://github.com/IfcOpenShell/IfcOpenShell/pull/1945 # https://github.com/IfcOpenShell/IfcOpenShell/issues/1941 -def cache_string(s): +# TODO: it would be nice to have some mechanism to clear that cache +# instead of storing it forever. +def cache_string(s: Any) -> str: + # TODO: is it ever non-string? s = str(s) - if not hasattr(cache_string, "data"): # Another way to define a function attribute - cache_string.data = defaultdict(str) cache_string.data[s] = s - return cache_string.data[s] + return s -cache_string.data = {} +cache_string.data: dict[str, str] = {} def get_attribute_enum_values(prop: "Attribute", context: bpy.types.Context) -> list[tuple[str, str, str]]: @@ -96,10 +97,11 @@ def get_attribute_enum_values(prop: "Attribute", context: bpy.types.Context) -> ) else: for e in data: + cache_string(e) items.append( ( - cache_string(e), - cache_string(e), + e, + e, "", ) )