patch: Change default sorting algorithm in sort(1)

Cyril Zhang cyril at freebsdfoundation.org
Tue May 18 18:28:08 UTC 2021


On Tue, May 18, 2021 at 2:24 AM Hans Petter Selasky <hps at selasky.org> wrote:
> Will this affect small-memory systems ability to sort data?

It shouldn't. The sort program already allocates a large amount of
memory during the preprocessing stage, far more than the amount of
bytes in the input file (which is a wholly separate issue from the one
at hand). The extra memory required for mergesort is much smaller than
that, even in the worst case where each line of input contains only a
single character.

The sort program also has a mechanism to use temporary files in order
to handle cases where the system runs out of memory. This is
documented in the -S flag:

-S size, --buffer-size=size
    Use size for the maximum size of the memory buffer.  Size modi-
    fiers %,b,K,M,G,T,P,E,Z,Y can be used.  If a memory limit is not
    explicitly specified, sort takes up to about 90% of available
    memory.  If the file size is too big to fit into the memory buf-
    fer, the temporary disk files are used to perform the sorting.

So a small-memory system could set the -S flag to allow for some extra
memory for use with mergesort.

Finally, the --heapsort and --quicksort flags will still be available,
if memory conservation is truly needed.


More information about the freebsd-hackers mailing list