Re: [PATCH 15/16] commit-reach: make can_all_from_reach... linear
- From
René Scharfe <l.s.r@web.de>
- Date
- Oct 5, 2018, 19:36 UTC
- Message-ID
- <c05f192b-4e89-48b0-1c23-b43ec6fdb74b@web.de>
- In-Reply-To
- <20181005190847.GC17482@sigill.intra.peff.net>
Am 05.10.2018 um 21:08 schrieb Jeff King:
Show 19 quoted lines
> 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.
> As an aside, we may need to take a "scope" argument in case somebody > wants to do this in a non-static way.
Sure. (They could easily wrap the static function, but a macro parameter is simpler still.)
> It would be nice if we could make > this "static inline", but I don't think even a clever compiler would be > able to omit the wrapper call.
It could, if it was to inline qsort(3). Current compilers don't do that AFAIK, but I wouldn't be too surprised if they started to.
The typed comparison function can be inlined into the one with the void pointers, though.
René