4 ms·
>I've wondered if there is any way to coax a C compiler into inlining the sort routine, callback and all, but I haven't tried it yet. And how is the compiler s
by srean 12y ago
>I've wondered if there is any way to coax a C compiler into inlining the sort routine, callback and all, but I haven't tried it yet.
And how is the compiler supposed to inline the callback in qsort() when all it has is a pointer that will be provided only at runtime ? With JIT yes that would be possible. This requires a runtime optimization solution, not a compile time. This is also the reason why templates come so handy, sans the error messages (although GCC has improved by leagues, now to the point I find GCC's error messages easier to debug STL code with than Clang's). The error message scenario would a be a lot better once they get concepts sorted out.
- mtdewcmu 12y agoIt's probably impossible with qsort. I was wondering, if both the sort routine and the callback were declared static, would gcc be clever enough to inline both, eliminating the function pointer? It seems like it should be possible in principle.
- SamReidHughes 12y agoInline qsort, then inline the callback.
- Someone 12y agoI don't have stats, but I would think that the comparison function's definition typically is available at compile time. Also, if the compiler gets the qsort prototype by including it from the system header <stdlib.h>, I think the compiler may assume that it is the standard qsort function. So, it could emit a custom qsort function that inlines the comparison function.
- mtdewcmu 12y ago>I don't have stats, but I would think that the comparison function's definition typically is available at compile time. The comparison function usually will be. The qsort function generally won't be available. >Also, if the compiler gets the qsort prototype by including it from the system header <stdlib.h>, I think the compiler may assume that it is the standard qsort function. So, it could emit a custom qsort function that inlines the comparison function. It could do something like that, but gcc doesn't (https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Other-Builtins.html#Other-Builtins https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Other-Builtins....). If you look at the kinds of API functions implemented as builtins, they are generally simple.