Skip to content

Add a step budget that bounds diff work without a clock - #18

Merged
ajitk merged 8 commits into
faster-word-difffrom
diff-step-budget
Nov 12, 2026
Merged

ajitk merged 8 commits into
faster-word-difffrom
diff-step-budget

Conversation

@distill-ai-dev

@distill-ai-dev distill-ai-dev commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

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>, default None (no limit, as before). One step is one diagonal visited or one token walked along a snake; the budget is shared across one diff_main call.
  • When it runs out, the search runs on until it has a matched run of 20+ tokens or has looked 1,024 edits ahead, splits there, and diffs the parts in a loop with a second allowance the size of the budget. Whatever is still unsolved comes back as delete plus insert.
  • Half-match is skipped whenever a budget is set, since its work isn't counted in steps. diff_timeout can still be set alongside; whichever runs out first stops the search.

Why

diff_timeout is 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_found and an_over_budget_diff_keeps_long_matches pin the output at exact budgets.
  • 900 random runs: both texts rebuild, reruns are equal, no empty pieces, merge is a fixed point.
  • Work stays within 2 × budget plus one snake; 200,000 unrelated chars finish on a 256 KB stack.
  • No budget, a sufficient budget, and timeout-only give the same output as before.
  • One over-budget case each for budget + timeout, word mode, grapheme mode, diff_bisect and line mode.
  • "ab"×20,000 vs "ba"×20,000 + "c" at budget 0 with the default timeout returns delete plus insert without the half-match scan.

Not covered

  • Total work is up to twice the budget, and two unrelated texts pay that for no gain.
  • A larger budget usually gives a shorter diff, not always.
  • A diff with a budget searches about 13% slower than one without, measured on 43 real page pairs (counting steps plus tracking matched runs). Diffs without a budget run the plain copy.
  • Budgets under roughly half a million steps on texts with little in common return delete plus insert, same as budget 0.
  • diff_step_budget is a new public field: a full Dmp { .. } literal without ..Dmp::new() needs it.

@distill-ai-dev distill-ai-dev mentioned this pull request Nov 11, 2026
@ajitk
ajitk added this pull request to stack #20 November 12, 2026 05:32
`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.
@ajitk
ajitk merged commit e6c6003 into master Nov 12, 2026
4 checks passed
@ajitk
ajitk deleted the diff-step-budget branch November 12, 2026 12:13
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>
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.

2 participants