Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit a000425

Browse files
authored
refactor: implement iterative top-down traversal (#2508)
1 parent fe94910 commit a000425

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

bigframes/core/bigframe_node.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,32 @@ def top_down(
330330
"""
331331
Perform a top-down transformation of the BigFrameNode tree.
332332
"""
333+
results: Dict[BigFrameNode, BigFrameNode] = {}
334+
# Each stack entry is (node, t_node). t_node is None until transform(node) is called.
335+
stack: list[tuple[BigFrameNode, typing.Optional[BigFrameNode]]] = [(self, None)]
333336

334-
@functools.cache
335-
def recursive_transform(node: BigFrameNode) -> BigFrameNode:
336-
return transform(node).transform_children(recursive_transform)
337+
while stack:
338+
node, t_node = stack[-1]
339+
340+
if t_node is None:
341+
if node in results:
342+
stack.pop()
343+
continue
344+
t_node = transform(node)
345+
stack[-1] = (node, t_node)
346+
347+
all_done = True
348+
for child in reversed(t_node.child_nodes):
349+
if child not in results:
350+
stack.append((child, None))
351+
all_done = False
352+
break
353+
354+
if all_done:
355+
results[node] = t_node.transform_children(lambda x: results[x])
356+
stack.pop()
337357

338-
return recursive_transform(self)
358+
return results[self]
339359

340360
def bottom_up(
341361
self: BigFrameNode,

0 commit comments

Comments
 (0)