---
title: "Reduce text diff edit runs in C#"
url: https://recursiveai.org/onediff/cookbook/reduce-text-edit-runs/
description: "Fold short unchanged spans into nearby replacements with OneDiff’s efficiency cleanup. Compare insertion and deletion run counts in a runnable C# example."
version: "1.0.1"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneDiff) · [GitHub](https://github.com/recursive-ai-oss/onediff-csharp) · [Docs](https://recursiveai.org/onediff/docs/) · [Download example (.zip)](https://recursiveai.org/onediff/cookbook/reduce-text-edit-runs/reduce-text-edit-runs.zip)

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

```shell
dotnet add package RecursiveAI.OneDiff
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onediff/cookbook/reduce-text-edit-runs/reduce-text-edit-runs.zip), 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.

```csharp
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:

```csharp
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.

## Related recipes

- [Compare strings word by word](https://recursiveai.org/onediff/cookbook/compare-text/)
- [Bound the work of a text diff](https://recursiveai.org/onediff/cookbook/bound-diff-work/)
- [Diff typed sequences](https://recursiveai.org/onediff/cookbook/diff-typed-sequences/)

## 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](https://github.com/recursive-ai-oss/onediff-csharp/blob/6443915c4136cc8c5a49fa2543322011665f309e/src/OneDiff/Cleanup/DiffCleanup.cs#L56-L68) 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.
