lkml.org 
[lkml]   [2019]   [Mar]   [13]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [PATCH 5/5] lib/list_sort: Optimize number of calls to comparison function
From
Date
On 05/03/2019 06.58, George Spelvin wrote:
> CONFIG_RETPOLINE has severely degraded indirect function call
> performance, so it's worth putting some effort into reducing
> the number of times cmp() is called.
>
> This patch avoids badly unbalanced merges on unlucky input sizes.
> It slightly increases the code size, but saves an average of 0.2*n
> calls to cmp().
>
[snip]
>
> (I confess to being more than a little bit proud of how clean this
> code turned out. It took a lot of thinking, but the resultant inner
> loop is very simple and efficient.)
>
> Refs:
> Bottom-up Mergesort: A Detailed Analysis
> Wolfgang Panny, Helmut Prodinger
> Algorithmica 14(4):340--354, October 1995
> https://doi.org/10.1007/BF01294131
> https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.6.5260
>
> The cost distribution of queue-mergesort, optimal mergesorts, and
> power-of-two rules
> Wei-Mei Chen, Hsien-Kuei Hwang, Gen-Huey Chen
> Journal of Algorithms 30(2); Pages 423--448, February 1999
> https://doi.org/10.1006/jagm.1998.0986
> https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.4.5380
>
> Queue-Mergesort
> Mordecai J. Golin, Robert Sedgewick
> Information Processing Letters, 48(5):253--259, 10 December 1993
> https://doi.org/10.1016/0020-0190(93)90088-q
> https://sci-hub.tw/10.1016/0020-0190(93)90088-Q

This is beautiful. So no comments on the patch itself. One thing that
might be nice would be to see the reduction in number of cmp callbacks
explicitly; it should be trivial to use the priv element for that in the
list_sort_test module. But to really see it one would of course have to
extend that test to do a lot more different sizes of lists.

And looking at the actual cmp functions used made me think we might do
something similar to what you did for the swap functions for sort(),
though it will require updating call sites. Suppose we have some

struct foo {
...
struct list_head list;
...
some_integer_type key;
...
};

and a trivial cmp function that just compares the ->key members. What if
we do something like

#define LIST_SORT_SIMPLE_CMP(type, list, key) ({ \
/* encode the size and signedness of type.key along with
offsetof(type, key) - offsetof(type, list) into the low, say,
24 bits - a signed 20 bit number should be sufficient for the
offsetof diff, and we can bail at compile time if too big */
})

then do

int do_cmp(void *priv, list_head *a, list_head *b, cmp_func cmp)
{
if ((long)cmp & high_bits) /* it's a kernel pointer */
return cmp(priv, a, b);
int diff = extract_diff(cmp);
int type = extract_type(cmp);
void *keya = (void*)a + diff;
void *keyb = (void*)b + diff;
if (type == s32)
return *(s32*)keya > *(s32*)keyb;
if (type == u32)
return *(u32*)keya > *(u32*)keyb;
...

in practice, we'd probably only have to support 4- and 8-byte signed and
unsigned versions (and we can check at compile time whether key has a
supported type).

Similarly one could do a SORT_SIMPLE_CMP() for when sorting an array of
structs according to a single numeric member. That sort is not stable,
so the comparison functions would have to do a full -1,0,1 return, of
course, but we'd still avoid indirect calls.

Rasmus

\
 
 \ /
  Last update: 2019-03-14 00:29    [W:0.160 / U:6.496 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site