Post

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

ArchitectureNative Size
32-bit CPU32 bits (4 bytes)
64-bit CPU64 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_t for image processing
  • uint16_t for compressed data
✔ DO
  • Use size_t for indexing
  • Use native types (int64_t on 64-bit systems)
  • Align data properly
  • Profile performance
❌ DON’T
  • Use small types blindly
  • Ignore alignment
  • Mix data types unnecessarily
FactorSmall TypeNative Type
MemoryEfficientLarger
CacheBetterWorse
CPU opsSlowerFaster
SIMDHarderEasier
This post is licensed under CC BY 4.0 by the author.