Similar to what's being done in the net code, this takes advantage of the fact that most invocations use only a few common swap functions, and replaces indirect calls to them with (highly predictable) conditional branches. (The downside, of course, is that if you *do* use a custom swap function, there are a few additional (highly predictable) conditional branches on the code path.)
This actually *shrinks* the x86-64 code, because it inlines the various swap functions inside do_swap, eliding function prologues & epilogues. x86-64 code size 770 -> 709 bytes (-61) Signed-off-by: George Spelvin <l...@sdf.org> --- lib/sort.c | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/lib/sort.c b/lib/sort.c index 2aef4631e7d3..226a8c7e4b9a 100644 --- a/lib/sort.c +++ b/lib/sort.c @@ -117,6 +117,33 @@ static void generic_swap(void *a, void *b, int size) } while (n); } +typedef void (*swap_func_t)(void *a, void *b, int size); + +/* + * The values are arbitrary as long as they can't be confused with + * a pointer, but small integers make for the smallest compare + * instructions. + */ +#define U64_SWAP (swap_func_t)0 +#define U32_SWAP (swap_func_t)1 +#define GENERIC_SWAP (swap_func_t)2 + +/* + * The function pointer is last to make tail calls most efficient if the + * compiler decides not to inline this function. + */ +static void do_swap(void *a, void *b, int size, swap_func_t swap_func) +{ + if (swap_func == U64_SWAP) + u64_swap(a, b, size); + else if (swap_func == U32_SWAP) + u32_swap(a, b, size); + else if (swap_func == GENERIC_SWAP) + generic_swap(a, b, size); + else + swap_func(a, b, size); +} + /** * parent - given the offset of the child, find the offset of the parent. * @i: the offset of the heap element whose parent is sought. Non-zero. @@ -151,10 +178,10 @@ static size_t parent(size_t i, unsigned int lsbit, size_t size) * @cmp_func: pointer to comparison function * @swap_func: pointer to swap function or NULL * - * This function does a heapsort on the given array. You may provide a - * swap_func function if you need to do something more than a memory copy - * (e.g. fix up pointers or auxiliary data), but the built-in swap isn't - * usually a bottleneck. + * This function does a heapsort on the given array. You may provide + * a swap_func function if you need to do something more than a memory + * copy (e.g. fix up pointers or auxiliary data), but the built-in swap + * avoids a slow retpoline and so is significantly faster. * * Sorting time is O(n log n) both on average and worst-case. While * qsort is about 20% faster on average, it suffers from exploitable @@ -174,11 +201,11 @@ void sort(void *base, size_t num, size_t size, if (!swap_func) { if (alignment_ok(base, size, 8)) - swap_func = u64_swap; + swap_func = U64_SWAP; else if (alignment_ok(base, size, 4)) - swap_func = u32_swap; + swap_func = U32_SWAP; else - swap_func = generic_swap; + swap_func = GENERIC_SWAP; } /* @@ -194,7 +221,7 @@ void sort(void *base, size_t num, size_t size, if (a) /* Building heap: sift down --a */ a -= size; else if (n -= size) /* Sorting: Extract root to --n */ - swap_func(base, base + n, size); + do_swap(base, base + n, size, swap_func); else /* Sort complete */ break; @@ -221,7 +248,7 @@ void sort(void *base, size_t num, size_t size, c = b; /* Where "a" belongs */ while (b != a) { /* Shift it into place */ b = parent(b, lsbit, size); - swap_func(base + b, base + c, size); + do_swap(base + b, base + c, size, swap_func); } } } -- 2.20.1