Data Type Optimization Based on CPU Architecture
Data Type Optimization Based on CPU Architecture
Data Type Optimization Based on CPU Architecture (C++)
Prerequisites
1. Why CPU Architecture Matters
Modern CPUs are optimized for specific data widths and alignment.
Choosing the right data type improves both performance and memory efficiency.
Using mismatched data types can cause:
- Slower execution
- Extra instructions
- Cache inefficiency
Use data types that match the native word size of the CPU
| Architecture | Native Size |
|---|---|
| 32-bit CPU | 32 bits (4 bytes) |
| 64-bit CPU | 64 bits (8 bytes) |
Most modern systems = 64-bit
2. Optimal Data Types
✔ On 64-bit CPU
1
2
int64_t a; // optimal
size_t b; // optimal
Matches CPU register size
❌ Suboptimal
1
int8_t a;
May require:
- Extra masking
- Additional instructions
CPUs operate on registers:
- 64-bit CPU → 64-bit registers
- Smaller types often promoted internally
Example
1
2
uint8_t a, b;
uint8_t c = a + b;
Internally:
- Promoted to
int - Then truncated back
2-1. Alignment Consideration
✔ Proper alignment
1
int64_t a;
Aligned → fast access
❌ Misaligned
1
2
3
4
5
6
#pragma pack(1)
struct A
{
char c;
int64_t x;
};
Misaligned access → slower
2-2. SIMD & Vectorization
SIMD prefers aligned, consistent data types
1
float arr[8];
Works well with AVX (256-bit)
❌ Mixed / irregular layout
1
2
3
4
5
struct A
{
char c;
float x;
};
Hard to vectorize
When Smaller Types Are Better
✔ Large arrays (memory-bound)
✔ Cache-sensitive workloads
uint8_tfor image processinguint16_tfor compressed data
✔ DO
- Use
size_tfor indexing - Use native types (
int64_ton 64-bit systems) - Align data properly
- Profile performance
❌ DON’T
- Use small types blindly
- Ignore alignment
- Mix data types unnecessarily
| Factor | Small Type | Native Type |
|---|---|---|
| Memory | Efficient | Larger |
| Cache | Better | Worse |
| CPU ops | Slower | Faster |
| SIMD | Harder | Easier |
This post is licensed under CC BY 4.0 by the author.