From 4f8741c1547bf2d63b761a4c416b089f3a3e2b7a Mon Sep 17 00:00:00 2001 From: Fons van der Plas Date: Mon, 5 Aug 2024 12:35:39 +0200 Subject: [PATCH] Speedup large notebooks: fix cache miss in `notebook_to_js` (#2973) --- src/notebook/Notebook.jl | 15 ++++++++++++--- src/notebook/saving and loading.jl | 8 +++++--- src/webserver/Dynamic.jl | 2 +- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/notebook/Notebook.jl b/src/notebook/Notebook.jl index 858081d9c3..d244252aa6 100644 --- a/src/notebook/Notebook.jl +++ b/src/notebook/Notebook.jl @@ -31,7 +31,7 @@ Base.@kwdef mutable struct Notebook path::String notebook_id::UUID=uuid1() topology::NotebookTopology - _cached_topological_order::Union{Nothing,TopologicalOrder}=nothing + _cached_topological_order::TopologicalOrder _cached_cell_dependencies_source::Union{Nothing,NotebookTopology}=nothing # buffer will contain all unfetched updates - must be big enough @@ -96,10 +96,12 @@ function Notebook(cells::Vector{Cell}, @nospecialize(path::AbstractString), note (cell.cell_id, cell) end) cell_order=map(x -> x.cell_id, cells) + topology = _initial_topology(cells_dict, cell_order) Notebook(; cells_dict, cell_order, - topology=_initial_topology(cells_dict, cell_order), + topology, + _cached_topological_order=topological_order(topology), path, notebook_id ) @@ -117,7 +119,14 @@ function Base.getproperty(notebook::Notebook, property::Symbol) end end -PlutoDependencyExplorer.topological_order(notebook::Notebook) = topological_order(notebook.topology) +function PlutoDependencyExplorer.topological_order(notebook::Notebook) + cached = notebook._cached_topological_order + if cached === nothing || cached.input_topology !== notebook.topology + topological_order(notebook.topology) + else + cached + end +end function PlutoDependencyExplorer.where_referenced(notebook::Notebook, topology::NotebookTopology, something) # can't use @deprecate on an overload diff --git a/src/notebook/saving and loading.jl b/src/notebook/saving and loading.jl index 0e24c8ca9d..80831ebbb2 100644 --- a/src/notebook/saving and loading.jl +++ b/src/notebook/saving and loading.jl @@ -319,13 +319,15 @@ function load_notebook_nobackup(@nospecialize(io::IO), @nospecialize(path::Abstr appeared_cells_dict = filter(collected_cells) do (k, v) k ∈ appeared_order end + topology = _initial_topology(appeared_cells_dict, appeared_order) Notebook(; cells_dict=appeared_cells_dict, cell_order=appeared_order, - topology=_initial_topology(appeared_cells_dict, appeared_order), - path=path, - nbpkg_ctx=nbpkg_ctx, + topology, + _cached_topological_order=topological_order(topology), + path, + nbpkg_ctx, nbpkg_installed_versions_cache=nbpkg_cache(nbpkg_ctx), metadata=notebook_metadata, ) diff --git a/src/webserver/Dynamic.jl b/src/webserver/Dynamic.jl index 4bb2c72e25..fd6351b523 100644 --- a/src/webserver/Dynamic.jl +++ b/src/webserver/Dynamic.jl @@ -167,7 +167,7 @@ function notebook_to_js(notebook::Notebook) ) end, "status_tree" => Status.tojs(notebook.status_tree), - "cell_execution_order" => cell_id.(collect(topological_order(notebook))), + "cell_execution_order" => cell_id.(collect(notebook._cached_topological_order)), ) end