Home / Forums / Understanding Memory Alignment, Cache Lines & Structure Padding in x86_64

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Tutorial

Understanding Memory Alignment, Cache Lines & Structure Padding in x86_64

PEHeader
PE Format & Linker Tech
VIP
Rep: 350
Join Date: Jun 2023
Posts: 35
Thanks: 95
4y ago · Mar 6, 2022 11:07 AM
#1
CPUs fetch memory in 64-byte cache lines. Struct member reordering can dramatically reduce cache misses and struct sizeof padding.
PEHeader | IMAGE_NT_HEADERS & Section Parsing
The following users thanked PEHeader for this post:
HexRays99
Senior Reverse Engineer
VIP
Rep: 380
Join Date: Apr 2022
Posts: 36
Thanks: 94
4y ago · Mar 6, 2022 1:17 PM
#2
Alignas(64) on hot variables prevents false sharing between CPU cores in multi-threaded loops.
HexRays99 · Reverse Engineering & Static Analysis
CPP
// Always check your pointers!
if (!pLocalPlayer) return;
NullPtr_
Assembly & Engine Research
MEMBER
Rep: 260
Join Date: May 2023
Posts: 27
Thanks: 62
4y ago · Mar 8, 2022 3:17 PM
#3
Compilers insert implicit padding to align 64-bit pointers to 8-byte boundaries. Reordering largest to smallest types shrinks structures.
NullPtr_ · 0xDEADBEEF was here