[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <dd8924c1-07a9-4317-bfa8-23271b138a62@rasmusvillemoes.dk>
Date: Thu, 14 Mar 2019 00:28:16 +0100
From: Rasmus Villemoes <linux@...musvillemoes.dk>
To: George Spelvin <lkml@....org>, linux-kernel@...r.kernel.org
Cc: Andrew Morton <akpm@...ux-foundation.org>,
Andrey Abramov <st5pub@...dex.ru>,
Geert Uytterhoeven <geert@...ux-m68k.org>,
Daniel Wagner <daniel.wagner@...mens.com>,
Don Mullis <don.mullis@...il.com>,
Dave Chinner <dchinner@...hat.com>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>
Subject: Re: [PATCH 5/5] lib/list_sort: Optimize number of calls to comparison
function
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
Powered by blists - more mailing lists