Re: [PATCH 1/8] lib/sort: Heapsort implementation of sort()

From: Matt Mackall (mpm_at_selenic.com)
Date: 01/31/05

  • Next message: Chuck Harding: "Re: 2.6.11-rc2-mm2"
    Date:	Mon, 31 Jan 2005 11:30:32 -0800
    To: Andreas Gruenbacher <agruen@suse.de>
    
    

    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?

    > 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.

    > 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 and
    growing the code footprint to boot. Function pointer wins in my
    benchmarks.

    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).
      */

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

    -- 
    Mathematics is the supreme nostalgia of our time.
    -
    To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
    the body of a message to majordomo@vger.kernel.org
    More majordomo info at  http://vger.kernel.org/majordomo-info.html
    Please read the FAQ at  http://www.tux.org/lkml/
    

  • Next message: Chuck Harding: "Re: 2.6.11-rc2-mm2"

    Relevant Pages

    • Re: x86 exception handling and stack demand
      ... When an exception occurs that will be passed down to user mode as an SEH exception, the kernel arranges for control to return to user mode at a special function in NTDLL, with several parameters on the stack containing information about the exception. ... In XP and later, the system stores a pointer to the initial stack allocation block in the TEB that is used by the kernel to decommit the stack via NtFreeVirtualMemory when the thread is terminated in a non-graceful fashion, closing this leak. ...
      (microsoft.public.win32.programmer.kernel)
    • [PATCH] x86: style fascism for xen assemblies
      ... * a view to being able to inline as much as possible. ... push %eax ... * This is run where a normal iret would be run, with the same stack setup: ... In order to deliver the nested exception properly, ...
      (Linux-Kernel)
    • RE: System.AccessViolationException in .NET 2.0 application
      ... Based on the call stack, there is an AV exception in the ... Microsoft Online Community Support ...
      (microsoft.public.dotnet.general)
    • Controlled types and exception safety
      ... I can classify the stack's operations by assigning them any of the above four levels, so that I know what can be expected when an exception is thrown for any reason (like inability to allocate more memory, or alike). ... For example, if the Push method of the stack gives me the strong guarantee, then I *know* that by calling this method either the new element will be appended to the stack, or the stack will remain unchanged, so that even if the exception is thrown, I don't have to worry about the stack's internal consistency. ... Since stack can be a dynamic data structure, assigning one stack object to another may involve destroying one existing data structure *and* creating a new one in its place. ...
      (comp.lang.ada)
    • Re: Stop Error: 0x0000007F (0x00000008,0x80042000,0x00000000,0x00000000)
      ... call to the handler for a prior exception. ... double fault. ... A kernel stack overflow. ... One of three types of problems occurred in kernel-mode: Hardware ...
      (microsoft.public.windowsxp.help_and_support)