public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andreas Gruenbacher <agruen@suse.de>
To: Matt Mackall <mpm@selenic.com>
Cc: Andrew Morton <akpm@osdl.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 1/8] lib/sort: Heapsort implementation of sort()
Date: Tue, 01 Feb 2005 18:50:00 +0100	[thread overview]
Message-ID: <1107280199.12050.113.camel@winden.suse.de> (raw)
In-Reply-To: <20050131193032.GR2891@waste.org>

On Mon, 2005-01-31 at 20:30, Matt Mackall wrote:
> On Mon, Jan 31, 2005 at 06:16:23PM +0100, Andreas Gruenbacher wrote:
> > Hello,
> > 
> > On Mon, 2005-01-31 at 08:34, Matt Mackall wrote:
> > > This patch adds a generic array sorting library routine. This is meant
> > > to replace qsort, which has two problem areas for kernel use.
> > 
> > looks reasonable.
> > 
> > > Note that this function has an extra parameter for passing in an
> > > optimized swapping function. This is worth 10% or more over the
> > > typical byte-by-byte exchange functions.
> > 
> > I would appreciate a version without the swap callback.
> 
> Why? To eliminate an argument?

Yes, because a custom swap routine isn't very useful generally. It's
over-engineered IMHO.

> > The optimized version of swap should use the machine word size
> > instead of u32.
> 
> That occurred to me, but most instances I found in my audit were using
> u32 rather than long.

Alright, that may be the case.

> > How about this approach instead, if you think we
> > must really optimize swapping?
> >
> > static inline void swap(void *a, void *b, int size)
> > {
> >         if (size % sizeof(long)) {
> >                 char t;
> >                 do {
> >                         t = *(char *)a;
> >                         *(char *)a++ = *(char *)b;
> >                         *(char *)b++ = t;
> >                 } while (--size > 0);
> >         } else {
> >                 long t;
> >                 do {
> >                         t = *(long *)a;
> >                         *(long *)a = *(long *)b;
> >                         *(long *)b = t;
> >                         size -= sizeof(long);
> >                 } while (size > sizeof(long));
> >         }
> > }
> 
> This makes things worse. Sort isn't inlined, so we don't know size
> until we're called and then we're branching in the innermost loop

Well, the example code I referred to had an inlined __sort, and the size
== sizeof(long) case ended up being perfectly optimized. It bloats the
code somewhat though, but it's still tiny.

> and growing the code footprint to boot.

You mean the object footprint? I don't care much whether we use some 350
bytes more or not.

> Function pointer wins in my benchmarks.

I had a slowdown in the low percentage range when doing bytewise swap
instead of wordwise swap as well, but function pointers were about as
fast as wordwise swap. So no significant gain.

> Note that there are callers like IA64 extable that want a custom swap already:
> 
> - * stack may be more than 2GB away from the exception-table).
> + * Sort the exception table. It's usually already sorted, but there
> + * may be unordered entries due to multiple text sections (such as the
> + * .init text section). Note that the exception-table-entries contain
> + * location-relative addresses, which requires a bit of care during
> + * sorting to avoid overflows in the offset members (e.g., it would
> + * not be safe to make a temporary copy of an exception-table entry on
> + * the stack, because the stack may be more than 2GB away from the
> + * exception-table).
>   */

We could either leave the current insertion sort in place in
arch/ia64/mm/extable.c, or transform the exception table into sortable
form first, as in the below mock-up.

+       /* Exception-table-entries contain location-relative addresses. Convert
+          to addresses relative to START before sorting, and convert back to
+          location-relative addresses afterwards. This allows us to use the
+          general-purpose sort function. */
+       for (p = start+1; p < finish; p++) {
+               int n = (void *)p - (void *)start;
+               p->addr += n;
+               p->cont += n;
+       }
+       sort(start, finish - start, sizeof(struct exception_table_entry),
+            compare_entries);
+       for (p = start+1; p < finish; p++) {
+               int n = (void *)p - (void *)start;
+               p->addr -= n;
+               p->cont -= n;
+       }

Switching to the generic sort probably isn't worth it in this case.

> There are a bunch of other potential users that sort parallel arrays
> a[] and b[] with keys in a[] that want this too.

Where?

Thanks,
-- 
Andreas Gruenbacher <agruen@suse.de>
SUSE Labs, SUSE LINUX GMBH


  reply	other threads:[~2005-02-01 17:50 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-31  7:34 [PATCH 0/8] lib/sort: Add generic sort to lib/ Matt Mackall
2005-01-31  7:34 ` [PATCH 1/8] lib/sort: Heapsort implementation of sort() Matt Mackall
2005-01-31  7:34   ` [PATCH 2/8] lib/sort: Replace qsort in XFS Matt Mackall
2005-01-31  7:35     ` [PATCH 3/8] lib/sort: Replace qsort in NFS ACL code Matt Mackall
2005-01-31  7:35       ` [PATCH 4/8] lib/sort: Kill qsort() Matt Mackall
2005-01-31  7:35         ` [PATCH 5/8] lib/sort: Replace open-coded O(pids**2) bubblesort in cpusets Matt Mackall
2005-01-31  7:35           ` [PATCH 6/8] lib/sort: Replace insertion sort in exception tables Matt Mackall
2005-01-31  7:35             ` [PATCH 7/8] lib/sort: Replace insertion sort in IA64 " Matt Mackall
2005-01-31  7:35               ` [PATCH 8/8] lib/sort: Use generic sort on x86_64 Matt Mackall
2005-01-31 12:02           ` [PATCH 5/8] lib/sort: Replace open-coded O(pids**2) bubblesort in cpusets Paul Jackson
2005-02-01 22:29     ` [PATCH 2/8] lib/sort: Replace qsort in XFS Chris Wedgwood
2005-02-01 22:22       ` Randy.Dunlap
2005-02-02  4:31         ` Zan Lynx
2005-02-02 10:48           ` Herbert Xu
2005-02-01 22:48       ` Matt Mackall
2005-01-31 17:16   ` [PATCH 1/8] lib/sort: Heapsort implementation of sort() Andreas Gruenbacher
2005-01-31 17:30     ` Paulo Marques
2005-02-01 17:54       ` Andreas Gruenbacher
2005-02-01 18:11         ` linux-os
2005-02-01 19:04           ` linux-os
2005-02-01 19:47           ` Andreas Gruenbacher
2005-01-31 19:30     ` Matt Mackall
2005-02-01 17:50       ` Andreas Gruenbacher [this message]
2005-02-02  1:00         ` Horst von Brand
2005-02-02 10:50     ` Herbert Xu
2005-02-02 11:14       ` Andreas Gruenbacher
2005-02-03 23:19         ` Junio C Hamano
2005-02-01  2:10   ` Horst von Brand
2005-02-27 13:17   ` Andreas Gruenbacher
2005-02-27 21:25     ` Matt Mackall
2005-02-27 21:53       ` Andreas Gruenbacher
2005-02-27 22:10         ` Andreas Gruenbacher
2005-03-01 13:23       ` Andreas Gruenbacher
2005-03-01 19:06       ` Christophe Saout
2005-03-01 20:12         ` Matt Mackall
2005-03-01 21:47           ` Andrew Morton
  -- strict thread matches above, loose matches on Subject: below --
2005-01-31 11:52 Alexey Dobriyan
2005-01-31 16:53 ` Matt Mackall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1107280199.12050.113.camel@winden.suse.de \
    --to=agruen@suse.de \
    --cc=akpm@osdl.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mpm@selenic.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox