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.

perf: better datastructures for kernel Runner (#8251)

Myles Scolnickc3f6e319605b
diff --git a/marimo/_runtime/app/script_runner.py b/marimo/_runtime/app/script_runner.py
index 37f2b35c05..f6551cd10f 100644
--- a/marimo/_runtime/app/script_runner.py
+++ b/marimo/_runtime/app/script_runner.py
@@ -2,6 +2,7 @@
from __future__ import annotations
import asyncio
+from collections import deque
from typing import TYPE_CHECKING, Any, Callable, Optional
from marimo._ast.names import SETUP_CELL_NAME
@@ -56,12 +57,12 @@ class AppScriptRunner:
excluded=CellId_t(SETUP_CELL_NAME),
)
- self.cells_to_run = [
+ self.cells_to_run: deque[CellId_t] = deque(
cid
for cid in pruned_execution_order
if app.cell_manager.cell_data_at(cid).cell is not None
and not self.app.graph.is_disabled(cid)
- ]
+ )
self._executor = get_executor(ExecutionConfig())
def _cancel(self, cell_id: CellId_t) -> None:
@@ -90,7 +91,7 @@ class AppScriptRunner:
outputs: dict[CellId_t, Any] = {}
while self.cells_to_run:
- cid = self.cells_to_run.pop(0)
+ cid = self.cells_to_run.popleft()
if cid in self.cells_cancelled:
continue
# Set up has already run in this case.
@@ -133,7 +134,7 @@ class AppScriptRunner:
outputs: dict[CellId_t, Any] = {}
while self.cells_to_run:
- cid = self.cells_to_run.pop(0)
+ cid = self.cells_to_run.popleft()
if cid in self.cells_cancelled:
continue
diff --git a/marimo/_runtime/dataflow/topology.py b/marimo/_runtime/dataflow/topology.py
index 6e3f797c06..1aa7862484 100644
--- a/marimo/_runtime/dataflow/topology.py
+++ b/marimo/_runtime/dataflow/topology.py
@@ -103,19 +103,17 @@ class MutableGraphTopology(GraphTopology):
if cell_id not in self.cells:
raise ValueError(f"Cell {cell_id} not found")
- # Remove from cells
+ # Remove this node from its parents' children lists
+ for parent_id in self._parents[cell_id]:
+ self._children[parent_id].discard(cell_id)
+ # Remove this node from its children's parent lists
+ for child_id in self._children[cell_id]:
+ self._parents[child_id].discard(cell_id)
+
del self._cells[cell_id]
del self._children[cell_id]
del self._parents[cell_id]
- # Remove from other nodes' parent/child lists
- for elems in self.parents.values():
- if cell_id in elems:
- elems.remove(cell_id)
- for elems in self.children.values():
- if cell_id in elems:
- elems.remove(cell_id)
-
def add_edge(self, parent: CellId_t, child: CellId_t) -> None:
"""Add an edge from parent to child."""
self.children[parent].add(child)
diff --git a/marimo/_runtime/runner/cell_runner.py b/marimo/_runtime/runner/cell_runner.py
index 51ae67e5f2..17c97cd02a 100644
--- a/marimo/_runtime/runner/cell_runner.py
+++ b/marimo/_runtime/runner/cell_runner.py
@@ -8,6 +8,7 @@ import io
import signal
import threading
import traceback
+from collections import deque
from dataclasses import dataclass
from pathlib import Path
from types import TracebackType
@@ -39,6 +40,7 @@ from marimo._runtime.executor import (
)
from marimo._runtime.marimo_pdb import MarimoPdb
from marimo._runtime.runner.hook_context import (
+ CancelledCells,
ExceptionOrError,
ExecutionContextManager,
)
@@ -154,15 +156,17 @@ class Runner:
# so that they can be transitioned out of error if a future
# run request repairs the graph
self.roots = roots
- self.cells_to_run: list[CellId_t]
-
- self.cells_to_run = Runner.compute_cells_to_run(
- self.graph, self.roots, self.excluded_cells, self.execution_mode
+ self.cells_to_run: deque[CellId_t] = deque(
+ Runner.compute_cells_to_run(
+ self.graph,
+ self.roots,
+ self.excluded_cells,
+ self.execution_mode,
+ )
)
- # map from a cell that was cancelled to its descendants that have
- # not yet run:
- self.cells_cancelled: dict[CellId_t, set[CellId_t]] = {}
+ # tracks cancelled cells: raising cell -> descendants, with O(1) lookup
+ self.cancelled_cells = CancelledCells()
# whether the runner has been interrupted
self.interrupted = False
# mapping from cell_id to exception it raised
@@ -265,19 +269,18 @@ class Runner:
def cancel(self, cell_id: CellId_t) -> None:
"""Mark a cell (and its descendants) as cancelled."""
- self.cells_cancelled[cell_id] = set(
+ descendants = set(
cid
for cid in dataflow.transitive_closure(self.graph, set([cell_id]))
if cid in self.cells_to_run
)
- for cid in self.cells_cancelled[cell_id]:
+ self.cancelled_cells.add(cell_id, descendants)
+ for cid in descendants:
self.graph.cells[cid].set_run_result_status("cancelled")
def cancelled(self, cell_id: CellId_t) -> bool:
"""Return whether a cell has been cancelled."""
- return any(
- cell_id in cancelled for cancelled in self.cells_cancelled.values()
- )
+ return cell_id in self.cancelled_cells
def pending(self) -> bool:
"""Whether there are more cells to run."""
@@ -360,7 +363,7 @@ class Runner:
def pop_cell(self) -> CellId_t:
"""Get the next cell to run."""
- return self.cells_to_run.pop(0)
+ return self.cells_to_run.popleft()
def _run_result_from_exception(
self,
@@ -682,12 +685,20 @@ class Runner:
graph=self.graph,
execution_mode=self.execution_mode,
)
+ all_temporaries: frozenset[str] = (
+ frozenset().union(
+ *(cell.temporaries for cell in self.graph.cells.values())
+ )
+ if self.graph.cells
+ else frozenset()
+ )
post_exec_ctx = PostExecutionHookContext(
graph=self.graph,
glbls=self.glbls,
execution_context=self.execution_context,
exceptions=self.exceptions,
- cells_cancelled=self.cells_cancelled,
+ cancelled_cells=self.cancelled_cells,
+ all_temporaries=all_temporaries,
should_broadcast_data=_should_broadcast_data(),
)
@@ -747,7 +758,7 @@ class Runner:
graph=self.graph,
cells_to_run=self.cells_to_run,
interrupted=self.interrupted,
- cells_cancelled=self.cells_cancelled,
+ cancelled_cells=self.cancelled_cells,
exceptions=self.exceptions,
)
LOGGER.debug("Running on_finish hooks")
diff --git a/marimo/_runtime/runner/hook_context.py b/marimo/_runtime/runner/hook_context.py
index 70512ec21c..bf1465c5f9 100644
--- a/marimo/_runtime/runner/hook_context.py
+++ b/marimo/_runtime/runner/hook_context.py
@@ -9,7 +9,7 @@ from marimo._messaging.errors import Error
from marimo._types.ids import CellId_t
if TYPE_CHECKING:
- from collections.abc import Mapping, Sequence
+ from collections.abc import Iterator, Mapping, Sequence
from contextlib import AbstractContextManager
from marimo._runtime.context.types import ExecutionContext
@@ -22,6 +22,38 @@ ExecutionContextManager: TypeAlias = Callable[
]
+class CancelledCells:
+ """Tracks cancelled cells with both structured and flat views.
+
+ Maintains a mapping from raising cell -> cancelled descendants,
+ and a flat set for O(1) membership checks.
+ """
+
+ def __init__(self) -> None:
+ self._by_raising_cell: dict[CellId_t, set[CellId_t]] = {}
+ self._all: set[CellId_t] = set()
+
+ def add(self, raising_cell: CellId_t, descendants: set[CellId_t]) -> None:
+ """Record that raising_cell caused descendants to be cancelled."""
+ self._by_raising_cell[raising_cell] = descendants
+ self._all.update(descendants)
+
+ def __contains__(self, cell_id: object) -> bool:
+ """O(1) check if a cell has been cancelled."""
+ return cell_id in self._all
+
+ def __iter__(self) -> Iterator[CellId_t]:
+ """Iterate over raising cells."""
+ return iter(self._by_raising_cell)
+
+ def __getitem__(self, raising_cell: CellId_t) -> set[CellId_t]:
+ """Get descendants cancelled by a specific raising cell."""
+ return self._by_raising_cell[raising_cell]
+
+ def __bool__(self) -> bool:
+ return bool(self._by_raising_cell)
+
+
@dataclass(frozen=True)
class PreparationHookContext:
graph: DirectedGraph
@@ -42,22 +74,18 @@ class PostExecutionHookContext:
execution_context: ExecutionContextManager | None
# Dict, because errors get mutated (formatted) by hooks.
exceptions: dict[CellId_t, ExceptionOrError]
- cells_cancelled: Mapping[CellId_t, set[CellId_t]]
+ cancelled_cells: CancelledCells
+ # Pre-computed union of all cell temporaries
+ all_temporaries: frozenset[str]
# Whether data (variables, datasets, etc.) should be broadcast
# to the frontend. Computed once per run to avoid repeated checks.
should_broadcast_data: bool = False
- def cancelled(self, cell_id: CellId_t) -> bool:
- """Check if a cell was cancelled."""
- return any(
- cell_id in cancelled for cancelled in self.cells_cancelled.values()
- )
-
@dataclass(frozen=True)
class OnFinishHookContext:
graph: DirectedGraph
cells_to_run: Sequence[CellId_t]
interrupted: bool
- cells_cancelled: Mapping[CellId_t, set[CellId_t]]
+ cancelled_cells: CancelledCells
exceptions: Mapping[CellId_t, ExceptionOrError]
diff --git a/marimo/_runtime/runner/hooks_on_finish.py b/marimo/_runtime/runner/hooks_on_finish.py
index bca01c6f77..bc9ee9c2be 100644
--- a/marimo/_runtime/runner/hooks_on_finish.py
+++ b/marimo/_runtime/runner/hooks_on_finish.py
@@ -38,8 +38,8 @@ def _send_interrupt_errors(ctx: OnFinishHookContext) -> None:
@kernel_tracer.start_as_current_span("send_cancellation_errors")
def _send_cancellation_errors(ctx: OnFinishHookContext) -> None:
- for raising_cell in ctx.cells_cancelled:
- for cid in ctx.cells_cancelled[raising_cell]:
+ for raising_cell in ctx.cancelled_cells:
+ for cid in ctx.cancelled_cells[raising_cell]:
# `cid` was not run
cell = ctx.graph.cells[cid]
if cell.runtime_state != "idle":
diff --git a/marimo/_runtime/runner/hooks_post_execution.py b/marimo/_runtime/runner/hooks_post_execution.py
index eb2ad7863a..c555394b03 100644
--- a/marimo/_runtime/runner/hooks_post_execution.py
+++ b/marimo/_runtime/runner/hooks_post_execution.py
@@ -90,7 +90,7 @@ def _set_run_result_status(
) -> None:
if isinstance(run_result.exception, MarimoInterruptionError):
cell.set_run_result_status("interrupted")
- elif ctx.cancelled(cell.cell_id):
+ elif cell.cell_id in ctx.cancelled_cells:
cell.set_run_result_status("cancelled")
elif run_result.exception is not None:
cell.set_run_result_status(
@@ -246,11 +246,9 @@ def _store_state_reference(
# Associate state variables with variable names
runtime_ctx = get_context()
runtime_ctx.state_registry.register_scope(ctx.glbls, defs=cell.defs)
- privates = set().union(
- *[cell.temporaries for cell in runtime_ctx.graph.cells.values()]
- )
+ # Use pre-computed all_temporaries
runtime_ctx.state_registry.retain_active_states(
- set(ctx.graph.definitions.keys()) | privates
+ set(ctx.graph.definitions.keys()) | ctx.all_temporaries
)
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index 19d0eda9f1..cb4c9f9594 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -1471,6 +1471,7 @@ class Kernel:
# by object ID (via is operator)
if ref in self.globals and self.globals[ref] is state:
cells_with_stale_state.add(cid)
+ break
self.graph.set_stale(cells_with_stale_state, prune_imports=True)
if not self.lazy():
self._execute_stale_cells_callback()
diff --git a/tests/_runtime/runner/test_cancelled_cells.py b/tests/_runtime/runner/test_cancelled_cells.py
new file mode 100644
index 0000000000..e3d8fc5861
--- /dev/null
+++ b/tests/_runtime/runner/test_cancelled_cells.py
@@ -0,0 +1,70 @@
+# Copyright 2026 Marimo. All rights reserved.
+from __future__ import annotations
+
+import pytest
+
+from marimo._runtime.runner.hook_context import CancelledCells
+from marimo._types.ids import CellId_t
+
+
+class TestCancelledCells:
+ def test_empty(self) -> None:
+ cc = CancelledCells()
+ assert not cc
+ assert CellId_t("a") not in cc
+ assert list(cc) == []
+
+ def test_add_and_contains(self) -> None:
+ cc = CancelledCells()
+ cc.add(
+ CellId_t("raiser"),
+ {CellId_t("child1"), CellId_t("child2")},
+ )
+ assert cc
+ assert CellId_t("child1") in cc
+ assert CellId_t("child2") in cc
+ assert CellId_t("other") not in cc
+
+ def test_contains_checks_descendants_not_raisers(self) -> None:
+ """The `in` operator checks the flat descendant set, not just raisers."""
+ cc = CancelledCells()
+ cc.add(CellId_t("raiser"), {CellId_t("raiser"), CellId_t("child")})
+ # Both raiser and descendant should be found
+ assert CellId_t("raiser") in cc
+ assert CellId_t("child") in cc
+
+ def test_iter_yields_raising_cells(self) -> None:
+ cc = CancelledCells()
+ cc.add(CellId_t("r1"), {CellId_t("a")})
+ cc.add(CellId_t("r2"), {CellId_t("b")})
+ assert set(cc) == {CellId_t("r1"), CellId_t("r2")}
+
+ def test_getitem(self) -> None:
+ cc = CancelledCells()
+ descendants = {CellId_t("x"), CellId_t("y")}
+ cc.add(CellId_t("r"), descendants)
+ assert cc[CellId_t("r")] == descendants
+
+ def test_getitem_missing_raises(self) -> None:
+ cc = CancelledCells()
+ with pytest.raises(KeyError):
+ cc[CellId_t("missing")]
+
+ def test_multiple_raisers_flat_set_merges(self) -> None:
+ """Flat set is the union of all descendants across raisers."""
+ cc = CancelledCells()
+ cc.add(CellId_t("r1"), {CellId_t("a"), CellId_t("b")})
+ cc.add(CellId_t("r2"), {CellId_t("b"), CellId_t("c")})
+ assert CellId_t("a") in cc
+ assert CellId_t("b") in cc
+ assert CellId_t("c") in cc
+ assert CellId_t("d") not in cc
+
+ def test_shared_reference_semantics(self) -> None:
+ """Mutations after passing to a frozen dataclass are visible."""
+ cc = CancelledCells()
+ # Simulate: PostExecutionHookContext holds a reference,
+ # then Runner.cancel() adds entries after construction.
+ assert CellId_t("x") not in cc
+ cc.add(CellId_t("r"), {CellId_t("x")})
+ assert CellId_t("x") in cc
diff --git a/tests/_runtime/test_topology.py b/tests/_runtime/test_topology.py
index ed46d7dbea..9db82660c4 100644
--- a/tests/_runtime/test_topology.py
+++ b/tests/_runtime/test_topology.py
@@ -102,6 +102,39 @@ class TestMutableGraphTopology:
assert "cell_2" not in graph.children["cell_1"]
assert "cell_2" not in graph.parents["cell_3"]
+ def test_remove_hub_node_cleans_all_neighbors(self) -> None:
+ """Removing a hub node updates all its parents and children."""
+ graph = MutableGraphTopology()
+ cells = {f"cell_{i}": parse_cell(f"x{i} = {i}") for i in range(1, 6)}
+ for cid, cell in cells.items():
+ graph.add_node(cid, cell)
+
+ # cell_3 is a hub: parents=[cell_1, cell_2], children=[cell_4, cell_5]
+ graph.add_edge("cell_1", "cell_3")
+ graph.add_edge("cell_2", "cell_3")
+ graph.add_edge("cell_3", "cell_4")
+ graph.add_edge("cell_3", "cell_5")
+ # unrelated edge
+ graph.add_edge("cell_1", "cell_2")
+
+ graph.remove_node("cell_3")
+
+ # cell_3 is fully removed
+ assert "cell_3" not in graph.cells
+ assert "cell_3" not in graph.children
+ assert "cell_3" not in graph.parents
+
+ # parents no longer list cell_3 as a child
+ assert "cell_3" not in graph.children["cell_1"]
+ assert "cell_3" not in graph.children["cell_2"]
+ # children no longer list cell_3 as a parent
+ assert "cell_3" not in graph.parents["cell_4"]
+ assert "cell_3" not in graph.parents["cell_5"]
+
+ # unrelated edge is untouched
+ assert "cell_2" in graph.children["cell_1"]
+ assert "cell_1" in graph.parents["cell_2"]
+
def test_add_edge(self) -> None:
"""Test adding an edge between two nodes."""
graph = MutableGraphTopology()