3 ms·
> The patten I like most is a struct of const function pointers, instantiated as a const global, then passed by address to whatever wants to act on that table
by camel-cdr 2y ago
> The patten I like most is a struct of const function pointers, instantiated as a const global, then passed by address to whatever wants to act on that table
> If you heed the "const" word above the compiler inlines through that just fine.
But only when the function it self is inlined, which you quite often don't want. If you sort integers in a bunch of places, you don't really want qsort to be inlined all over the place, but rather that the compiler creates a single specialized copy of qsort just for integers.
With something as simple as qsort compilers sometimes do the function specialization, but it's very brittle and you can't rely on it. If it's not specialized nor inlines then performamce is often horible.
IMO an additional way to force the compiler to specialize a function based on constant arguments is needed. Something like specifying arguments as "inline".
IIRC, this library uses this style of generic programming with a very nice API: https://github.com/JacksonAllan/CC https://github.com/JacksonAllan/CC
It's just unfortunate that everything needs to get inlined for good performance.
- JonChesterfield 2y agoSort of. If you want the instantiation model, you can go with forwarding functions (maybe preprocessor instantiated): void malloc_stack_drop(void *s) { stack_drop(&malloc_stack_global, s); } If you inline stack_drop into that and user code has calls into malloc_stack_drop, you get the instantiation model back. Absolutely agreed that this is working around a lack of compiler hook. The interface I want for that is an attribute on a parameter which forces the compiler to specialise with respect to that parameter when it's a compile time constant, apply that attribute to the vtable argument. The really gnarly problem in function specialisation is the cost metric, the actual implementation is fine - so have the programmer mark functions as a good idea while trying to work out the heuristic. Going to add that to my todo list, had forgotten about it.