Repository navigation
Short-circuit Delta.diff/2 for large payloads - #3214
Closed
simonyang08 wants to merge 1 commit into
Closed
simonyang08 wants to merge 1 commit into
simonyang08 wants to merge 1 commit into
Conversation
When a smart cell embeds very large source payloads (e.g. Excalidraw JSON), Livebook.Text.Delta.diff/2 can take seconds to minutes to compute because the underlying Myers algorithm's worst case grows with the edit distance on very long inputs. This change keeps the existing fine-grained Myers path for normal inputs and short-circuits with a single canonical delete+insert replace when either input exceeds 100_000 UTF-16 code units. The replace is built through the public Delta constructors so that append/2 normalization keeps the ops canonical, and equal payloads short-circuit to an empty delta. The threshold is exposed via Delta.large_payload_threshold/0 for tests and future tuning. Fixes livebook#2916. Signed-off-by: simonyang08 <ppt5928@gmail.com>
|
|
simonyang08
marked this pull request as ready for review
September 3, 2026 06:08
Member
|
Hey @simonyang08, thank you for the PR! Short-circuting is the way to go, but if we only check the text against the limit, then a cell with a single long line will degrade to replace every time, even for changes where the long line is not affected. I opened #3215 with an expanded approach :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Livebook.Text.Delta.diff/2becomes prohibitively slow when a smart cell embeds a very large source payload (e.g. Excalidraw JSON): the Myers implementation's cost grows with the edit distance, and on large inputs even a small change can take many seconds (measured 43s on a ~75KB JSON-like payload with scattered edits on the unpatched baseline). Livebook appears to hang when the cell source is re-synced (#2916).Change
Keep the existing fine-grained Myers path for normal inputs and short-circuit to a single replace when either input exceeds
100_000UTF-16 code units (measured withText.JS.length, matching the delta length convention used across this module):Delta.delete/2+Delta.insert/2constructors soappend/2normalization keeps the ops canonical: no zero-length deletes/inserts, and the adjacent pair is stored in the module's standard insert-then-delete form, like every other delta producer.Delta.large_payload_threshold/0for tests and future tuning.Fixes #2916.
Tests
mix test test/livebook/text/ test/livebook/session/data_sync_test.exs→ 2 doctests, 97 tests, 0 failures.mix format --check-formattedpasses.Trade-off
For very large payloads a single edit may now send the full payload over the wire instead of a fine-grained diff — the trade-off suggested in the issue thread. Fine-grained concurrent editing of such payloads degrades to last-writer-wins on the replaced range, which seems acceptable when editor stability beats wire-size efficiency.