From e3bbec6394d5c3b1b4b4387158ca69ea465aae97 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Mon, 16 Jun 2025 18:18:14 +0500 Subject: [PATCH] entity_instance constructor and destructor micro optimizations --- .../ifcopenshell/entity_instance.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/entity_instance.py b/src/ifcopenshell-python/ifcopenshell/entity_instance.py index da31b702cf..53ef66cbd0 100644 --- a/src/ifcopenshell-python/ifcopenshell/entity_instance.py +++ b/src/ifcopenshell-python/ifcopenshell/entity_instance.py @@ -162,13 +162,16 @@ class entity_instance: """ :param e: Wrapper's ``entity_instance`` or a tuple ``(schema_identifier, ifc_class)``. """ + # Instances of this class will be created and removed very often, + # so it's important to keep it very optimized. + if isinstance(e, tuple): e = ifcopenshell_wrapper.new_IfcBaseClass(*e) - super().__setattr__("wrapped_data", e) - super().__setattr__("method_list", None) + object.__setattr__(self, "wrapped_data", e) + object.__setattr__(self, "method_list", None) # Make sure the file is not gc'ed while we have live instances - self.wrapped_data.file = file + e.file = file def __del__(self): """ @@ -180,7 +183,7 @@ class entity_instance: # and wrapped_data is unset. Hacky since we override # both __dict__ and __dir__. try: - wrapped_data = super().__getattribute__("wrapped_data") + wrapped_data = object.__getattribute__(self, "wrapped_data") wrapped_data.file = None except AttributeError: return