| Artifact ID: | 3be8c9e8879fed9c80bf2f8a8bfb0595d88f5faf7297cb872aa5949e9d48482c |
|---|---|
| Page Name: | checkin/20def862b03a4202d9e93a895187b312300c3252622ca200007173e261b516e0 |
| Date: | 2026-11-30 15:23:34 |
| Original User: | drh |
| Mimetype: | text/x-markdown |
Side-by-side diff: reducing edit-mark "confetti"
Problem
When a block of lines is mostly rewritten, the side-by-side diff pairs the
old and new lines and then marks the individual changed spans inside each
pair. For a rewrite this produces many tiny marks separated by coincidental
matches ((, ,, a 1-byte common letter...). The marks are noisier than no
marks at all.
How the marks are produced
- Line pairing -
diffBlockAlignment()runs a Wagner edit-distance over a change block and decides which left line becomes which right line, usingmatch_dline()(score 5..100, lower = more similar). A pair was accepted ifscore<=90or if it was near the diagonal, so even completely different lines were paired as an "edit". - Marks inside a pair -
oneLineChange()takes the common prefix and suffix, thentextLineChanges()repeatedly splits at the longest common substring (textLCS()) while spans are at least 6 bytes.textLCS()accepted a common substring of any length, including 1 byte.
Builders: xEdit (marks inside line), xReplace (delete + insert, no inner
marks). The alignment array uses codes 1 delete, 2 insert, 3 edit, 4 replace.
The change
Weakly matching lines (score >
DIFF_REPLACE_SCORE=75) are still lined up but shown as replace (code 4): deleted and inserted with no inner marks. Implemented atdiffBlockAlignment()A common fragment must be >=
LCS_MIN_LEN(4) bytes and contain an alnum run >=LCS_MIN_ALNUM(3) to split a span (lcsIsUseful()). Implemented attextLineChanges()If <
LINECHNG_MIN_COMMON_PCT(40)% of the changed region is common, use one big span instead of a split (lineChangesAreWorthShowing()) Implemented atOneLineChange()
Other fixes made at the same time:
fossil test-line-matchsegfaulted:test_dline_match()leftDLine.nwandDLine.indentuninitialized. Both structs are now zeroed.- The old alignment code stored
3 | score*4in an unsigned char, which overflows above score 63. Nothing used the stored score, so the matrix now just records 3 or 4, and the traceback keeps that code.
Results
Unit pairs with fossil test-line-diff A B (digits mark change spans):
| Pair | Before | After |
|---|---|---|
if(...) line rewrite, score 85 |
5 spans | 1 span |
sqlite3_exec(..) -> sqlite3_prepare_v2(..) |
3 spans | 1 span |
...MemSetStr(..) -> ...MemSetText(..) |
5 spans | 1 span |
foo(alpha, beta, gamma) -> foo(alpha, delta, gamma) |
1 small span | unchanged |
compute_widget_total(a, b) -> (a, b, c) and offset -> offset2 |
2 small spans | unchanged |
In a side-by-side HTML diff of a small file, the score-85 line shows no
marks at all (delete plus insert); the other changed lines keep their edit
marks. The diff tests in test/ pass (11 of 11).
Caveats
- The thresholds (75, 4, 3, 40%) are guesses from a handful of examples, not
tuned on real rewrite hunks. They are
#defines insrc/diff.c. - A pair like
exec(..)->prepare_v2(..)(score 73) is still paired as an edit, but its marks collapse to a single span covering text that was previously left unmarked inside the line. - The motivating example is
(
sqlite.org/src/fdiff?v1=9ced25388a5fab29&v2=5b6db495b688fa97).