Structure optimization is all about reducing the memory footprint of a structure in memory.
One important thing to keep in mind is the alignment. On 32-bit machines, it's 4 bytes, on 64-bit, it's 8 bytes.
There's an art to this, while there's rules to how structures are laid out in memory, these depend on specific compilers, ABIs, and so on. Rust would generate a different structure layout then C would, for example.
## Unpacked
An "unpacked" or unoptimized structure looks like this, with holes.
```c
// 32-bits assumed
struct test {
char c; // 1 byte, 1st byte
bool a; // 1 byte, 2nd byte
// padding: 2 bytes
int num; // 4 bytes, 5th byte...
};
```
## Strategies
* Ensure that the largest natural size of a data type is listed first in c-like languages.
* Use a pack pragma, if the language allows
* Store less in the structure itself, use a pointer to refer to another structure
* Use bitfields for large banks of flags (a char can store 8 flags)
---
# References
http://www.catb.org/esr/structure-packing/