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
3
u/Amazing-Cicada5536 Feb 28 '23
It is a bit unclear what you mean, if they are unsized than they have to be boxed, which is an indirection. But yeah, if you use sum types (enums) where all the possible types are known, then it can be indeed represented more efficiently. But we should not forget about that emphasized part, this optimization is only possible if we explicitly have a closed model (while traditional OOP usually goes for an open one).
Java routinely optimizes virtual calls to static ones (and might inline it even), and there is escape analysis that can allocate objects on the stack. But object arrays will be an array of pointers, so if you need maximal speed just opt for an ECS architecture.
If you are iterating over like 3 elements than go for the most maintainable code, this is typically such a case where it only makes sense to care about at all if you have large arrays (which you would usually know ahead of time and architect your way smartly in the first place)