1. Problem It Solves
Performance often depends on arranging frequently processed data contiguously and communicating proven alignment to optimized code. It makes an important assumption visible and checkable.
2. Prerequisites
Arrays, pointers, loops, memory layout, and profiling basics.
You should be able to compile a short program and read its output.
3. Core Idea
The cache fetches neighborhoods, not abstract objects. Data-oriented design lays hot fields along the walking path so each fetched cache line is useful. Read std::assume_aligned as a precise promise; runtime preconditions still belong to the programmer.
4. Minimal Syntax
double* aligned = std::assume_aligned<alignof(double)>(values.data());5. How It Works
The program introduces the smallest relevant form of
std::assume_aligned.It applies the feature to fixed data while required owners remain in scope.
It prints one result that can be checked against the source.
6. Common Mistakes
assume_alignedis a promise with no runtime check; giving it a pointer that does not meet the stated alignment creates undefined behavior.Also check the required header, C++20 library support, lifetime, and deduced types.
7. When to Use It
Use it when profiling identifies a hot sequential loop and the data layout can match its access pattern.
Avoid it when no measurement supports the complexity or alignment cannot be guaranteed.
8. Simple Example
Separate contiguous coordinate arrays are traversed linearly, and only the natural double alignment is promised safely. The companion .cpp uses no input, so its result is easy to reproduce.
Complete sample code
Source file
cpp20/50_cache_locality_data_oriented_assume_aligned/main.cpp
// Day 50: Cache Locality, Data-Oriented Design, and assume_aligned
#include <iostream>
#include <memory>
#include <vector>
int main() {
std::vector<double> x{1.0, 2.0, 3.0, 4.0};
std::vector<double> y{10.0, 20.0, 30.0, 40.0};
// vector storage satisfies at least the natural alignment of double.
double* aligned_x = std::assume_aligned<alignof(double)>(x.data());
double total = 0.0;
for (std::size_t i = 0; i < x.size(); ++i) {
total += aligned_x[i] + y[i]; // Sequential, contiguous access.
}
std::cout << "total = " << total << '\n';
}
9. Key Takeaways
std::assume_alignedexpresses the central C++20 idea of this day.The example isolates one behavior with fixed data.
Compiler checks do not replace lifetime and runtime reasoning.
Prefer the smallest interface that states the real requirement.
10. Self-Check Questions
Easy — What is the main job of
std::assume_alignedin the minimal example?Medium — Why can separate arrays improve locality when a loop reads only the
xcoordinates?Hard — What exact precondition must hold before replacing
alignof(double)with a larger claimed alignment?