开发者

Counting overhead due to packing in C (gcc/g++)

I'd like to count/sum up the overhead in an object file due to packing (and, ideally, have gcc minimize it for me).

For example, consider the following structure (32 bit x86, gcc):

struct a { 
    uint8_t a_char;
    uint32_t an_integer
    uint8_t another_letter;
};

While the actual data only takes up 6 bytes, the structure takes up 12 bytes in memory because both chars are followed by 3 padding bytes. By reordering the structure as follows:

struct b { 
    uint32_t an_integer
    uint8_t a_char;
    uint8_t another_letter;
};

The structure will only have sizeof(struct b) == 8 (still 4 bytes of overhead).

(1) Ideally, I'd like for gcc to rearrange struct a to struct b and save me the space, but my version (4.2) doesn't seem to do this for any optimization level.

(2) Alternatively, given struct a, I'd like to (automatically) get either the number 6 (total amount of overhead) or 4 (minimal amount of overhead, if members are ordered "ideally"). The purpose of this is to determine whether or not manually reordering structures is worth the time (likely not).

开发者_开发百科

Is there a way for gcc to do (1), and is there a tool that would perform (2)? The closest thing I can think of for (1) is #pragma pack(1), but (I'm guessing) it would have serious performance implications by making most/all memory accesses unaligned. For (2), I'm thinking a Perl script parsing debugging symbols might be able to do this, but I'm not familiar enough with DWARF to know for sure.


For #1, the reason it's not done is that both the C and C++ standards prohibit structure member reordering.

Yes, struct packing will generally reduce performance. And, as mentioned in a comment, in some cases on non-x86 architectures you can get a SIGBUS if you try to operate on a member.

For #2, yes a perl script might be able to do it. Instead of parsing DWARF info, you could try scanning the source code for struct definitions, and maybe generate some small test programs to check the sizeof() of structs and members and so on.


In linux there is a tool called pahole that will parse an ELF file with debug information and printout for each struct what each of the member's alignment is and how much padding is done by the compiler. You can use that information to guide you into manually packing if you notice that there is too much overhead.


This cannot always be done the problem is that access to unaligned memory isn't allowed on all architectures. This also simplifies some code for the compiler allowing to optimize certain accesses on the memory itself. Also reorganizing structures is probably not worth your time as 4-8 bytes of overhead per structure isn't a large deal unless you're running VERY memory sensitive software. As for your question I'm not sure if there is a way but I'm sure if someone knows they will let you know (maybe gcc-4.6 with highest optimization flag does it?)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜