5-3 ALIGNMENT OF DATA - IMPROVES PERFORMANCE
*********************************************
Alignment is putting data and code in the computer memory in addresses
that are more efficient for the hardware to access (at the price of
wasting some memory).
The hardware of 'modern' RISC machines reads memory in multi-byte
'chunks' usually 4 or 8 bytes long, these 'chunks' must begin at
addresses that are multiples of the 'chunk' size. Memory accesses to
misaligned addresses may cause 'bus errors'.
Older computers (VAX), can handle misaligned (byte aligned) memory
accesses, it just slows them down. Some new computers like sun4 series
and SGI can do that also using special libraries, again at the cost
of degrading performance.
Code alignment is usually controlled by the compiler and there is
little you can do about it.
Integer and float data types sizes are usually a multiple of 4 bytes,
if you have only these types you have automatically longword (32 bits)
alignment, each number will start on a longword boundary.
Longword alignment is usually sufficient, except on 64 bit machines
when using 8 bytes data types, then you will need quadword alignment.
The problems start when you use the smaller types like character or
byte. Put them at the end of declaration lists, argument lists, and
common blocks, there they will cause less harm.
A horrible little example program (of course using the non-standard
BYTE data-type and EQUIVALENCE, or the non-standard LOC() intrinsic
function (...), would result in a nicer program):
PROGRAM ALIGN
C ------------------------------------------------------------------
INTEGER
* I, J, K,
* ASCII_ARROW, ASCII_SPACE, LONG_ARROW
C ------------------------------------------------------------------
CHARACTER
* CH*1, CHAR_ARROW
C ------------------------------------------------------------------
COMMON // I, CH, J, K
C ------------------------------------------------------------------
PARAMETER(
* CHAR_ARROW = '>',
* ASCII_ARROW = 62,
* ASCII_SPACE = 32,
* LONG_ARROW = ASCII_ARROW +
* ASCII_SPACE * (256 + 256**2 + 256**3))
C ------------------------------------------------------------------
I = LONG_ARROW
J = LONG_ARROW
K = LONG_ARROW
CH = CHAR_ARROW
C ------------------------------------------------------------------
CALL SUB()
C ------------------------------------------------------------------
END
SUBROUTINE SUB()
C ------------------------------------------------------------------
CHARACTER
* ARRAY*13
C ------------------------------------------------------------------
COMMON // ARRAY
C ------------------------------------------------------------------
WRITE (*,'(1X,1A13)') ARRAY
WRITE (*,'(1X,13I1)') 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0
C ------------------------------------------------------------------
RETURN
END
If your compiler doesn't automatically aligns data, you will see that
the CHARACTER*1 variable misaligns the last two INTEGER variables.
Automatic alignment may be inhibited with a compiler switch (see the
'compiler switches' chapter).
(Excerpt from VMS documentation)
Aligned data can provide significant performance
improvements on VAX systems (up to 4 times faster) and
on AXP systems (up to 100 times faster). Unaligned data
was typical on older VAX systems where minimal consumption
of memory was very important and data was packed tightly
together, ignoring alignment issues.
If you share data between AXP and VAX systems, avoid
unaligned data structures. Use at least longword alignment
for in-memory data structures whenever possible; quadword
alignment is preferred, if possible.
If you have a real-time application or an application that
exhibits poor file I/O characteristics and shares data through
a file, you may find it advantageous to align the file data
structures.
Return to contents page