Skip to content

Commit 22a241c

Browse files
George Spelvintorvalds
authored andcommitted
lib/sort: use more efficient bottom-up heapsort variant
This uses fewer comparisons than the previous code (approaching half as many for large random inputs), but produces identical results; it actually performs the exact same series of swap operations. Specifically, it reduces the average number of compares from 2*n*log2(n) - 3*n + o(n) to n*log2(n) + 0.37*n + o(n). This is still 1.63*n worse than glibc qsort() which manages n*log2(n) - 1.26*n, but at least the leading coefficient is correct. Standard heapsort, when sifting down, performs two comparisons per level: one to find the greater child, and a second to see if the current node should be exchanged with that child. Bottom-up heapsort observes that it's better to postpone the second comparison and search for the leaf where -infinity would be sent to, then search back *up* for the current node's destination. Since sifting down usually proceeds to the leaf level (that's where half the nodes are), this does O(1) second comparisons rather than log2(n). That saves a lot of (expensive since Spectre) indirect function calls. The one time it's worse than the previous code is if there are large numbers of duplicate keys, when the top-down algorithm is O(n) and bottom-up is O(n log n). For distinct keys, it's provably always better, doing 1.5*n*log2(n) + O(n) in the worst case. (The code is not significantly more complex. This patch also merges the heap-building and -extracting sift-down loops, resulting in a net code size savings.) x86-64 code size 885 -> 767 bytes (-118) (I see the checkpatch complaint about "else if (n -= size)". The alternative is significantly uglier.) Link: http://lkml.kernel.org/r/2de8348635a1a421a72620677898c7fd5bd4b19d.1552704200.git.lkml@sdf.org Signed-off-by: George Spelvin <[email protected]> Acked-by: Andrey Abramov <[email protected]> Acked-by: Rasmus Villemoes <[email protected]> Reviewed-by: Andy Shevchenko <[email protected]> Cc: Daniel Wagner <[email protected]> Cc: Dave Chinner <[email protected]> Cc: Don Mullis <[email protected]> Cc: Geert Uytterhoeven <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 37d0ec3 commit 22a241c

File tree

1 file changed

+81
-31
lines changed

1 file changed

+81
-31
lines changed

lib/sort.c

Lines changed: 81 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/*
3-
* A fast, small, non-recursive O(nlog n) sort for the Linux kernel
3+
* A fast, small, non-recursive O(n log n) sort for the Linux kernel
44
*
5-
* Jan 23 2005 Matt Mackall <[email protected]>
5+
* This performs n*log2(n) + 0.37*n + o(n) comparisons on average,
6+
* and 1.5*n*log2(n) + O(n) in the (very contrived) worst case.
7+
*
8+
* Glibc qsort() manages n*log2(n) - 1.26*n for random inputs (1.63*n
9+
* better) at the expense of stack usage and much larger code to avoid
10+
* quicksort's O(n^2) worst case.
611
*/
712

813
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
@@ -15,7 +20,7 @@
1520
* is_aligned - is this pointer & size okay for word-wide copying?
1621
* @base: pointer to data
1722
* @size: size of each element
18-
* @align: required aignment (typically 4 or 8)
23+
* @align: required alignment (typically 4 or 8)
1924
*
2025
* Returns true if elements can be copied using word loads and stores.
2126
* The size must be a multiple of the alignment, and the base address must
@@ -115,6 +120,32 @@ static void swap_bytes(void *a, void *b, int size)
115120
} while (n);
116121
}
117122

123+
/**
124+
* parent - given the offset of the child, find the offset of the parent.
125+
* @i: the offset of the heap element whose parent is sought. Non-zero.
126+
* @lsbit: a precomputed 1-bit mask, equal to "size & -size"
127+
* @size: size of each element
128+
*
129+
* In terms of array indexes, the parent of element j = @i/@size is simply
130+
* (j-1)/2. But when working in byte offsets, we can't use implicit
131+
* truncation of integer divides.
132+
*
133+
* Fortunately, we only need one bit of the quotient, not the full divide.
134+
* @size has a least significant bit. That bit will be clear if @i is
135+
* an even multiple of @size, and set if it's an odd multiple.
136+
*
137+
* Logically, we're doing "if (i & lsbit) i -= size;", but since the
138+
* branch is unpredictable, it's done with a bit of clever branch-free
139+
* code instead.
140+
*/
141+
__attribute_const__ __always_inline
142+
static size_t parent(size_t i, unsigned int lsbit, size_t size)
143+
{
144+
i -= size;
145+
i -= size & -(i & lsbit);
146+
return i / 2;
147+
}
148+
118149
/**
119150
* sort - sort an array of elements
120151
* @base: pointer to data to sort
@@ -129,17 +160,20 @@ static void swap_bytes(void *a, void *b, int size)
129160
* isn't usually a bottleneck.
130161
*
131162
* Sorting time is O(n log n) both on average and worst-case. While
132-
* qsort is about 20% faster on average, it suffers from exploitable
163+
* quicksort is slightly faster on average, it suffers from exploitable
133164
* O(n*n) worst-case behavior and extra memory requirements that make
134165
* it less suitable for kernel use.
135166
*/
136-
137167
void sort(void *base, size_t num, size_t size,
138168
int (*cmp_func)(const void *, const void *),
139169
void (*swap_func)(void *, void *, int size))
140170
{
141171
/* pre-scale counters for performance */
142-
int i = (num/2 - 1) * size, n = num * size, c, r;
172+
size_t n = num * size, a = (num/2) * size;
173+
const unsigned int lsbit = size & -size; /* Used to find parent */
174+
175+
if (!a) /* num < 2 || size == 0 */
176+
return;
143177

144178
if (!swap_func) {
145179
if (is_aligned(base, size, 8))
@@ -150,32 +184,48 @@ void sort(void *base, size_t num, size_t size,
150184
swap_func = swap_bytes;
151185
}
152186

153-
/* heapify */
154-
for ( ; i >= 0; i -= size) {
155-
for (r = i; r * 2 + size < n; r = c) {
156-
c = r * 2 + size;
157-
if (c < n - size &&
158-
cmp_func(base + c, base + c + size) < 0)
159-
c += size;
160-
if (cmp_func(base + r, base + c) >= 0)
161-
break;
162-
swap_func(base + r, base + c, size);
163-
}
164-
}
165-
166-
/* sort */
167-
for (i = n - size; i > 0; i -= size) {
168-
swap_func(base, base + i, size);
169-
for (r = 0; r * 2 + size < i; r = c) {
170-
c = r * 2 + size;
171-
if (c < i - size &&
172-
cmp_func(base + c, base + c + size) < 0)
173-
c += size;
174-
if (cmp_func(base + r, base + c) >= 0)
175-
break;
176-
swap_func(base + r, base + c, size);
187+
/*
188+
* Loop invariants:
189+
* 1. elements [a,n) satisfy the heap property (compare greater than
190+
* all of their children),
191+
* 2. elements [n,num*size) are sorted, and
192+
* 3. a <= b <= c <= d <= n (whenever they are valid).
193+
*/
194+
for (;;) {
195+
size_t b, c, d;
196+
197+
if (a) /* Building heap: sift down --a */
198+
a -= size;
199+
else if (n -= size) /* Sorting: Extract root to --n */
200+
swap_func(base, base + n, size);
201+
else /* Sort complete */
202+
break;
203+
204+
/*
205+
* Sift element at "a" down into heap. This is the
206+
* "bottom-up" variant, which significantly reduces
207+
* calls to cmp_func(): we find the sift-down path all
208+
* the way to the leaves (one compare per level), then
209+
* backtrack to find where to insert the target element.
210+
*
211+
* Because elements tend to sift down close to the leaves,
212+
* this uses fewer compares than doing two per level
213+
* on the way down. (A bit more than half as many on
214+
* average, 3/4 worst-case.)
215+
*/
216+
for (b = a; c = 2*b + size, (d = c + size) < n;)
217+
b = cmp_func(base + c, base + d) >= 0 ? c : d;
218+
if (d == n) /* Special case last leaf with no sibling */
219+
b = c;
220+
221+
/* Now backtrack from "b" to the correct location for "a" */
222+
while (b != a && cmp_func(base + a, base + b) >= 0)
223+
b = parent(b, lsbit, size);
224+
c = b; /* Where "a" belongs */
225+
while (b != a) { /* Shift it into place */
226+
b = parent(b, lsbit, size);
227+
swap_func(base + b, base + c, size);
177228
}
178229
}
179230
}
180-
181231
EXPORT_SYMBOL(sort);

0 commit comments

Comments
 (0)