23

The diff implementation on OpenBSD has a non-standard -d option with the following documentation:

-d

Try very hard to produce a diff as small as possible. This may consume a lot of processing power and memory when processing large files with many changes.

The GNU diff implementation has the same option with the shorter documentation

-d, --minimal

try hard to find a smaller set of changes

From time to time I've used this option just to see if it generates output that is in any shape or form different from the same diff command without the option, but I've never seen any difference (no pun intended).

Could someone provide or point to an example where this option actually produces a different result from the same command without -d? Alternatively, if someone could explain the circumstances required for this option to kick in. I'm also uncertain whether "minimal" means "fewer lines of output" or "fewer hunks".

An uneducated guess is that it has to do with very large hunks.

7
  • 1
    unix.stackexchange.com/questions/472528 piqued your curiosity did it? (-:
    – JdeBP
    Commented Oct 1, 2018 at 9:54
  • @JdeBP Yes indeed. It reminded me about this flag and the fact that I simply don't know what it does since I've never seen it do anything.
    – Kusalananda
    Commented Oct 1, 2018 at 9:56
  • 1
    info diff performance explains it IIRC Commented Oct 1, 2018 at 10:28
  • 1
    Clearly related. Sadly no example of myers --> minimal results.
    – user232326
    Commented Oct 2, 2018 at 6:36
  • 1
    I would really like to get an example that would create different output with gdiff -d in order to check whether the additions to OpenBSD are useful. From my tests, I could not get any differences but it is obvious that the OpenBSD code slows down the performance which looks like a significant impact, since the diff Algorithm from Douglas McIlroy is faster than gdiff as long as you use normal file sizes.
    – schily
    Commented Oct 2, 2018 at 13:21

1 Answer 1

14

In GNU diff, also used on FreeBSD, the --minimal flag triggers an algorithm variation by Paul Eggert that causes it "to limit the cost to O(N**1.5 log N) at the price of producing suboptimal output for large inputs with differences". More specifically, it causes it to not apply several heuristics that deal in finding merely close to optimal solutions and in throwing out "confusing" lines as extra differences.

In OpenBSD diff, which uses the older Unix diff algorithm from the 1970s, the algorithm employed is credited to Harold Stone, and the --minimal flag triggers a search that is (effectively un-) bounded by the maximum value of an unsigned integer instead of by the square root of the size of the range of lines being compared (or 256 if it is greater).

Further reading

2
  • 1
    When I created a better diff from the UNIX sources, I checked that OpenBSD enhancement and could not find any better results. Note that the original stone() function uses: ` } while ((y = b[++j]) > 0);` and BTW: for normal file sizes, my enhanced UNIX diff is faster than GNU diff.
    – schily
    Commented Oct 1, 2018 at 10:50
  • Thank you for answer. Unfortunately I still cannot see what are the differences. In what circumstances it will produce different outputs?
    – midnite
    Commented Dec 26, 2021 at 13:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .