Optimise remove_deep, which can make removing lots of elements 100x faster in scripts.

This commit is contained in:
Dion Moult
2024-05-23 20:15:01 +10:00
parent 993ce46fcc
commit a8729933af
2 changed files with 16 additions and 1 deletions
@@ -505,6 +505,11 @@ class file:
) -> list[ifcopenshell.entity_instance]: ) -> list[ifcopenshell.entity_instance]:
"""Return a list of entities that reference this entity """Return a list of entities that reference this entity
Warning: this is a slow function, especially when there is a large
number of inverses (such as for a shared owner history). If you are
only interested in the total number of inverses (typically 0, 1, or N),
consider using :func:`get_total_inverses`.
:param inst: The entity instance to get inverse relationships :param inst: The entity instance to get inverse relationships
:type inst: ifcopenshell.entity_instance :type inst: ifcopenshell.entity_instance
:param allow_duplicate: Returns a `list` when True, `set` when False :param allow_duplicate: Returns a `list` when True, `set` when False
@@ -530,6 +535,9 @@ class file:
def get_total_inverses(self, inst: ifcopenshell.entity_instance) -> int: def get_total_inverses(self, inst: ifcopenshell.entity_instance) -> int:
"""Returns the number of entities that reference this entity """Returns the number of entities that reference this entity
This is equivalent to `len(model.get_inverse(element))`, but
significantly faster.
:param inst: The entity instance to get inverse relationships :param inst: The entity instance to get inverse relationships
:type inst: ifcopenshell.entity_instance :type inst: ifcopenshell.entity_instance
:returns: The total number of references :returns: The total number of references
@@ -1315,6 +1315,8 @@ def remove_deep2(
:type element: ifcopenshell.entity_instance :type element: ifcopenshell.entity_instance
""" """
# ifc_file.batch() # ifc_file.batch()
if ifc_file.get_total_inverses(element):
return
to_delete = set() to_delete = set()
subgraph = list(ifc_file.traverse(element, breadth_first=True)) subgraph = list(ifc_file.traverse(element, breadth_first=True))
subgraph.extend(also_consider) subgraph.extend(also_consider)
@@ -1325,7 +1327,12 @@ def remove_deep2(
if ( if (
subelement.id() subelement.id()
and subelement not in do_not_delete and subelement not in do_not_delete
and len(set(ifc_file.get_inverse(subelement)) - subgraph_set) == 0 and (
# 0 or 1 inverses means it only exists in this subgraph
ifc_file.get_total_inverses(subelement) < 2
# Alternatively, let's ensure all inverses are within the subgrpah
or len(set(ifc_file.get_inverse(subelement)) - subgraph_set) == 0
)
): ):
to_delete.add(subelement) to_delete.add(subelement)
subelement_queue.extend(ifc_file.traverse(subelement, max_levels=1)[1:]) subelement_queue.extend(ifc_file.traverse(subelement, max_levels=1)[1:])