Skip to content

Commit

Permalink
Improve performance of select_children() and select_parents() (#11099)
Browse files Browse the repository at this point in the history
* Improve performance of select_children() and select_parents()

* Add changelog entry.
  • Loading branch information
peterallenwebb authored Dec 5, 2024
1 parent ff6745c commit 1472b86
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changes/unreleased/Under the Hood-20241205-143144.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Under the Hood
body: Improve selection peformance by optimizing the select_children() and select_parents()
functions.
time: 2024-12-05T14:31:44.584216-05:00
custom:
Author: peterallenwebb
Issue: "11099"
20 changes: 16 additions & 4 deletions core/dbt/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,14 @@ def select_children(
while len(selected) > 0 and (max_depth is None or i < max_depth):
next_layer: Set[UniqueId] = set()
for node in selected:
next_layer.update(self.descendants(node, 1))
next_layer = next_layer - children # Avoid re-searching
next_layer.update(
iter(
e[1]
for e in self.graph.out_edges(node)
if e[1] not in children
and self.filter_edges_by_type(e[0], e[1], "parent_test")
)
)
children.update(next_layer)
selected = next_layer
i += 1
Expand All @@ -86,8 +92,14 @@ def select_parents(
while len(selected) > 0 and (max_depth is None or i < max_depth):
next_layer: Set[UniqueId] = set()
for node in selected:
next_layer.update(self.ancestors(node, 1))
next_layer = next_layer - parents # Avoid re-searching
next_layer.update(
iter(
e[0]
for e in self.graph.in_edges(node)
if e[0] not in parents
and self.filter_edges_by_type(e[0], e[1], "parent_test")
)
)
parents.update(next_layer)
selected = next_layer
i += 1
Expand Down

0 comments on commit 1472b86

Please sign in to comment.