Post

Data Type Size Optimization

Data Type Size Optimization

Data Type Size Optimization in C++ (Use Only What You Need)


Prerequisites


1. Why Data Type Size Matters

Choosing unnecessarily large data types can lead to:

Memory efficiency directly impacts CPU performance.

  • Increased memory usage
  • Poor cache utilization
  • Lower performance

Use the smallest data type that satisfies your requirements

1-1. Array Optimization

❌ Inefficient
1
int arr[1000000];  // 4MB
1
64 / 4 = 16 elements per cache line
✔ Optimized
1
uint8_t arr[1000000];  // 1MB
1
64 / 1 = 64 elements per cache line
  • 4x less memory
  • Better cache performance
Cache Impact

CPU cache line ≈ 64 bytes More data per cache line → fewer cache misses

1-2. Struct Optimization

❌ Wasteful
1
2
3
4
5
struct Data {
    int a;
    int b;
    int c;
};
✔ Optimized
1
2
3
4
5
struct Data {
    uint8_t a;
    uint8_t b;
    uint8_t c;
};

👉 Significant memory reduction when repeated many times

❗ Alignment & Padding
1
2
3
4
5
struct A 
{
    uint8_t a;
    int b;
};

Padding added → may reduce benefit

❗ CPU Efficiency
  • Some CPUs are optimized for 32-bit or 64-bit operations
  • Too small types may require extra instructions
When Optimization Matters

✔ Large arrays / datasets
✔ Performance-critical loops
✔ Embedded systems
✔ Game / simulation engines

2. Trade-offs

✔ DO

  • Choose smallest sufficient type
  • Use fixed-width types (uint8_t, uint32_t)
  • Profile memory-heavy code

❌ DON’T

  • Use int by default everywhere
  • Over-optimize prematurely
  • Ignore alignment and padding
FactorSmall TypeLarge Type
MemoryEfficientWasteful
CacheBetterWorse
RangeLimitedLarge
CPU opsSometimes slowerOften faster
This post is licensed under CC BY 4.0 by the author.