Skip to content

Commit 3d52904

Browse files
authored
perf(common): reduce the average recusrion depth in _flatten_collections (ibis-project#8709)
1 parent 6639163 commit 3d52904

2 files changed

Lines changed: 8 additions & 14 deletions

File tree

ibis/common/graph.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,18 @@ def _flatten_collections(node: Any) -> Iterator[N]:
5656
>>> c = MyNode(2, "c", (a, b))
5757
>>> d = MyNode(1, "d", (c,))
5858
>>>
59-
>>> assert list(_flatten_collections(a)) == [a]
6059
>>> assert list(_flatten_collections((c,))) == [c]
6160
>>> assert list(_flatten_collections([a, b, (c, a)])) == [a, b, c, a]
61+
>>> assert list(_flatten_collections([{"b": b, "a": a}])) == [b, a]
6262
6363
"""
64-
if isinstance(node, Node):
65-
yield node
66-
elif isinstance(node, (tuple, list)):
67-
for item in node:
64+
for item in node:
65+
if isinstance(item, Node):
66+
yield item
67+
elif isinstance(item, (tuple, list)):
6868
yield from _flatten_collections(item)
69-
elif isinstance(node, (dict, frozendict)):
70-
for value in node.values():
71-
yield from _flatten_collections(value)
69+
elif isinstance(item, (dict, frozendict)):
70+
yield from _flatten_collections(item.values())
7271

7372

7473
def _recursive_lookup(obj: Any, dct: dict) -> Any:

ibis/common/tests/test_graph.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,7 @@ def test_flatten_collections():
288288
assert list(result) == [A, B, C, D, E]
289289

290290
result = _flatten_collections(
291-
{
292-
"a": 0.0,
293-
"b": A,
294-
"c": (MyMapping(d=B, e=3), frozendict(f=C)),
295-
"d": [5, "6", {"e": (D, 8.9)}],
296-
}
291+
[0.0, A, (MyMapping(d=B, e=3), frozendict(f=C)), [5, "6", {"e": (D, 8.9)}]]
297292
)
298293
assert list(result) == [A, C, D]
299294

0 commit comments

Comments
 (0)