I am looking to make a hefty shift towards templates in one of my OpenGL projects, mainly for fun and the learning experience. I plan on watching the size of the executable carefully as I do this, to see just how much of the notorious bloat happens. Currently, the size of my Release build is around 580 KB when I favor speed and 440 KB when I favor size.
Yes, it's a tiny project, and in fact even if my executable bloats 10 x its size, it's still going to be 5 MB or so, which hardly seems large by today's standards... or is it? This brings me to my question. Is speed proportional to size, or are there leaps and plateaus at certain thresholds, thresholds which I should be aiming to stay below? (And if so, what are the thresholds specifically?)
What Does Code Bloat Mean? Code bloat is code that is allegedly too long or slow on most computer systems. While the term usually refers to source code that is too long, it can also refer to executables that might be considered excessively large.
Software bloat is a process whereby successive versions of a computer program become perceptibly slower, use more memory, disk space or processing power, or have higher hardware requirements than the previous version, while making only dubious user-perceptible improvements or suffering from feature creep.
Some techniques for reducing code bloat include: Code refactoring a commonly used code sequence into a subroutine, and calling that subroutine from several locations, rather than copy and pasting the code at each of those locations (copy-and-paste programming).
How does template cause the code bloat in C++? Code bloat occurs because compilers generate code for all templated functions in each translation unit that uses them. Back in the day, the duplicate code was not consolidated, which resulted in "code bloat". These days, the duplicate code can be removed at link time.
In my experience, code bloat and execution-time bloat go hand in hand, and it's all about how the software is designed, in particular, how the data structure is designed.
If one follows the approach that every mental concept becomes a class, if one follows the notification-style pattern where simply setting a property, or adding an item to a collection, can result in a hidden ripple effect of actions propogating throughout a large non-normalized data structure network in order to try to keep it consistent, then the result will be large source code and poor performance.
On the other hand, if one tries to minimize data structure and keep it normalized (as much as reasonably possible), if to a reasonable extent, inconsistency in the data structure can be tolerated and repaired on a loosely-coupled basis, and if code generation can be used so that the program is not processing information at run time that hardly ever changes and could have been handled prior to compiling, then source code will be smaller, easily developed, and efficient.
Here's a small example where a reasonably-designed program, through a series of steps, was reduced in size by a factor of four, and made faster by a factor of 40, by eliminating data structure and using code generation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With