On Fri, Oct 05, 2018 at 09:36:28PM +0200, René Scharfe wrote:

> Am 05.10.2018 um 21:08 schrieb Jeff King:
> > On Fri, Oct 05, 2018 at 08:48:27PM +0200, René Scharfe wrote:
> >> +#define DEFINE_SORT(name, type, compare)                          \
> >> +static int compare##_void(const void *one, const void *two)               
> >> \
> >> +{                                                                 \
> >> +  return compare(one, two);                                       \
> >> +}                                                                 \
> >> +static void name(type base, size_t nmemb)                         \
> >> +{                                                                 \
> >> +  const type dummy = NULL;                                        \
> >> +  if (nmemb > 1)                                                  \
> >> +          qsort(base, nmemb, sizeof(base[0]), compare##_void);    \
> >> +  else if (0)                                                     \
> >> +          compare(dummy, dummy);                                  \
> >> +}
> > 
> > I do like that this removes the need to have the code block aspart of
> > the macro.
> > 
> > Did you measure to see if there is any runtime impact?
> 
> No, but I wouldn't expect any -- the generated code should be the same
> in most cases.
> 
> Here's an example: https://godbolt.org/z/gwXENy.

OK, that's good enough for me.

> The typed comparison function can be inlined into the one with the void
> pointers, though.

Right, that makes sense. I suspect it depends on the comparison function
being static, but in a DEFINE_SORT() world, they generally could be.

So I like this approach.

-Peff

Reply via email to