mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Olivier Galibert <galibert@pobox.com>
To: Jon Chesterfield <jonathanchesterfield@gmail.com>
Cc: musl@lists.openwall.com
Subject: Re: [musl] [PATCH] stdlib: implement qsort_r
Date: Sun, 8 Aug 2021 21:39:00 +0200	[thread overview]
Message-ID: <CAMFK0gt8igjCSVZccTbfzqxbc5kuZYyreMmSfjeq2EnjDEEnhw@mail.gmail.com> (raw)
In-Reply-To: <CAOUYtQBSpJf-QnXpAKZQJk9E=Lx3SRxOjk-r3gm_8JnxEj7NSQ@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2037 bytes --]

I don’t believe in deep inlining in that case, there’s too much recursion.
As for adding overhead to qsort by sharing qsort_r’s code it is indeed
trivial to do but I don’t know whether the tradeoff should be done.

  OG.

Le dim. 8 août 2021 à 14:42, Jon Chesterfield <
jonathanchesterfield@gmail.com> a écrit :

> On Sun, Aug 8, 2021 at 12:29 PM Olivier Galibert <galibert@pobox.com>
> wrote:
>
>> The extension qsort_r allows calling qsort on a list of indices
>> without having a global variable to hold the data to sort.
>>
>
> qsort_r is a strong improvement on qsort. I think it's available outside of
> glibc.
>
> I remember doing something similar locally. Just looked it up and I
> renamed & mutated qsort to pass the context along. Therefore typed into
> email, I think something like this would provide an implementation of
> qsort in terms of qsort_r.
>
> // declare qsort_r
> typedef int (*cmp_r_t)(const void *, const void *, void * context);
>
>
> void qsort_r(void *base, size_t nel, size_t width, cmp_r_t cmp, void*
> context);
>
>
> // pass it a function that extracts the comparator for qsort from the
> // context
> typedef int (*cmp_t)(const void *, const void *);
>
>
> static int compare_adapter(const void *l, const void *r, void * context)
> {
>   cmpt_t c = (cmpt_t) context;
>
>
>   return c(l,r);
>
>
> }
>
> // tail call
> void qsort(void *base, size_t nel, size_t width, cmp_t c)
> {
>   return qsort_r(base, nel, width, compare_adapter, (cmp_t_t)c);
>
>
> }
>
> Given optimism about inlining or an always inline annotation
> it should turn into the same machine code as the macro
> instantiation approach. Alternatively it's a tail call into qsort_r, so
> a couple of indirections in exchange for minimal code size growth.
>
> I haven't compiled or tested that (or looked up the coding conventions
> for musl) so this is a drive by suggestion, written in pseudocode
> instead of prose for clarity.
>
> Thanks all!
>
> Jon
>
>

[-- Attachment #2: Type: text/html, Size: 4400 bytes --]

  reply	other threads:[~2021-08-08 19:39 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-08 11:26 Olivier Galibert
2021-08-08 12:42 ` Jon Chesterfield
2021-08-08 19:39   ` Olivier Galibert [this message]
2021-08-08 20:35   ` Érico Nogueira
2021-08-08 21:21     ` Jon Chesterfield
2021-08-08 23:05       ` Rich Felker
2021-08-09  6:03         ` Olivier Galibert
2021-08-09 17:27   ` Florian Weimer
2021-08-08 20:28 ` Érico Nogueira
2021-08-18 17:36 Olivier Galibert
2021-08-18 20:18 ` Rich Felker

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=CAMFK0gt8igjCSVZccTbfzqxbc5kuZYyreMmSfjeq2EnjDEEnhw@mail.gmail.com \
    --to=galibert@pobox.com \
    --cc=jonathanchesterfield@gmail.com \
    --cc=musl@lists.openwall.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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).