r/programminghorror • • Jul 05 '26

c Enterprise level C code

Post image
347 Upvotes

57 comments sorted by

View all comments

Show parent comment

3

u/tstanisl Jul 05 '26 edited Jul 05 '26

It's not that I think that your solution is wrong. It is convenient, it's extensible, but a bit slow due to extra indirection. Usually, the performance difference is negligible for high-level abstractions like UI.

The container_of compiles essentially to "subtracting constant from a pointer", a single line of register-only assembly. Moreover, the macro does type checking, thus it is safer than a plain void*. (btw.. type-safe container_of can be written in C89-compliant way).

This pattern works best for static-inheritance but it lets use type-safe dynamic inheritance if necessary. Thus, imo, it is more suited to low-level and high-performance systems.

EDIT. Added "not"

1

u/TheChief275 Jul 05 '26

Oh yeah no this isn't meant to be fast or production ready! This isn't a testament to the speed of V-Table generation. You can embed them (removing an indirection) depending on the size of the V-Table. They are just, again, something I don't like to write manually because of the boilerplate

1

u/tstanisl Jul 05 '26

I mean "It's not that I think that your solution is wrong", sorry for critical typo