Subject: Re: [boost] [review][sort] Formal review period for Sort library begins today, November 10, and ends Wednesday, November 19
From: Steven Ross (spreadsort_at_[hidden])
Date: 2014-11-15 21:29:50
On Sat, Nov 15, 2014, 9:13 AM Niall Douglas <s_sourceforge_at_[hidden]>
> some_iterator first, last;
> sorted_view v(first, last); // O(N), builds an index
> for(auto &i : v) // O(N log N), but can early out
> This is the sort of stuff that separates a sort function from a Boost
> general purpose sort library. sorted_view builds a lookup index in
> O(N) time and space and can be looked at as a fixed initial cost.
> Iterating the view may take considerably longer, but because the O(N
> log N) complexity is pushed into the iteration, code can early out or
> do other more efficient things. Point is you give options to code
> with this design rather than sorting being all or nothing as it is
> with std::sort. What one is looking for is *significantly* improving
> on the STL sort facilities, and performance is one of many ways of
> improving on the STL.
> A particular advantage of the above design is that it should be much
> more cache friendly for large datasets. You blitz the LL cache in a
> single pre-iteration step when building the index, after that
> iteration should only touch a small subset of total cache lines.
> As a practical example of utility, coincidentally this Monday I
> should be starting yet another new Boost library, this one
> implementing a portable kernel memory allocator and manager. This
> abstracts the management of memory allocated in the kernel. One thing
> you can do with it is work with sparse really huge datasets, ones
> which cannot possibly fit into a process address space. A sorted_view
> design would let you build the sort index as a very slow initial
> overhead (each window into the sparse data has to be individually
> mapped, processed and unmapped) and then iteration could "pick off"
> the top N items from the sort using very few window maps.
> Another example of what I mean by sorted_view. Hopefully it makes
> sense now.
Thanks for the explanation. Have you considered partial_sort? That
provides the separation of the sort runtime overhead you're requesting (as
do heaps). With regards to sorting data that won't fit in main memory,
there are two standard approaches:
k-way merge, and radix-based division of the problem. Radix-based division
is usually faster (merging is simply concatenation), but k-way merge
guarantees that it will split up the data small enough to fit in RAM in one
pass (radix is usually 1, and can guarantee no more than 2 passes). Radix
based division is compatible with the kind of piece at a time sorting you
mention. Both approaches are efficient ways to sort in parallel and/or
data on disk. I've written code for both in the past.
Disk-based sorting requires writing and managing a bunch of temp files, for
which the optimal approach may vary across OSes. Most people who want a
scalable way to sort data that is too large to fit in a single system's
memory are probably best off using something like MapReduce. I could write
a radix-based division/map function to split up data into manageable chunks
as you're requesting, but handling the output streams efficiently is more
complicated than the trivial bucketing the function would provide. Have
you considered just bucketing your data based on the top X bits (say 8-16),
which allows you to finish the sort later for each bucket as needed?
> > > I'd also prefer to see fallback implementations for less than random
> > > iterators, right down to forward only iterators.
> > To sort at reasonable speed requires either random access iterators or
> > allocating a deep or shallow copy of the data. I could write a simple
> > wrapper to create a vector of iterators pointing to the input data,
> > sort the iterators, and then use them to permute the original list,
> > but what's the use case?
> You're assuming sizes of dataset which can fit into memory, and/or
> all items are available quickly to the sorting process.
> I am also unsure if random access iterators really are necessary. A
> brute force solution is to create a copy of a forward only iterator
> one per item, and swap those around. I'm sure there must be sort
> algorithms that can do better than that though.
You are correct about the brute force approach with forward iterators: that
is a shallow copy. Sorting data that won't fit in memory normally requires
a deep copy. Once you get into network or file I/O, the limitations of the
I/O tend to dominate performance if the merge/split and sorting algorithms
My recommended flow for sorting files from a forward iterator on a single
system with too much data to fit in RAM using k-way merge:
Read a section of the file at a time that will comfortably fit in RAM.
Sort it (possibly with a separate thread), and write it to disk. Repeat
with the next section, until you have a set of sorted files. Then merge
them all together in a k-way merge.
My recommended approach with MSD radix sorting:
Read the input stream, writing each element into a file corresponding to a
bucket based on its most significant bits. Track the counts of elements
within each interval at sufficient granularity (1000 or so) that if some
buckets are too full, you'll know exactly how to split them up in a second
pass and store that with buckets that need it. Then sort each bucket from
the top or the bottom as needed, concatenating the results. These
sub-sorts can be done on separate thread.
I'm willing to code up the radix bucketing with a guaranteed maximum of 2
bucketing passes, or the k-way merge step, but they can't wrap the entire
functionality without adding a bunch of file operations, which can be
tricky to port efficiently. They'd also need a bunch of parameters about
things like maximum RAM to use, number of files, maximum numbers of files
to have open simultaneously, etc.
> > > 2. Exception safety is not obvious, and explicit exception guarantees
> > > need to be made and stated per API in the documentation.
> > How about statements like this in the documentation for each of the
> > Exceptions: Throws if any of the element comparisons, the element
> > swaps (or moves), the right shift, subtraction of right-shifted
> > elements, or the operations on iterators throws.
> Exception safety is about what "throws" exactly does at any point in
> the sort. Does it leave things half sorted? If all your operators
> above are marked noexcept, can you use an inplace sort instead of
> making copies? Does your sort recover from a throw, or take
> alternative action e.g. exclude the items where a throw happens. That
> sort of thing.
> Generally the STL follows a rule of leaving inputs untouched if an
> exception is thrown which is why when operators are not noexcept it
> must do a lot of copying. This is where std::move_if_noexcept comes
> from. Probably code entering a Boost library should copy STL
> conventions here where appropriate.
I've added some basic documentation on exception safety along these lines,
just like std::sort has, in the develop branch.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk