optimizationtypesprocessor

Should I use the smallest type possible?


A long time ago I remember reading that you should always use the smallest possible type to store your data, but nearly every piece of code I read doesn't do this. They often use 32 bit integers all over the place.

I've heard the rationale that a 32 bit value is fetched as fast as an 8 bit value, but processors have some way of fetching several smaller values at once.. Right?

So if I use 4 bytes instead of 4 integers, shouldn't the compiler be able to optimize this so the 4 bytes is fetched/stored in a single 32 bit register?

Or is all of this really just premature optimization and the potential performance gain is negligible?


Solution

  • Premature optimization indeed! However, once you are optimising, it also depends on your architecture. For example, on ARM, memory accesses must be 32-bit aligned (some instructions can do this, but they just do a 32-bit access then mask/shift behind the scenes). If you use a byte, a compiler will often give each 'byte' four actual bytes of RAM so it can be accessed faster (not to mention the CPU will freak outif you try to access unaligned bytes without special code to handle them).

    There is an argument to using 'int' for everything since it's the CPU's preferred size, but basically, just use the type of the size you need, and let the compiler worry about optimisations :D