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

Introduce branching sections #449

Merged
merged 6 commits into from Jul 15, 2021
Merged

Introduce branching sections #449

merged 6 commits into from Jul 15, 2021

Conversation

jonatanklosko
Copy link
Member

Closes #21.

Allows for having sections that branch out of a previous regular section. Such branching section still has access to all bindings defined by the parent section, but runs concurrently and generally has no impact on further sections (for instance "stale" invalidation is limited to that single section).

Intro

image

Demo

branching.mp4

Details

From Livbook.Session:

## Evaluation

All regular sections are evaluated in the same process
(the :main_flow evaluation container). On the other hand,
each branching section is evaluated in its own process
and thus runs concurrently.

### Implementation considerations

In practice, every evaluation container is a `Livebook.Evaluator`
process, so we have one such process for the main flow and one
for each branching section. Since a branching section inherits
the evaluation context from the parent section, the last context
needs to be copied from the main flow evaluator to the branching
section evaluator. The latter synchronously asks the former for
that context using `Livebook.Evaluator.fetch_evaluation_context/3`.
Consequently, in order to evaluate the first cell in a branching
section, the main flow needs to be free of work, otherwise we wait.
This assumptions are mirrored in by `Livebook.Session.Data` when
determining cells for evaluation.

Note: the context could be copied asynchronously if evaluator
kept the contexts in its process dictionary, however the other
evaluator could only read the whole process dictionary, thus
allocating a lot of memory unnecessarily, which would be unacceptable
for large data. By making a synchronous request to the evalutor
for a single specific evaluation context we make sure to copy
as little memory as necessary.

case Evaluator.fetch_evaluation_context(
source_evaluator,
source_evaluation_ref,
cached_id: state.initial_context.id
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each context now has a unique id. If we keep reevaluating the first cell in a branching section, we will transfer the context only the first time.

By default all cells are included.
"""
@spec cell_dependency_graph(t()) :: Graph.t(Cell.id())
def cell_dependency_graph(notebook, opts \\ []) do
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was fun :shipit:

Copy link
Contributor

@josevalim josevalim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely beautiful! Just some minor comments and ship it!

@jonatanklosko jonatanklosko merged commit d55c4a1 into main Jul 15, 2021
@jonatanklosko jonatanklosko deleted the jk-branching branch July 15, 2021 16:19
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.

Support branching sections
2 participants