public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: Kuan-Wei Chiu <visitorckw@gmail.com>
Cc: Zack Weinberg <zack@owlfolio.org>,
	 Adhemerval Zanella <adhemerval.zanella@linaro.org>,
	 GNU libc development <libc-alpha@sourceware.org>,
	 Noah Goldstein <goldstein.w.n@gmail.com>
Subject: Re: [PATCH] stdlib: Optimize number of calls to comparison function
Date: Sun, 17 Dec 2023 19:02:44 +0100	[thread overview]
Message-ID: <87sf404shn.fsf@oldenburg.str.redhat.com> (raw)
In-Reply-To: <ZX8mPWMIPk4tu26P@visitorckw-System-Product-Name> (Kuan-Wei Chiu's message of "Mon, 18 Dec 2023 00:47:57 +0800")

* Kuan-Wei Chiu:

> On Sun, Dec 17, 2023 at 10:42:17AM -0500, Zack Weinberg wrote:
>> It occurred to me late last night that *any* in-place sorting algorithm
>> that operates on an array, can be made stable with no additional storage
>> cost, by using each element's address as a tiebreaker for the comparison
>> function.  It *doesn't* need to be the element's original address -- all
>> that matters is that for every pair of elements that compare equal, the
>> sorting algorithm preserves the < relation for their addresses across
>> all swaps, which should happen naturally if address-< is used as a
>> tiebreaker for comparisons.
>> 
>> (That said, I still think we should give that "block exchange mergesort"
>> algorithm I dug up the other week a try.  I might have time early in
>> January to put a patch set together.)
>
> Additionally, if we're open to exploring non-in-place options, I think
> Timsort might be worth considering. It's widely adopted, including in
> Python and Android, serving as a stable hybrid sorting algorithm.
> Timsort is an adaptive sort algorithm that operates in O(n) time when
> the input is already sorted and maintains a worst-case time complexity
> of O(n log n), similar to mergesort. I'd be happy to prepare a patch
> for Timsort next month if needed.

I think we really want to get rid of that malloc in qsort.  This means
that we need something in-place-ish: we can throw a fair amount of stack
space at the problem (probably quite a bit more of the circa 1024 bytes
we use today), but not something that scales linearly with the size of
the input array.  We cannot not even even assume space for just one
temporary object on the stack, I think.

Thanks,
Florian


  reply	other threads:[~2023-12-17 18:02 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-02 21:48 Kuan-Wei Chiu
2023-12-04  8:20 ` Florian Weimer
2023-12-04 18:31   ` Kuan-Wei Chiu
2023-12-05 10:44     ` Florian Weimer
2023-12-05 20:00       ` Kuan-Wei Chiu
2023-12-05 20:35     ` Zack Weinberg
2023-12-06 10:21       ` Florian Weimer
2023-12-06 12:51         ` Adhemerval Zanella Netto
2023-12-17 15:42           ` Zack Weinberg
2023-12-17 15:55             ` Florian Weimer
2023-12-17 16:47             ` Kuan-Wei Chiu
2023-12-17 18:02               ` Florian Weimer [this message]
2023-12-05  3:22   ` [PATCH v2 0/2] stdlib: Optimize number of calls to comparison function in qsort Kuan-Wei Chiu
2023-12-05  3:22     ` [PATCH v2 1/2] " Kuan-Wei Chiu
2023-12-05  3:22     ` [PATCH v2 2/2] stdlib: Adjust the factor in tst-qsort5 Kuan-Wei Chiu
2024-02-16  7:08 ` [PATCH] stdlib: Optimize number of calls to comparison function Kuan-Wei Chiu
2024-03-27 19:45   ` Adhemerval Zanella Netto
2024-03-27 19:59     ` Kuan-Wei Chiu
2024-03-27 20:42       ` Adhemerval Zanella Netto

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=87sf404shn.fsf@oldenburg.str.redhat.com \
    --to=fweimer@redhat.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=goldstein.w.n@gmail.com \
    --cc=libc-alpha@sourceware.org \
    --cc=visitorckw@gmail.com \
    --cc=zack@owlfolio.org \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).