Data alignment and performance
I read an informative IBM article about data alignment and its importance when developing native code.
Data alignment is an important issue for all programmers who directly use memory. Data alignment affects how well your software performs, and even if your software runs at all. As this article illustrates, understanding the nature of alignment can also explain some of the “weird” behaviors of some processors.
Delphi (my preferred native Windows programming language) supports the $CODEALIGN compiler directive , as well as another interesting custom alignment method. The Delphi code below sets 128 byte boundaries:
procedure TForm1.Button1Click(Sender: TObject); type TFFTData = array[0..63535] of double; PFFTData = ^TFFTData; var Buffer: pointer; FFTDataPtr: PFFTData; i: integer; const Alignment = 128; // needs to be power of 2 begin GetMem(Buffer, SizeOf(TFFTData) + Alignment); try FFTDataPtr := PFFTData((LongWord(Buffer) + Alignment - 1) and not (Alignment - 1)); // use data... for i := Low(TFFTData) to High(TFFTData) do FFTDataPtr[i] := i * pi; finally FreeMem(Buffer); end; end;