Data Alignment and its Side Effects

IBM carries an interesting article on data alignment and what are its side effects.

Historically, data misalignment is known to cause the following issues:

  • Performance degradation
  • Functionally incorrect behavior
  • Panic or crash in the system
  • Silent memory corruption

Among the above, the first two are the ones that can possibly happen in the current age. The last two are legends.

Some processors will simply refuse to access memory that is not word/double word aligned (like 68000), they will cause a panic to happen. And some others simply mask the lowest bits to get a aligned address and access it – this is more crazy!!

Performance degradation can be caused in two ways – where one memory access needs to suffice, two memory accesses happen – not just that, some time is also spent in shifting/masking the unnecessary bits and combining the required bits. A more serious performance degradation happens where the processor refuses to do the alignment and instead requires OS intervention to do the needful – like what the processor does for alignment.

Functionally incorrect behavior occurs in that rare case – the article points out – where the lock is on an unaligned address and it crosses a page boundary. A memory access is no more atomic, if it crosses a page boundary. Atomicity fails.

Well, now-a-days, all of these things are taken care of by the compiler so that the programmer can focus on better things.

On a side note, it is not just intra structure padding that happens, but also inter structure padding (in an array) so that the next structure is aligned properly.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s