Skip to content

Commit

Permalink
Speedup large notebooks: fix cache miss in notebook_to_js (#2973)
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp authored Aug 5, 2024
1 parent 34409a7 commit 4f8741c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
15 changes: 12 additions & 3 deletions src/notebook/Notebook.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions src/notebook/saving and loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
2 changes: 1 addition & 1 deletion src/webserver/Dynamic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 4f8741c

Please sign in to comment.