speed up prune_empty() on large patterns

This commit is contained in:
Jan Petykiewicz 2024-12-12 23:47:19 -08:00
parent 94a1b3d793
commit 9917355bb0

View File

@ -1068,20 +1068,22 @@ class ILibrary(ILibraryView, MutableMapping[str, 'Pattern'], metaclass=ABCMeta):
Returns: Returns:
A set containing the names of all deleted patterns A set containing the names of all deleted patterns
""" """
parent_graph = self.parent_graph()
empty = {name for name, pat in self.items() if pat.is_empty()}
trimmed = set() trimmed = set()
while empty := {name for name, pat in self.items() if pat.is_empty()}: while empty:
parents = set()
for name in empty: for name in empty:
del self[name] del self[name]
for parent in parent_graph[name]:
for pat in self.values(): del self[parent].refs[name]
for name in empty: parents |= parent_graph[name]
# Second pass to skip looking at refs in empty patterns
if name in pat.refs:
del pat.refs[name]
trimmed |= empty trimmed |= empty
if not repeat: if not repeat:
break break
empty = {parent for parent in parents if self[parent].is_empty()}
return trimmed return trimmed
def delete( def delete(