Repository navigation
Skip the surrogate gap when numbering word tokens - #16
Merged
Merged
Conversation
The 55,296th distinct word mapped to U+D800, which is not a char, so diff_words_tochars panicked. The decoder already skips the gap.
This was referenced Nov 11, 2026
Merged
ajitk
added a commit
that referenced
this pull request
Nov 12, 2026
Stacked on #16. Faster word diff, cleanup and patching, same output. Also fixes one panic and one stack overflow in `diff_cleanup_merge`. - every cleanup pass now does one pass over the diff. They used to rescan or `Vec::remove` after each change, which is quadratic: semantic went 3.3 s → 7 ms on 30,000 shuffled words, and at 80,000 pieces lossless went 2.8 s → 18 ms, overlaps 8.9 s → 47 ms, efficiency 10.5 s → 27 ms, merge shifts 2.8 s → 34 ms - `diff_cleanup_merge` returns an empty diff when everything cancels (it panicked on subtract overflow) and runs its rounds in a loop (it recursed once per round and could overflow the stack) - `diff_words_tochars` hashes each word once and borrows it from the input. Word ids use the std keyed `HashMap`, so tokens with a shared prefix can't pile into one bucket - `patch_splitmax`, `patch_make` and `patch_apply` stop splicing the whole text per edit: splitting one patch of 40,000 edits 11.4 s → 14 ms, applying 20,000 sparse patches 484 → 22 ms - bitap zeroes only its search window, and `rfind_sub` searches backward from the expected spot ## Tests - Each rewrite keeps the old loop in the tests as the reference and compares against it on seeded random inputs with pinned branch counts. - 43 real page pairs: all four cleanup passes hash the same before and after. ## Not covered - `patch_make` still scans the whole text once per patch to check its context is unique, as the reference does. - `patch_apply` on same-length word edits is about 7% slower (1.88 → 2.02 ms at 20,000 words). - Merge with long chains of shifting edits is still one round per link. --------- Co-authored-by: ajit <ajit@distill.io>
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.
diff_words_tocharspanics on the 55,296th distinct word.Change
make_token_dictnumbers tokens throughslot_to_id, so ids step over the surrogate range.Why
Words are packed as placeholder chars. Id 55,296 is U+D800, which is not a
char, sochar::from_u32(..).unwrap()panics. The decoder (id_to_slot) already skipped the range; the encoder didn't.Tests
word_tokens_round_trip_past_the_surrogate_gap: a text with more distinct words than fit below the gap diffs and rebuilds both texts. Panics onmaster.Not covered
charrange).