views:

506

answers:

3

Hi,

I was just wondering about the considerations to be followed while packing items (int, float, unions, etc) in a C structure (C struct definition ) that would allow the compiler to further optimize it.

I would like to know whether there are any guidelines that one should follow e.g. adding items to the structure in an order that would allow alignments to the word boundaries. etc. ?

details would be appreciated.

Regards, -J

The question also entails the optimization strategies towards cross compiling such C structures.

+6  A: 

If you really want to minimize space, order things from largest alignment to smallest. That should guarantee that you will get minimal padding.

MSN
A good optimizing compiler should probably do this for you, but I don't know how many will. I would assume GCC does this, but I haven't tested it.
Chris Lutz
C doesn not allowed the compiler to rearrange the members in a struct, so if you want the minimal space and maximum efficiency you'll have to do it yourself.
nos
@chris, I hope it doesn't because that would sure break by binary blob mapping!
Aiden Bell
As far as I know, compilers do not reorganize data, because it will affect mappings (as Aiden said). it will affect alignment for SIMD operations, also it may cause some problems with access using memory pointers.
Vova
Indeed. I understand why you might not want to rearrange the members, but relying on the exact memory mapping of a struct seems like a bad idea.
Chris Lutz
Thanks for all the comments. I am also trying to understand this optimality of member alignments in case when i change my platform. e.g. if i optimize it with gcc on linux, i need to understand how much change do i need to do when i compile (/cross compile ) it on say windows.. ( i am not a windows guy so that might take a little extra effort for me :-/)
Jay D
+4  A: 

If you can compile your code under gcc, you might be able to use pahole to find structures that can be improved, and automatically repack them.

Here are a couple of articles about pahole that might help you:

Hasturkun
pahole seems interesting tool. But I am not sure if i optimize my struct definitions on linux with gcc, How do i have to reconsider it when i am compiling it on say Windows in the Visual Studio environment. (i am not a windows guy so its taking me more time to understand that aspect :-/ )
Jay D
Struct padding and alignment requirements tend to be tied to the hardware platform, so GCC's output structure should be identical to the one output by VS.
Hasturkun
ohh !! I thought its compiler specific as every compiler has its own way of arranging the data structure. (every compiler tries to optimize that as per the hardware though). So if the originally compiled code optimal on a given platform NOT NECESSARILY means that cross-compiled code would be optimal.. ? right ?
Jay D
A: 

What are the requirements for your program? How many elements would you have? What kind of structures?

For example, if you have struct with several elements, and you need to traverse the array of such elements searching for something, then it is better to do not array of structs, but to do struct of arrays. That's because frequently used members will be located side-by-side, so cache memory will work fine.

If you would like to exploit SIMD (vector) operations, you should align all members by some boundaries.

It's hardly to suggest something correctly, because I don't know what do you expect from your data.

Vova