Recursive AI

Reduce text diff edit runs in C#

OneDiff is a free, open-source diff library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

An edit run is a stretch of text that’s all added, all deleted, or all unchanged.

Terminal
dotnet add package RecursiveAI.OneDiff

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneDiff;
foreach (int cost in new[] { 0, 4 })
    Console.WriteLine(
        Diff.Efficient("A-B", "X-Y", cost)
            .Pieces.Count(run =>
                run.Kind != EditKind.Equal));

Unzip the example, then dotnet run.

Prints the change-run count before and after cleanup.

Text
4
2

The full program

The program uses two changes separated by a hyphen. It prints both scripts with the same inputs and character-level comparison.

C#
using OneDiff;
 
const string before = "A-B";
const string after = "X-Y";
 
Print("Raw", Diff.Characters(before, after));
Print("Cleaned", Diff.Efficient(before, after, editCost: 4));
 
static void Print(string label, TextDiff diff)
{
    int changes = diff.Pieces.Count(run => run.Kind != EditKind.Equal);
    Console.WriteLine($"{label}: {changes} change runs");
    foreach (var run in diff.Pieces)
        Console.WriteLine($"  {run.Kind}: {run.Text}");
}

dotnet run prints:

Text
Raw: 4 change runs
  Delete: A
  Insert: X
  Equal: -
  Delete: B
  Insert: Y
Cleaned: 2 change runs
  Delete: A-B
  Insert: X-Y

The hyphen moves from the equal run into both the deletion and insertion. Cleanup changes how the edits are grouped, not the text they reconstruct. A negative editCost throws ArgumentOutOfRangeException.

Variations

Keep a one-character match, or disable cleanup. In the full program, replace the two Print calls with these:

C#
Print("Cost 1", Diff.Efficient(before, after, editCost: 1));
Print("Cost 0", Diff.Efficient(before, after, editCost: 0));

Both calls print four change runs on this input. At cost 1 the one-character match isn’t below the threshold for four surrounding runs. At 0 cleanup leaves the script alone.

The short answer

Use Diff.Efficient(before, after, editCost: 4) to trade short unchanged spans for fewer insertion and deletion runs. The default tokenization is by character, as in Diff.Characters. Compare those two results on the same input so that you’re measuring cleanup rather than a change in tokenization. Count only Insert and Delete if those are the records your format has to write.

What is an edit script in a text diff?

An edit script describes how to get from the old text to the new text. Equal runs retain text; deletions use old text, and insertions use new text. A replacement is represented as a deletion and an insertion, not as a third kind of change run. In this example the two replacements start out as four change runs with one equal run between them. After cleanup there’s one replacement, or two change runs. The hyphen still appears in the result because the combined insertion includes it.

How do you make a text diff more compact?

If a patch or message format has a fixed overhead per operation, count that overhead as a cost per run. Efficiency cleanup’s cost model charges one unit per character of changed text plus editCost for each insertion or deletion run. With four change runs around an equal span, it can fold that span when its length is below editCost. With three runs, the threshold is half the cost. With two, the merge doesn’t save a run and that rule won’t fold the span. The library can also join edits by sliding a change across a whole equal run.

For A-B and X-Y, folding the hyphen means carrying it twice, once in each changed run, but doing away with two change records. A lower run count isn’t a promise of a smaller file: your encoding might charge for bytes, offsets, or other metadata differently. If bytes are the deciding factor, compare the sizes of the actual messages you’ll send.

What’s the difference between semantic and efficiency cleanup?

Efficiency cleanup is for a consumer that has a cost per insertion or deletion record. It doesn’t score whether an edit begins at a natural word or line boundary. Semantic cleanup is for people reading the changes: it can remove distracting matches and move boundaries to make the result easier to read. If the unchanged hyphen is valuable context in a review, the raw script may be the right one to display even when you send the more compact script over the wire. Both cleanup modes rewrite the representation, not the input strings.

All onediff recipes