Skip to content

Commit

Permalink
Speedup large notebooks: cache cell dependencies in notebook_to_js
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Aug 5, 2024
1 parent 4f8741c commit a973643
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
17 changes: 17 additions & 0 deletions src/analysis/DependencyCache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,25 @@ function update_dependency_cache!(notebook::Notebook, topology::NotebookTopology

if notebook._cached_cell_dependencies_source !== topology
notebook._cached_cell_dependencies_source = topology

for cell in all_cells(topology)
update_dependency_cache!(cell, topology)
end

notebook._cached_cell_dependencies = Dict{UUID,Dict{String,Any}}(
id => Dict{String,Any}(
"cell_id" => cell.cell_id,
"downstream_cells_map" => Dict{String,Vector{UUID}}(
String(s) => cell_id.(r)
for (s, r) in cell.cell_dependencies.downstream_cells_map
),
"upstream_cells_map" => Dict{String,Vector{UUID}}(
String(s) => cell_id.(r)
for (s, r) in cell.cell_dependencies.upstream_cells_map
),
"precedence_heuristic" => cell.cell_dependencies.precedence_heuristic,
)
for (id, cell) in notebook.cells_dict
)
end
end
3 changes: 2 additions & 1 deletion src/notebook/Notebook.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Base.@kwdef mutable struct Notebook
notebook_id::UUID=uuid1()
topology::NotebookTopology
_cached_topological_order::TopologicalOrder
_cached_cell_dependencies::Dict{UUID,Dict{String,Any}}=Dict{UUID,Dict{String,Any}}()
_cached_cell_dependencies_source::Union{Nothing,NotebookTopology}=nothing

# buffer will contain all unfetched updates - must be big enough
Expand Down Expand Up @@ -122,7 +123,7 @@ end
function PlutoDependencyExplorer.topological_order(notebook::Notebook)
cached = notebook._cached_topological_order
if cached === nothing || cached.input_topology !== notebook.topology
topological_order(notebook.topology)
notebook._cached_topological_order = topological_order(notebook.topology)
else
cached
end
Expand Down
15 changes: 1 addition & 14 deletions src/webserver/Dynamic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,6 @@ function notebook_to_js(notebook::Notebook)
"metadata" => cell.metadata,
)
for (id, cell) in notebook.cells_dict),
"cell_dependencies" => Dict{UUID,Dict{String,Any}}(
id => Dict{String,Any}(
"cell_id" => cell.cell_id,
"downstream_cells_map" => Dict{String,Vector{UUID}}(
String(s) => cell_id.(r)
for (s, r) in cell.cell_dependencies.downstream_cells_map
),
"upstream_cells_map" => Dict{String,Vector{UUID}}(
String(s) => cell_id.(r)
for (s, r) in cell.cell_dependencies.upstream_cells_map
),
"precedence_heuristic" => cell.cell_dependencies.precedence_heuristic,
)
for (id, cell) in notebook.cells_dict),
"cell_results" => Dict{UUID,Dict{String,Any}}(
id => Dict{String,Any}(
"cell_id" => cell.cell_id,
Expand Down Expand Up @@ -167,6 +153,7 @@ function notebook_to_js(notebook::Notebook)
)
end,
"status_tree" => Status.tojs(notebook.status_tree),
"cell_dependencies" => notebook._cached_cell_dependencies,
"cell_execution_order" => cell_id.(collect(notebook._cached_topological_order)),
)
end
Expand Down

0 comments on commit a973643

Please sign in to comment.