There are three common reasons for aligning memory addresses with cachelines:
- Improving Performance
- Maintaining Atomicity
- Preventing False Sharing
The third reason has been widely discussed online, so we will not cover it here. Instead, we will focus on the first two reasons.
Cross-Line Performance
Let’s start with the following program. The program first allocates an 8K memory space. It’s important to note that the starting address of the space provided by malloc
is aligned to 8 or 16 bytes. We need to manually align it to 64 bytes for easier manipulation. The allocated space is 8K, which is the size of two pages. Regardless of the starting address, this space will contain a page boundary, which is a good alignment point and useful for testing. The subsequent operations simply involve repeatedly writing data and measuring the total time to calculate the average time per operation.