Storable tuples and what is 'alignment'?

Andrew J Bromage ajb@spamcop.net
Tue, 6 Aug 2002 10:57:43 +1000


G'day all.

On Mon, Aug 05, 2002 at 05:39:19PM -0700, Hal Daume III wrote:

> 2) Could someone tell me what 'alignment' is [...]

On many architectures, data must be stored at an address which is
an integer multiple of some small number (often 2, 4 or 8, depending
on the data and on the architecture).  Even where it is not a
requirement of the architecture, it's often faster to align data
so that it does not require more than one bus cycle to fetch and/or
store, and so that it doesn't span a cache line boundary.

This number is called the "alignment", and a good rule of thumb for
computing it is:

	instance Storable a where
	    alignment a = sizeOf a `min` machine_word_size

where machine_word size is 4 for 32-bit architectures, 8 for 64-bit
architectures and so on.  Note that the above formula may not work
in some situations (e.g. when sizeOf a < machine_word_size but
sizeOf a is not a power of 2, or when a is a strange machine-specific
type, like a DMA buffer or a hardware page).

In general, you just have to know what the alignment of some type is.

Cheers,
Andrew Bromage