Lib: sort.c: replaced heap sort algorithm with introspective sort
PATCH 1/1 Lib: sort.c: replaced heap sort algorithm with introspective sort
Replaced heap sort algorithm with faster introspective sort algorithm.
---
v1: The introspective sort algorithm is faster the heap sort (for example on my machine on a 100MB of random data it was consistently almost twice faster) and it doesn't have the worst case, unlike qsort.
lib/sort.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 112 insertions(+), 15 deletions(-)
lib/sort.c +112 -15
@@ -1,8 +1,9 @@// SPDX-License-Identifier: GPL-2.0 /* -* A fast, small, non-recursive O(nlog n) sort for the Linux kernel +* A fast, recursive O(nlog n) sort for the Linux kernel * * Jan 23 2005 Matt Mackall <[email protected]> +* Feb 5 2019 Andrey Abramov <[email protected]> (introspective sort) */ #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt
@@ -48,33 +49,22 @@ static void generic_swap(void *a, void *b, int size)* @num: number of elements * @size: size of each element * @cmp_func: pointer to comparison function -* @swap_func: pointer to swap function or NULL +* @swap_func: pointer to swap function * -* This function does a heapsort on the given array. You may provide a -* swap_func function optimized to your element type. +* This function does a heapsort on the given array. * * 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 * O(n*n) worst-case behavior and extra memory requirements that make * it less suitable for kernel use. */ - -void sort (void *base,size_t num,size_t size, +void heapsort (void *base,size_t num,size_t size,int (*cmp_func)(const void *,const void *),void (*swap_func)(void *,void *,int size)) {/* pre-scale counters for performance */ int i = (num/2 -1 ) * size, n = num * size, c, r; -if (!swap_func) { -if (size ==4 &&alignment_ok (base,4 )) - swap_func = u32_swap; -else if (size ==8 &&alignment_ok (base,8 )) - swap_func = u64_swap; -else - swap_func = generic_swap; - } -/* heapify */ for ( ; i >=0 ; i -= size) {for (r = i; r *2 + size < n; r = c) {
@@ -103,4 +93,111 @@ void sort(void *base, size_t num, size_t size, } } +void introsort (void *base,size_t num,size_t size, +int (*cmp_func)(const void *,const void *), +void (*swap_func)(void *,void *,int size), +unsigned int max_depth,unsigned int depth) +{ + +void *last = base + (num -1 ) * size; +void *pivot = base + ((num -1 ) /2 ) * size; +void *i = base; +void *j = last; + +if (num <=1 ) +return ; + +/* switching to heapsort */ +if (depth >= max_depth) { +heapsort (base, num, size, cmp_func, swap_func); +return ; + } + +/* making pivot be the median of middle, first and last elements */ +if ((cmp_func (pivot, base) >=0 &&cmp_func (base, last) >=0 ) + || (cmp_func (last, base) >=0 &&cmp_func (base, pivot) >=0 )) { + pivot = base; + }else if ((cmp_func (pivot, last) >=0 &&cmp_func (last, base) >=0 ) + || (cmp_func (base, last) >=0 &&cmp_func (last, pivot) >=0 )) { + pivot = last; + } + +/* split array */ +while (true) { +while (cmp_func (i, pivot) <0 && i < last) + i += size; +while (cmp_func (j, pivot) >0 && j > base) + j -= size; + +if (i >= j) +break ; + +swap_func (i, j, size); + +if (i == pivot) + pivot = j; +else if (j == pivot) + pivot = i; + + j -= size; + i += size; + } + +/* continue for smaller parts */ +if (i < last) +introsort (i, ((size_t )last - (size_t )i) / size +1 , + size, cmp_func, swap_func, max_depth, depth +1 ); +if (base < j) +introsort (base, ((size_t )j - (size_t )base) / size +1 , + size, cmp_func, swap_func, max_depth, depth +1 ); +} + +unsigned int log2_up (size_t val) +{ +unsigned int log =0 ; +size_t current =1 ; + +unsigned int max_reachable_log =sizeof (val) *8 -1 ; + +while (current < val) { + current <<=1 ; + log++; +if (log == max_reachable_log && current < val) +return max_reachable_log +1 ; + } + +return log; +} + + +/** +* sort - sort an array of elements +* @base: pointer to data to sort +* @num: number of elements +* @size: size of each element +* @cmp_func: pointer to comparison function +* @swap_func: pointer to swap function or NULL +* +* This function does a introspective sort on the given array. You may provide a +* swap_func function optimized to your element type. +* +* The introspective sort use both qsort and heapsort, +* so it is faster than heapsort on average, +* but it doesn't have the worst case unlike qsort +*/ +void sort (void *base,size_t num,size_t size, +int (*cmp_func)(const void *,const void *), +void (*swap_func)(void *,void *,int size)) +{ +if (!swap_func) { +if (size ==4 &&alignment_ok (base,4 )) + swap_func = u32_swap; +else if (size ==8 &&alignment_ok (base,8 )) + swap_func = u64_swap; +else + swap_func = generic_swap; + } + +introsort (base, num, size, cmp_func, swap_func,log2_up (num) *2 ,1 ); +}EXPORT_SYMBOL (sort);
Signature · 1 line
2.20.1
Andrew Morton
maintainerQuoted 5 lines · On Tue, 5 Feb 2019 21:24:13 +0300 Andrey Abramov <[email protected]> wrote:
Replaced heap sort algorithm with faster introspective sort algorithm.
Signed-off-by Andrey Abramov <[email protected]>---
v1: The introspective sort algorithm is faster the heap sort (for example on my machine on a 100MB of random data it was consistently almost twice faster) and it doesn't have the worst case, unlike qsort.
I guess faster is always better, but are there any known parts of the kernel which actually care about the performance of sort()?
lib/sort.c line 3
Quoted 10 lines
...
lib/sort.c +1
-1@@ -1,8 +1,9 @@// SPDX-License-Identifier: GPL-2.0 /* -* A fast, small, non-recursive O(nlog n) sort for the Linux kernel +* A fast, recursive O(nlog n) sort for the Linux kernel
oops. A good analysis of the maximum stack utilization is needed, please.
Quoted 14 lines
lib/sort.c +2
-3* Jan 23 2005 Matt Mackall <[email protected]> +* Feb 5 2019 Andrey Abramov <[email protected]> (introspective sort) */ #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt@@ -48,33 +49,22 @@ static void generic_swap(void *a, void *b, int size)* @num: number of elements * @size: size of each element * @cmp_func: pointer to comparison function -* @swap_func: pointer to swap function or NULL +* @swap_func: pointer to swap function * -* This function does a heapsort on the given array. You may provide a -* swap_func function optimized to your element type.
lib/sort.c static void generic_swap(void *a, void *b, int size) · line 61
Quoted 10 lines
lib/sort.c +2
-2+* This function does a heapsort on the given array. * * 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 * O(n*n) worst-case behavior and extra memory requirements that make * it less suitable for kernel use. */ - -void sort(void *base, size_t num, size_t size, +void heapsort(void *base, size_t num, size_t size,
static?
Quoted 19 lines
lib/sort.c +0
-9int (*cmp_func)(const void *,const void *), void (*swap_func)(void *, void *, int size)) { /* pre-scale counters for performance */ int i = (num/2 - 1) * size, n = num * size, c, r; -if (!swap_func) { -if (size ==4 &&alignment_ok (base,4 )) - swap_func = u32_swap; -else if (size ==8 &&alignment_ok (base,8 )) - swap_func = u64_swap; -else - swap_func = generic_swap; - } -/* heapify */ for ( ; i >=0 ; i -= size) {for (r = i; r *2 + size < n; r = c) {
lib/sort.c void sort(void *base, size_t num, size_t size, · line 96
Quoted 4 lines
lib/sort.c +1
-0@@ -103,4 +93,111 @@ void sort(void *base, size_t num, size_t size, } } +void introsort(void *base, size_t num, size_t size,
static?
lib/sort.c void sort(void *base, size_t num, size_t size, · line 105
Quoted 9 lines
lib/sort.c +9
-0+int (*cmp_func)(const void *,const void *), + void (*swap_func)(void *, void *, int size), + unsigned int max_depth, unsigned int depth) +{ + + void *last = base + (num - 1) * size; +void *pivot = base + ((num -1 ) /2 ) * size; +void *i = base; +void *j = last;
That's about 100 bytes per invokation.
lib/sort.c void sort(void *base, size_t num, size_t size, · line 110
Quoted 4 lines
lib/sort.c +4
-0+if (num <=1 ) +return ; + +/* switching to heapsort */
Please explain the reason for this in the comment.
lib/sort.c void sort(void *base, size_t num, size_t size, · line 112
Quoted 2 lines
lib/sort.c +2
-0+if (depth >= max_depth) { +heapsort (base, num, size, cmp_func, swap_func);
Ditto.
lib/sort.c void sort(void *base, size_t num, size_t size, · line 116
Quoted 4 lines
lib/sort.c +4
-0+return ; + } + +/* making pivot be the median of middle, first and last elements */
"Make"
(and why?)
Quoted 29 lines
lib/sort.c +29
-0+if ((cmp_func (pivot, base) >=0 &&cmp_func (base, last) >=0 ) + || (cmp_func (last, base) >=0 &&cmp_func (base, pivot) >=0 )) { + pivot = base; + }else if ((cmp_func (pivot, last) >=0 &&cmp_func (last, base) >=0 ) + || (cmp_func (base, last) >=0 &&cmp_func (last, pivot) >=0 )) { + pivot = last; + } + +/* split array */ +while (true) { +while (cmp_func (i, pivot) <0 && i < last) + i += size; +while (cmp_func (j, pivot) >0 && j > base) + j -= size; + +if (i >= j) +break ; + +swap_func (i, j, size); + +if (i == pivot) + pivot = j; +else if (j == pivot) + pivot = i; + + j -= size; + i += size; + } +
lib/sort.c void sort(void *base, size_t num, size_t size, · line 155
Quoted 10 lines
lib/sort.c +10
-0+/* continue for smaller parts */ +if (i < last) +introsort (i, ((size_t )last - (size_t )i) / size +1 , + size, cmp_func, swap_func, max_depth, depth +1 ); +if (base < j) +introsort (base, ((size_t )j - (size_t )base) / size +1 , + size, cmp_func, swap_func, max_depth, depth +1 ); +} + +unsigned int log2_up (size_t val)
static?
Quoted 5 lines
lib/sort.c +5
-0+{ +unsigned int log =0 ; +size_t current =1 ; + +unsigned int max_reachable_log =sizeof (val) *8 -1 ;
lib/sort.c void sort(void *base, size_t num, size_t size, · line 170
Quoted 10 lines
lib/sort.c +10
-0+ +while (current < val) { + current <<=1 ; + log++; +if (log == max_reachable_log && current < val) +return max_reachable_log +1 ; + } + +return log; +}
Please add a comment explaining what this does. Partly so that readers (such as I) can understand why it can't utilize the existing log2().
Quoted 4 lines
lib/sort.c +4
-0+ +/** +* sort - sort an array of elements +* @base: pointer to data to sort
lib/sort.c void sort(void *base, size_t num, size_t size, · line 185
Quoted 10 lines
lib/sort.c +10
-0+* @num: number of elements +* @size: size of each element +* @cmp_func: pointer to comparison function +* @swap_func: pointer to swap function or NULL +* +* This function does a introspective sort on the given array. You may provide a +* swap_func function optimized to your element type. +* +* The introspective sort use both qsort and heapsort, +* so it is faster than heapsort on average,
There's a non-ascii character after "heapsort".
lib/sort.c void sort(void *base, size_t num, size_t size, · line 186
Quoted 1 line
lib/sort.c +1
-0+* but it doesn't have the worst case unlike qsort
Unclear. "like qsort"?
Quoted 17 lines
lib/sort.c +16
-0+*/ +void sort (void *base,size_t num,size_t size, +int (*cmp_func)(const void *,const void *), +void (*swap_func)(void *,void *,int size)) +{ +if (!swap_func) { +if (size ==4 &&alignment_ok (base,4 )) + swap_func = u32_swap; +else if (size ==8 &&alignment_ok (base,8 )) + swap_func = u64_swap; +else + swap_func = generic_swap; + } + +introsort (base, num, size, cmp_func, swap_func,log2_up (num) *2 ,1 ); +}EXPORT_SYMBOL (sort);