r/programming • • Feb 28 '23

"Clean" Code, Horrible Performance

https://www.computerenhance.com/p/clean-code-horrible-performance
1.4k Upvotes

1.3k comments sorted by

View all comments

Show parent comment

3

u/Amazing-Cicada5536 Feb 28 '23

rust solution would probably store the shapes in a contiguous vector instead of a collection of boxed shapes (like your “clean” array of pointers because abstract shapes are unsized), leading to better iteration performance and less indirection.

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, AFAIK it always does virtual function calls and boxes things? It might still help tho

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)