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

Commit 4ca5233

Browse files
committed
refactor: implement iterative top-down traversal
1 parent 43353e2 commit 4ca5233

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

bigframes/core/bigframe_node.py

Lines changed: 25 additions & 5 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[int, 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-
338-
return recursive_transform(self)
337+
while stack:
338+
node, t_node = stack[-1]
339+
node_id = id(node)
340+
341+
if t_node is None:
342+
if node_id in results:
343+
stack.pop()
344+
continue
345+
t_node = transform(node)
346+
stack[-1] = (node, t_node)
347+
348+
all_done = True
349+
for child in reversed(t_node.child_nodes):
350+
if id(child) not in results:
351+
stack.append((child, None))
352+
all_done = False
353+
354+
if all_done:
355+
results[node_id] = t_node.transform_children(lambda x: results[id(x)])
356+
stack.pop()
357+
358+
return results[id(self)]
339359

340360
def bottom_up(
341361
self: BigFrameNode,

0 commit comments

Comments
 (0)