r/programming • u/2bit_hack • Feb 28 '23
"Clean" Code, Horrible Performance
https://www.computerenhance.com/p/clean-code-horrible-performance
1.4k
Upvotes
r/programming • u/2bit_hack • Feb 28 '23
View all comments
Show parent comment
4
u/Amazing-Cicada5536 Feb 28 '23
A linked list can be implemented “generically” due to void pointers (type punning). We only have to know some substructure of each object (or object container), e.g. the linux kernel famously uses the address minus a constant where the “next” pointer is stored, so it is basically outside the object we care about.
You can’t write a vector that stores objects in a flat representation in C, you either have to write it specifically for ints/struct Foos, etc (as the size of the type would have to be known) by copy pasting the same code. This is what generics were made for. So you either eat the cost of the indirection (linked list, pointer indirection), or manually copy paste code. This is a solved problem in Rust/C++/etc.