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:
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()
while empty := {name for name, pat in self.items() if pat.is_empty()}:
while empty:
parents = set()
for name in empty:
del self[name]
for pat in self.values():
for name in empty:
# Second pass to skip looking at refs in empty patterns
if name in pat.refs:
del pat.refs[name]
for parent in parent_graph[name]:
del self[parent].refs[name]
parents |= parent_graph[name]
trimmed |= empty
if not repeat:
break
empty = {parent for parent in parents if self[parent].is_empty()}
return trimmed
def delete(