Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

improvement: dataflow perf (#4313)

Myles Scolnick9f9ec487b362
diff --git a/marimo/_runtime/dataflow.py b/marimo/_runtime/dataflow.py
index 9c03962be4..1545e29148 100644
--- a/marimo/_runtime/dataflow.py
+++ b/marimo/_runtime/dataflow.py
@@ -2,6 +2,7 @@
from __future__ import annotations
import threading
+from collections import deque
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Callable, Literal, Optional
@@ -90,34 +91,36 @@ class DirectedGraph:
Only does a local analysis of refs, without taking into consideration
whether refs are defined by other cells.
"""
- children = set()
- for cid, cell in self.cells.items():
- if name not in cell.refs:
- continue
- elif language == "sql" and cell.language == "python":
- # SQL variables don't leak to Python cells, but
- # Python variables do leak to SQL cells
- continue
-
- children.add(cid)
-
- return children
+ if language == "sql":
+ # For SQL, only return SQL cells that reference the name
+ return {
+ cid
+ for cid, cell in self.cells.items()
+ if name in cell.refs and cell.language == "sql"
+ }
+ else:
+ # For Python, return all cells that reference the name
+ return {
+ cid for cid, cell in self.cells.items() if name in cell.refs
+ }
def get_path(self, source: CellId_t, dst: CellId_t) -> list[Edge]:
"""Get a path from `source` to `dst`, if any."""
if source == dst:
return []
- queue: list[tuple[CellId_t, list[Edge]]] = [(source, [])]
- found = set()
+ # deque has O(1) append/pop operation
+ queue: deque[tuple[CellId_t, list[Edge]]] = deque([(source, [])])
+ found = {source} # set has O(1) lookups
+
while queue:
- node, path = queue.pop(0)
- found.add(node)
+ node, path = queue.popleft() # O(1) operation
for cid in self.children[node]:
if cid not in found:
next_path = path + [(node, cid)]
if cid == dst:
return next_path
+ found.add(cid)
queue.append((cid, next_path))
return []
@@ -384,19 +387,22 @@ class DirectedGraph:
"""
# TODO: Consider caching on the graph level and updating on register /
# delete
- processed = set()
- queue = set(refs & self.definitions.keys())
+ processed: set[Name] = set()
+ queue: set[Name] = refs & self.definitions.keys()
predicate = predicate or (lambda *_: True)
while queue:
# Should ideally be one cell per ref, but for completion, stay
# agnostic to potenital cycles.
- cells = set().union(*[self.definitions[ref] for ref in queue])
+ cells = {
+ cell_id
+ for ref in queue
+ for cell_id in self.definitions.get(ref, set())
+ }
+
for cell_id in cells:
data = self.cells[cell_id].variable_data
- variables = set(data.keys())
- # intersection of variables and queue
- newly_processed = variables & queue
+ newly_processed = set(data.keys()) & queue
processed.update(newly_processed)
queue.difference_update(newly_processed)
for variable in newly_processed:
@@ -408,11 +414,13 @@ class DirectedGraph:
queue.update(to_process & self.definitions.keys())
# Private variables referenced by public functions
# have to be included.
- for maybe_private in (
- to_process - self.definitions.keys()
- ):
- if is_mangled_local(maybe_private, cell_id):
- processed.add(maybe_private)
+ processed.update(
+ maybe_private
+ for maybe_private in (
+ to_process - self.definitions.keys()
+ )
+ if is_mangled_local(maybe_private, cell_id)
+ )
if inclusive:
return processed | refs
@@ -441,29 +449,32 @@ def transitive_closure(
If predicate, only cells satisfying predicate(cell) are included; applied
after the relatives are computed
"""
- seen = set()
- cells = set()
- queue = list(cell_ids)
+
+ result: set[CellId_t] = cell_ids.copy() if inclusive else set()
+ seen: set[CellId_t] = cell_ids.copy()
+ queue: deque[CellId_t] = deque(cell_ids)
predicate = predicate or (lambda _: True)
def _relatives(cid: CellId_t) -> set[CellId_t]:
if relatives is None:
return graph.children[cid] if children else graph.parents[cid]
-
return relatives(graph, cid, children)
while queue:
- cid = queue.pop(0)
- seen.add(cid)
- cell = graph.cells[cid]
- if inclusive and predicate(cell):
- cells.add(cid)
- elif cid not in cell_ids and predicate(cell):
- cells.add(cid)
- for relative in _relatives(cid):
- if relative not in seen:
+ cid = queue.popleft() # O(1) operation
+
+ relatives_set = _relatives(cid)
+ new_relatives = relatives_set - seen
+
+ if new_relatives:
+ # Add new relatives to queue and result if they pass predicate
+ for relative in new_relatives:
+ if predicate(graph.cells[relative]):
+ result.add(relative)
+ seen.add(relative)
queue.append(relative)
- return cells
+
+ return result
def induced_subgraph(
@@ -473,8 +484,8 @@ def induced_subgraph(
Represents the subgraph induced by `cell_ids`.
"""
- parents = {}
- children = {}
+ parents: dict[CellId_t, set[CellId_t]] = {}
+ children: dict[CellId_t, set[CellId_t]] = {}
for cid in cell_ids:
parents[cid] = set(p for p in graph.parents[cid] if p in cell_ids)
children[cid] = set(c for c in graph.children[cid] if c in cell_ids)
@@ -500,29 +511,31 @@ def topological_sort(
When multiple cells have the same parents (including no parents), the tie is broken by
registration order - cells registered earlier are processed first.
"""
- from heapq import heappop, heappush
+ from heapq import heapify, heappop, heappush
+
+ # Use a list for O(1) lookup of registration order
+ registration_order = list(graph.cells.keys())
+ top_down_keys = {key: idx for idx, key in enumerate(registration_order)}
+ # Build adjacency lists and in-degree counts
parents, children = induced_subgraph(graph, cell_ids)
- # Use registration order as tiebreaker
- top_down_keys = {
- key: index for index, key in enumerate(graph.cells.keys())
- }
+ in_degree = {cid: len(parents[cid]) for cid in cell_ids}
- # Initialize heap with roots (nodes with no parents)
- heap: list[tuple[int, CellId_t]] = []
- for cid in cell_ids:
- if not parents[cid]:
- # Use tuple with registration order as first element for heap ordering
- heappush(heap, (top_down_keys[cid], cid))
+ # Initialize heap with roots
+ heap = [
+ (top_down_keys[cid], cid) for cid in cell_ids if in_degree[cid] == 0
+ ]
+ heapify(heap)
sorted_cell_ids: list[CellId_t] = []
while heap:
_, cid = heappop(heap)
sorted_cell_ids.append(cid)
+ # Process children
for child in children[cid]:
- parents[child].remove(cid)
- if not parents[child]:
+ in_degree[child] -= 1
+ if in_degree[child] == 0:
heappush(heap, (top_down_keys[child], child))
return sorted_cell_ids
@@ -543,26 +556,25 @@ def import_block_relatives(
# We prune definitions that have already been imported from the set of
# definitions used to find the descendants of this cell.
unimported_defs = cell.defs - cell.import_workspace.imported_defs
- children_ids = set().union(
- *[
- graph.get_referring_cells(name, language="python")
- for name in unimported_defs
- ]
- )
+
+ children_ids = {
+ child_id
+ for name in unimported_defs
+ for child_id in graph.get_referring_cells(name, language="python")
+ }
# If children haven't been executed, then still use imported defs;
# handle an edge case when an import cell is interrupted by an
# exception or user interrupt, so that a module is imported but the
# cell's children haven't run.
- for name in cell.import_workspace.imported_defs:
- for child_id in graph.get_referring_cells(name, language="python"):
- if graph.cells[child_id].run_result_status in (
- "interrupted",
- "cancelled",
- "marimo-error",
- None,
- ):
- children_ids.add(child_id)
+ if cell.import_workspace.imported_defs:
+ interrupted_states = {"interrupted", "cancelled", "marimo-error", None}
+ children_ids.update(
+ child_id
+ for name in cell.import_workspace.imported_defs
+ for child_id in graph.get_referring_cells(name, language="python")
+ if graph.cells[child_id].run_result_status in interrupted_states
+ )
return children_ids
diff --git a/tests/_runtime/test_dataflow.py b/tests/_runtime/test_dataflow.py
index baa802f422..80e1727d74 100644
--- a/tests/_runtime/test_dataflow.py
+++ b/tests/_runtime/test_dataflow.py
@@ -109,6 +109,109 @@ def test_graph_closure_predicate() -> None:
) == set(["0", "2"])
+def test_graph_closure_inclusive() -> None:
+ graph = dataflow.DirectedGraph()
+ code = "x = 0"
+ first_cell = parse_cell(code)
+ graph.register_cell("0", first_cell)
+
+ code = "y = x"
+ second_cell = parse_cell(code)
+ graph.register_cell("1", second_cell)
+
+ code = "z = y"
+ third_cell = parse_cell(code)
+ graph.register_cell("2", third_cell)
+
+ # 0 --> 1 --> 2
+ assert graph.cells == {"0": first_cell, "1": second_cell, "2": third_cell}
+ assert graph.parents == {"0": set(), "1": set(["0"]), "2": set(["1"])}
+ assert graph.children == {"0": set(["1"]), "1": set(["2"]), "2": set()}
+
+ # Test inclusive=True (default)
+ assert dataflow.transitive_closure(graph, cell_ids=set(["1"])) == set(
+ ["1", "2"]
+ )
+
+ # Test inclusive=False
+ assert dataflow.transitive_closure(
+ graph, cell_ids=set(["1"]), inclusive=False
+ ) == set(["2"])
+
+ # Test with multiple starting cells
+ assert dataflow.transitive_closure(graph, cell_ids=set(["0", "1"])) == set(
+ ["0", "1", "2"]
+ )
+
+ # Test ancestors (children=False)
+ assert dataflow.transitive_closure(
+ graph, cell_ids=set(["2"]), children=False
+ ) == set(["0", "1", "2"])
+
+ # Test ancestors with inclusive=False
+ assert dataflow.transitive_closure(
+ graph, cell_ids=set(["2"]), children=False, inclusive=False
+ ) == set(["0", "1"])
+
+
+def test_graph_closure_predicate_with_inclusive_false() -> None:
+ graph = dataflow.DirectedGraph()
+ code = "x = 0"
+ first_cell = parse_cell(code)
+ graph.register_cell("0", first_cell)
+
+ code = "y = x"
+ second_cell = parse_cell(code)
+ graph.register_cell("1", second_cell)
+
+ code = "z = y"
+ third_cell = parse_cell(code)
+ graph.register_cell("2", third_cell)
+
+ result = dataflow.transitive_closure(
+ graph, cell_ids=set(["0"]), inclusive=False
+ )
+ assert result == set(["1", "2"])
+
+ result = dataflow.transitive_closure(
+ graph, cell_ids=set(["1"]), inclusive=False
+ )
+ assert result == set(["2"])
+
+ result = dataflow.transitive_closure(
+ graph, cell_ids=set(["0", "1"]), inclusive=False
+ )
+ assert result == set(["2"])
+
+ result = dataflow.transitive_closure(
+ graph, cell_ids=set(["0", "1", "2"]), inclusive=False
+ )
+ assert result == set()
+
+
+def test_graph_closure_empty() -> None:
+ graph = dataflow.DirectedGraph()
+
+ # Test with empty graph
+ assert dataflow.transitive_closure(graph, cell_ids=set()) == set()
+
+ # Add a single cell with no connections
+ code = "x = 0"
+ first_cell = parse_cell(code)
+ graph.register_cell("0", first_cell)
+
+ # Test with single cell, no connections
+ assert dataflow.transitive_closure(graph, cell_ids=set(["0"])) == set(
+ ["0"]
+ )
+ assert (
+ dataflow.transitive_closure(
+ graph, cell_ids=set(["0"]), inclusive=False
+ )
+ == set()
+ )
+
+
def test_graph_redefine() -> None:
graph = dataflow.DirectedGraph()
code = "x = 0"