Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedup large notebooks: cache cell dependencies in notebook_to_js #2976

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading