Repository navigation
Add a step budget that bounds diff work without a clock - #18
Merged
Merged
Conversation
Merged
distill-ai-dev
force-pushed
the
diff-step-budget
branch
from
November 12, 2026 04:42
178bebc to
768651e
Compare
`diff_timeout` gives up at a wall-clock deadline, so a slow or loaded machine can return a different diff for the same inputs. The new `diff_step_budget` counts search steps instead: one per diagonal visited and per token walked along it, shared by every search of one diff. When it runs out the remaining sections fall back to delete plus insert, as they do when the deadline passes. A budget alone does not turn on half-match, so the diff stays optimal until the budget is spent. Counting costs about 6% of search time, so the search is compiled twice and a diff without a budget runs the uncounted one. Output without a budget is unchanged.
It had ended up on the Limit struct. Limit keeps its own one-line doc.
engine.rs was close to the 1000-line limit. Its unit tests now live in engine/tests.rs, the same layout as cleanup/tests.rs. The tests are unchanged; the suite still runs 11 engine tests.
Before, a search that ran out of budget gave up on its whole box and emitted it as one delete and one insert, losing every match inside it. Now a search past the budget keeps going, up to twice the budget, until it has walked a matched run of at least 20 tokens or looked 1024 edits ahead. It then splits the box at the furthest such points from the front and the back, and the parts are diffed in a loop rather than by recursion, so lopsided splits cannot overflow the stack. Merge cleanup repeats until no two equalities sit side by side, since neighbouring parts can cancel. Total work stays within twice the budget plus one snake. Diffs with no budget, with a budget that covers the work, or with only a timeout are unchanged. Tested with exact pins on small and repeated texts, a work-bound check, a check that a sufficient budget changes nothing, a 200,000-char pair on a 256 KB stack, and 900 random runs checked for validity, determinism, and merge fixed points.
distill-ai-dev
force-pushed
the
diff-step-budget
branch
from
November 12, 2026 06:31
768651e to
aa81727
Compare
ajitk
added a commit
that referenced
this pull request
Nov 12, 2026
Stacked on #18. Bumps `diff_match_patch` to 0.3.2. ## In this release - #16: `diff_words_tochars` no longer panics on the 55,296th distinct word. - #17: the word diff is faster with the same output (one-pass semantic cleanup, cheaper word packing). - #18: `Dmp::diff_step_budget`, a limit on diff work that gives the same output on every machine. ## Upgrade note `diff_step_budget` is a new public field on `Dmp`. Code that builds `Dmp` with a full struct literal and no `..Dmp::new()` needs to add it. With it set, `diff_main` skips the half-match shortcut, so the budget bounds the search on its own. Not published to crates.io yet; that happens after this merges. --------- Co-authored-by: ajit <ajit@distill.io>
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.
Stacked on #17. Adds
Dmp::diff_step_budget, a limit on diff work that doesn't depend on a clock.Change
diff_step_budget: Option<u64>, defaultNone(no limit, as before). One step is one diagonal visited or one token walked along a snake; the budget is shared across onediff_maincall.diff_timeoutcan still be set alongside; whichever runs out first stops the search.Why
diff_timeoutis wall-clock, so the same two texts can give different diffs on a fast and a busy machine. A step budget gives the same output every time. One step is about 2.4 ns native, so 400M steps is about 1 s.Giving up on the whole region loses every match in it. On 18 real page pairs at 10% of the steps they need, edits were 3.34× the exact diff with plain delete plus insert and 1.49× with the split.
Tests
a_spent_budget_keeps_what_it_foundandan_over_budget_diff_keeps_long_matchespin the output at exact budgets.diff_bisectand line mode."ab"×20,000vs"ba"×20,000 + "c"at budget 0 with the default timeout returns delete plus insert without the half-match scan.Not covered
diff_step_budgetis a new public field: a fullDmp { .. }literal without..Dmp::new()needs it.