Skip to contentSkip to conversation

xen/common: low performance of lib/sort.c

PATCH In review Andrew Cooper 2 participants 2 messages Mar 2, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/common: low performance of lib/sort.c

From: keios <[email protected]>

It is a non-standard heap-sort algorithm implementation because the index of child node is wrong . The sort function still outputs right result, but the performance is O( n * ( log(n) + 1 ) ) , about 10% ~ 20% worse than standard algorithm.

[Linux commit: d3717bdf8f08a0e1039158c8bab2c24d20f492b6]

Ported to Xen.

---
CC: Jan Beulich <[email protected]>

---
 xen/common/sort.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
xen/common/sort.c +5 -5
index d96fc2a..7b7544b 100644
@@ -46,7 +46,7 @@ void sort(void *base, size_t num, size_t size,
           void (*swap)(void *, void *, int size))
 {
     /* pre-scale counters for performance */
-    int i = (num/2) * size, n = num * size, c, r;
+    int i = (num/2 - 1) * size, n = num * size, c, r;
 
     if (!swap)
         swap = (size == 4 ? u32_swap : generic_swap);
@@ -54,9 +54,9 @@ void sort(void *base, size_t num, size_t size,
     /* heapify */
     for ( ; i >= 0; i -= size )
     {
-        for ( r = i; r * 2 < n; r  = c )
+        for ( r = i; r * 2 + size < n; r  = c )
         {
-            c = r * 2;
+            c = r * 2 + size;
             if ( (c < n - size) && (cmp(base + c, base + c + size) < 0) )
                 c += size;
             if ( cmp(base + r, base + c) >= 0 )
@@ -69,9 +69,9 @@ void sort(void *base, size_t num, size_t size,
     for ( i = n - size; i >= 0; i -= size )
     {
         swap(base, base + i, size);
-        for ( r = 0; r * 2 < i; r = c )
+        for ( r = 0; r * 2 + size < i; r = c )
         {
-            c = r * 2;
+            c = r * 2 + size;
             if ( (c < i - size) && (cmp(base + c, base + c + size) < 0) )
                 c += size;
             if ( cmp(base + r, base + c) >= 0 )
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines
Quoted 1 line
Quoted 1 line · On 02.03.17 at 16:27, <[email protected]> wrote:

From: keios <[email protected]>

It is a non-standard heap-sort algorithm implementation because the index of child node is wrong . The sort function still outputs right result, but the performance is O( n * ( log(n) + 1 ) ) , about 10% ~ 20% worse than standard algorithm.

Quoted 6 lines
[Linux commit: d3717bdf8f08a0e1039158c8bab2c24d20f492b6]

Ported to Xen.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel