Re:Modern programming languages require generics
by unwind on Tuesday May 24, 2022 @11:07AM
(#31493095)
The compare function is passed by value (like all things in C) and the preprocessor is not smart enough to find the code.
[ Parent
| on HN ]
Re:Modern programming languages require generics
by derriz on Tuesday May 24, 2022 @11:26AM
(#31493352)
There was a C library - posted here but I cannot remember the name - which used the preprocessor to avoid invoking a function call per comparison. You'd use it something like this:
#define SORT_ELEMENT_TYPE int
#define SORT_COMPARE(a, b) sign(a - b)
#include "specialized_sort.h"
And the header would define a function with the following signature:
int sort_int(int[] a, size_t n);
Re:Modern programming languages require generics
by jhallenworld on Tuesday May 24, 2022 @12:04PM
(#31493828)
I've written code just like this. Another option is to make the entire sort function a C macro. This would be feasible for something simple like quicksort.
But generics provide another benefit: in theory with them, the compiler should be able to determine that two high-level types are actually the same machine type, so in the above example, SORT_ELEMENT_TYPE int and long would not generate duplicate code on many machines.
Re:Modern programming languages require generics
by KapKap55 on Tuesday May 24, 2022 @02:48PM
(#31496024)
Maybe not it, but STC (https://github.com/tylov/STC) and CTL (https://github.com/glouw/ctl) both use this define method for creating data structures and linking up compare functions for them.
Re:Modern programming languages require generics
by randbox on Tuesday May 24, 2022 @02:15PM
(#31495601)
It's not necessary to use macros. Just make both the sort function which the comparison function is passed to, as well as the comparison function itself, both "static inline". The compiler is smart enough to figure it out.
Re:Modern programming languages require generics
by jjnoakes on Tuesday May 24, 2022 @11:48AM
(#31493625)
Why is this limited to the preprocessor? I imagine the compiler proper could inline the compare function parameter into the qsort implementation body in the typical case, at the qsort() call site. (Assuming qsort's implementation was in a header, as the original comment posited).
Re:Modern programming languages require generics
by syockit on Tuesday May 24, 2022 @01:52PM
(#31495308)
You're right. I tried it here https://godbolt.org/z/457dYWfq5 and if the generic function is visible to the call site and inlineable, then the compare function can be inlined too. I didn't know this, I had always thought that the semantics of function pointer prevented this from being achievable!
Re:Modern programming languages require generics
by randbox on Tuesday May 24, 2022 @02:18PM
(#31495656)
If both the comparison function as well as the sort function which the comparison function is passed to are declared 'static inline' and available in a header, the compiler is smart enough to figure it out and inline the code, provided optimizations are turned on.
Re:Modern programming languages require generics
by brabel on Tuesday May 24, 2022 @11:14AM
(#31493173)
Does the necessary pointer casting in the implementations also make it slower than it should be (the specialized versions generated by compilers that support generics wouldn't have any casting)?
Re:Modern programming languages require generics
by eptcyka on Tuesday May 24, 2022 @11:23AM
(#31493305)
Its not the pointer casting, its the indirect function call that probably can be cached but it will always be another memory location that the CPU will need to jump to.
Re:Modern programming languages require generics
by jjnoakes on Tuesday May 24, 2022 @11:48AM
(#31493631)
If the compare function is inlined into the qsort() body, there wouldn't need to be any function call. I think that was the point of the question above.
Re:Modern programming languages require generics
by OnACoffeeBreak on Tuesday May 24, 2022 @12:03PM
(#31493812)
The way to answer this question is to look at the assembly code generated by the compiler with various optimization levels and see whether the compiler ever inlines the compare function.
https://godbolt.org/ might be useful for this exercise.