Skip to content

Skip the surrogate gap when numbering word tokens - #16

Merged
ajitk merged 1 commit into
masterfrom
word-tokens-past-surrogate-gap
Nov 12, 2026
Merged

ajitk merged 1 commit into
masterfrom
word-tokens-past-surrogate-gap

Conversation

@distill-ai-dev

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

Copy link
Copy Markdown
Contributor

diff_words_tochars panics on the 55,296th distinct word.

Change

make_token_dict numbers tokens through slot_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, so char::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 on master.

Not covered

  • Ids still run out at about 1.11 million distinct words (the end of the char range).

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.
@ajitk
ajitk added this pull request to stack #20 November 12, 2026 05:32
@ajitk
ajitk merged commit 1293a42 into master Nov 12, 2026
4 checks passed
@ajitk
ajitk deleted the word-tokens-past-surrogate-gap branch November 12, 2026 12:13
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>
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