Skip to content

perf: Keep large graphs with node views responsive - #102

Merged
philippjfr merged 1 commit into
mainfrom
perf/large-graph-views
Nov 29, 2026
Merged

philippjfr merged 1 commit into
mainfrom
perf/large-graph-views

Conversation

@philippjfr

Copy link
Copy Markdown
Contributor

A tree of hundreds of Node instances, each holding a small Panel card (a one-row table, two buttons and a label), took a long time to appear, and selecting a node got slower as the graph grew. I profiled a 1000-node version on the server and in Chromium and found four costs that grew with the size of the graph:

  • Views built twice. _process_param_change serialized each Node with to_dict(), which calls __panel__() and threw the result away. Internal callers now use the view cache, so each node's view is built once.
  • Page-wide layout passes. Bokeh lays out the whole page from its root whenever any view resizes, and that pass measured every node view. Opening the side editor (one resize per widget) did this for every node. Node views and editors are now their own layout roots, and the page's layout passes skip them.
  • Global figure lookup. Each node called Bokeh.index.find_by_id, which searches every view on the page. That made mounting quadratic in the number of nodes: 4.3s of the 1000-node build. Nodes now skip the lookup when their models contain no figure, and otherwise resolve their own view through the parent.
  • Figure search on every child change. _patch_views searched every node's models for figures whenever any child changed, including on every selection. It was also passed the replaced models instead of the current ones, so on first render Python never patched a figure at all. It now searches each new model once.

With 1000 nodes, views appear in 6.0s instead of 9.7s, and the side editor opens in 0.13 to 0.22s instead of 0.25 to 0.33s. I added unit tests for building views once and searching for figures once, and UI tests for the figure stylesheet and for opening the side editor without laying out node views. Each new test fails on main, except the figure stylesheet test, which covers the new lookup path. The unit and UI suites pass.

Benchmark

1000-node random tree, every node its own Node subclass instance with a pn.pane.DataFrame, two pmu.Buttons and a pn.pane.Markdown, straight labelled edges, editor_mode="side". Headless Chromium, 1600x1000.

main this branch
Server build 4.4s 3.6s
Nodes in DOM 5.6s 4.8s
All views rendered 9.7s 6.0s
Settled (no more style inserts) 14.8s 11.1s
Select node to editor visible 0.25 to 0.33s 0.13 to 0.22s
Drag, worst frame 51ms 23ms
Zoom / pan fps 66 / 91 63 / 91

Server side, a selection went from 690ms to 330ms. The rest is Bokeh's DocumentModelManager.recompute, which walks every model in the document when the new editor's models are added.

What is left
  • Edge labels are SVG <text>, and Chrome re-lays out and re-shapes SVG text whenever an ancestor transform changes. During a zoom every label is invalidated on every step (37,000 invalidations over 37 steps with 999 edges). Without labels, zoom goes from about 60 to 99 fps and the long tasks disappear. text-rendering does not help. Moving labels to HTML through EdgeLabelRenderer would fix it, but it changes the DOM, the label styling props and label click behaviour, so I left it for a separate change.
  • About 5s of the 1000-node build is panel-material-ui: each pmu.Button renders in its own shadow root with its own emotion cache, which inserts 65,000 CSS rules for 2000 buttons.
  • Steady-state pan and zoom are bounded by hit testing and compositing a 137k element DOM.

Node instance views are no longer built on every sync, node views and
editors no longer take part in the page-wide Bokeh layout pass, the
figure stylesheet lookup no longer searches every view on the page, and
node views are searched for figures once instead of on every child
change.
@philippjfr
philippjfr merged commit 390c05f into main Nov 29, 2026
17 checks passed
@philippjfr
philippjfr deleted the perf/large-graph-views branch November 29, 2026 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant