Data Alignment Questions

Started by
2 comments, last by hughiecoles 15 years, 3 months ago
Hello all, Yet again I am reading another book. This one happens to be a beginner's guide to shaders. One of the things the author starts to say, and rather emphasizes, is proper data alignment. So, I guess my questions are: 1. Since this book is a little old, is this still an important topic for programming in general (to include regular apps not just games)? 2. I guess I could also ask what exactly is meant by it, even though I am going to google here in just a second, but maybe a summary of the idea... 3. Related to question #1, if it is important, but not necessarily all the time, is it an issue related to project/code size and how much data is used? In other words, when is it a concern to make your code 'properly data aligned?' Thanks for any insight! 4. One more question for good measure, does Visual Studio automatically try to accomplish data alignment, is it a compiler option or a coding practice? 5. Well crap, ok ok, one more question, is data alignment language specific, since this book mainly deals with C/C++ code?
Advertisement
1. Yes it has important implications in all kinds of applications

2. it's the practice of organizing data in a way that the processor can fetch it in the most efficient way possible. CPU's are designed go grab data at certain intervals of memory, for instance, a 32 bit processor will grab data from memory slots 0,3,7,11, etc(every 4 bytes), and reads the 4 bytes from there, so if you have a 32 bit integer stored across locations 3-6, the processor can fetch it in one try, if it's stored across locations 4-7, it will take twice as many tries, because it must read the block 3-6, then 7-10 to get the last byte, so compilers organize data(at the expense of a byte or 2 of ram wasted) in a way that the processor can access it well

3. It is generally a better idea to align data properly and sacrifice a bit of space, because the gains far out-weigh the loss of memory

4. yes, most compilers do automatic data alignment, but it can also be overridden


5.well not really, it affects the assembly code the compiler generates, but since somewhere along the line every single language is interpreted as assembly/machine code, no it's not language specific, it just tends to be more visible in some languages


EDIT: in the case of C/C++, you way you organize data within a struct can affect whether or not auto-data alignment is needed, for instance, if you're running a 32-bit operating system

//this is a well-aligned struct, because from the beginning of it, they break well into chunks of 4 bytes well
struct
{
int a; //usually 4 bytes
char b; // 1 byte
char c; // 1 byte
short d; // usually 2 bytes
};


//assuming this struct was started on a nice even boundry like 0, b stretches across a boundry of 4 (bytes 1-4) so while it will only take one access to get a or c, it would take 2 to get b.
struct
{
char a; //1 byte
int b; // 4 bytes
short c; // usually 2 bytes

};


I apoligize in advance if I eplained it poorly (keep in mind not all these numbers are correct, but it will give you a general idea of the situation)
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
Quote:Original post by godsenddeath
struct
{
int a; //usually 4 bytes
char b; // 1 byte
char c; // 1 byte
short d; // usually 2 bytes
};

struct
{
char a; //1 byte
int b; // 4 bytes
short c; // usually 2 bytes

};


sry this is just wrong. on a normal IA32 system the second struct would look like this in memory:
struct
{
char a; //1 byte
3 bytes alignment data
int b; // 4 bytes
short c; // usually 2 bytes
2 bytes alignment data
} total size 12bytes

because 4 byte data type get aligned to multiples of 4 in the structure, unless other specified...
Quote:Original post by sisifus
Quote:Original post by godsenddeath
struct
{
int a; //usually 4 bytes
char b; // 1 byte
char c; // 1 byte
short d; // usually 2 bytes
};

struct
{
char a; //1 byte
int b; // 4 bytes
short c; // usually 2 bytes

};


sry this is just wrong. on a normal IA32 system the second struct would look like this in memory:
struct
{
char a; //1 byte
3 bytes alignment data
int b; // 4 bytes
short c; // usually 2 bytes
2 bytes alignment data
} total size 12bytes

because 4 byte data type get aligned to multiples of 4 in the structure, unless other specified...



i was simply showing what a poorly aligned struct would look like, without a compiler aligning the data automaticlly, to demonstrate why it's important
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried

This topic is closed to new replies.

Advertisement