Cache Line
A cache line is the smallest unit of data that can be transferred between the main memory (RAM) and the CPU cache. Common cache line sizes are 32, 64 and 128 bytes
Resources
- http://www.nic.uoregon.edu/~khuck/ts/acumem-report/manual_html/ch03s02.html
- https://stackoverflow.com/questions/3928995/how-do-cache-lines-work
Modern PC memory modules transfer 64 bits (8 bytes) at a time, in a burst of eight transfers, so one command triggers a read or write of a full cache line from memory.
- So in total, we have 8bytes * 8 = 64 bytes at a time sent (8 double-words)
This is from memory → cache
Consider this
double x[64];
cout << x[0] << endl; // Your computer reads full cache line, so x[0] - x[7] is cached
A cache line is 64 bytes, so in this case, it will cache values x[0]
to x[7]
.
See Memory Alignment
https://x.com/seatedro/status/1874668513719464056/photo/1
- This is bad for example, because you would need to read the struct 3 times, as opposed to twice if you had written it
“The better question would be to not tell them that anything is wrong, rather ask them what the size of this struct is in memory, whether or not it is possible to reduce its memory footprint, if possible how can you reduce the memory footprint of the struct, and the performance implications alignment has when it comes to program execution”
struct {
a: u32,
b: u64,
c: u32,
}
struct {
a: u32,
b: u32,
c: u64,
}