Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is memory alignment required? [duplicate]

Possible Duplicate:
Purpose of memory alignment

I read some articles on net about memory alignment and could understand that from properly aligned memory (take 2-byte alignment) we can fetch data fastly in one go.

But if we have memory like a single hardware piece, then given an address, why cannot we read 2-byte directly from that position. like: enter image description here

I thought over it. I think that if the memory is in odd-even banks kind of then the theory would apply.

enter image description here

What am i missing ?

like image 575
Ashish Negi Avatar asked Oct 14 '12 10:10

Ashish Negi


People also ask

Why is memory alignment required?

The CPU can operate on an aligned word of memory atomically, meaning that no other instruction can interrupt that operation. This is critical to the correct operation of many lock-free data structures and other concurrency paradigms.

Does alignment matter memory?

Yes both alignment and arrangement of your data can make a big difference in performance, not just a few percent but few to many hundreds of a percent. Take this loop, two instructions matter if you run enough loops. A performance test you can very easily do yourself.

What does it mean for memory to be aligned?

Alignment refers to the arrangement of data in memory, and specifically deals with the issue of accessing data as proper units of information from main memory. First we must conceptualize main memory as a contiguous block of consecutive memory locations. Each location contains a fixed number of bits.

What is misaligned memory access?

Unaligned memory accesses occur when you try to read N bytes of data starting from an address that is not evenly divisible by N (i.e. addr % N != 0). For example, reading 4 bytes of data from address 0x10004 is fine, but reading 4 bytes of data from address 0x10005 would be an unaligned memory access.


1 Answers

Your pictures describe how we (humans) visualize computer memory.

In reality, think about memory as huge matrix of bits. Each matrix column has a "reader" attached that can read/write any bit from this column. Each matrix row has a "selector", which can select the specific bit that the reader will read/write.

Therefore, this reader can read the whole selected matrix row at once. Length of this row (number of matrix columns) define how much data can be read at once. For instance, if you have 64 columns, your memory controller can read 8 bytes at once (it usually can do more than that though).

As long as you keep your data aligned, you will need less of these memory accesses. Even if you need to read just two bits, but they are located on different rows, you will need two accesses to memory instead of one.

Also, there's a whole aspect of writing, which is a different problem.

Just as you can read the whole row, you also can write the whole row. If your data isn't aligned, when you write something that is not a full row, you will need to do read-modify-write (read the old content of the row, modify the relevant part and write the new content).

like image 70
kliteyn Avatar answered Sep 21 '22 00:09

kliteyn