---
title: "Limit diff algorithm work in C#"
url: https://recursiveai.org/onediff/cookbook/bound-diff-work/
description: "Set a diff search work cap in C# with OneDiff. Inspect CostCapReached and Work, and distinguish a valid coarse result from a completed search."
version: "1.0.1"
---

# Limit diff algorithm work 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/bound-diff-work/bound-diff-work.zip)

Cap the search and check whether it ended early.

```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;
var diff = Diff.Lines("a\nb\nc\n", "c\nb\na\n",
    new DiffSettings { MaxWork = 1 });
var status = diff.Diagnostic;
Console.WriteLine(status.CostCapReached);
```

The cap is expressed in algorithm work units, not milliseconds, bytes or output lines.

Prints whether the search stopped at its work cap:

```text
True
```

Unzip [the example](https://recursiveai.org/onediff/cookbook/bound-diff-work/bound-diff-work.zip), then `dotnet run`.

## The full program

The download compares the same three lines in reverse order with a deliberately tiny work cap.

```csharp
using OneDiff;

var diff = Diff.Lines("a\nb\nc\n", "c\nb\na\n",
    new DiffSettings { MaxWork = 1 });
Console.WriteLine($"Capped: {diff.Diagnostic.CostCapReached}");
Console.WriteLine($"Work: {diff.Diagnostic.Work}");
Console.Write(Diff.ToTerminal(diff, color: false));
```

```text
Capped: True
Work: 2
@@ -1,3 +1,3 @@
-a
-b
-c
+c
+b
+a
```

A negative `MaxWork` throws `ArgumentOutOfRangeException`; reaching a valid cap returns a result rather than throwing.

## Variations

### Allow an unbounded search

Replace the comparison in the full program; keep its diagnostic and output lines.

```csharp
// Replace the comparison in the full program:
var diff = Diff.Lines("a\nb\nc\n", "c\nb\na\n",
    new DiffSettings { MaxWork = long.MaxValue });
```

```text
Capped: False
Work: 14
@@ -1,3 +1,3 @@
-a
-b
 c
+b
+a
```

## Related recipes

- [Write an ANSI colored terminal diff in C#](https://recursiveai.org/onediff/cookbook/write-colored-terminal-diff/)
- [Diff two lists with a custom comparer in C#](https://recursiveai.org/onediff/cookbook/diff-typed-sequences/)
- [Compare text in C#](https://recursiveai.org/onediff/cookbook/compare-text/)

## The short answer

Pass `DiffSettings { MaxWork = ... }` to `Diff.Lines` and inspect `TextDiff.Diagnostic.CostCapReached`. A capped result is still a valid diff, but it may replace a broad region rather than finding the smallest set of edits. Don’t treat “a result was returned” as evidence that the search completed.

The example deliberately sets a tiny budget so the distinction is visible. It reports the cap and prints a coarse replacement of all three lines. The unbounded variation finds a shared line instead.

## How do I limit diff algorithm work in C#?

Use a finite `MaxWork` on the comparison call, as the snippet at the top does. The [algorithm documentation](https://recursiveai.org/onediff/docs/diff-algorithms/) defines Myers work as diagonal visits plus items matched while following a matching run, called a snake. The anchor-based algorithms count scanned items and comparisons while extending matches.

The diagnostic reports the work spent as well as whether the cap was reached. In this example the budget is one and the reported work is two. Treat the cap as the search’s stopping threshold, not a promise that `Work` will never exceed the supplied number. It is also not an input-size or allocation limit: the API still has to accept and tokenize the strings. If you need to reject oversized input, make that decision before starting the comparison.

## How can I tell if a diff algorithm stopped early?

Read `CostCapReached` on the returned diff’s diagnostic. A true value means the search exhausted its budget; a false value means it didn’t end because of the cap. `Work` is diagnostic information, not an edit count. The count includes shared head and tail items too. A comparison with nothing to search, such as an empty side, reports zero work.

When the cap is reached, already solved regions remain solved. Unsearched regions become a deletion followed by an insertion. That gives the output complete coverage of both inputs rather than a truncated list of edits. It also explains why a large replacement does not necessarily mean that no smaller shared runs exist.

For a viewer, you can display that result with an “ended early” status. For a workflow that requires a completed search, branch on the diagnostic before accepting the result. Raising the budget and comparing again is a separate policy decision; the returned object doesn’t automatically retry.

## How does the C# diff algorithm work with an unlimited budget?

Set `MaxWork = long.MaxValue`, as in the variation. That is the documented unbounded setting. Myers is the default algorithm and finds a script with the fewest deleted plus inserted items unless the work cap is reached. Its tie-breaking is deterministic, but a minimal script isn’t necessarily the alignment a person finds easiest to read.

The same settings also let you select patience or histogram. Those use anchors and aren’t guaranteed minimal even when their searches complete. Consequently, a clear cap diagnostic and a minimum edit count are separate properties: don’t infer the second from the first without considering the chosen algorithm.

An unlimited work setting is not a deadline. The comparison overloads also accept cancellation tokens; the docs describe cancellation checks during the search, not a hard wall-clock cutoff. Choose a finite work cap when you want a coarse but complete fallback result, and cancellation when the caller no longer wants the operation to continue.
