Re: Unnecessary struct padding
Re: Unnecessary struct padding
- Subject: Re: Unnecessary struct padding
- From: Wim Lewis <email@hidden>
- Date: Mon, 22 Aug 2011 17:00:39 -0700
On 22 Aug 2011, at 3:15 PM, Benjamin Rister wrote:
> I’ve got a situation where I’ve got a huge number of things I want to fit in memory without chomping up all of the user’s RAM, so I’m trying to optimize the data structure down as much as possible. I’m running into issues where it appears the compiler (LLVM 2.0) is padding things that don’t seem to need to be padded for alignment.
>
> The struct looks like this. I’ve marked the places where it’s being padded unnecessarily:
>
[...]
> Am I forgetting some rule about alignment or something that makes this padding legitimate or needed?
I don't know if it's required by the standard (I suspect it is), but I think it's common for compilers to pad a struct or union so that its length is a multiple of its own alignment requirement. Since your aggregates contain some pointers, which want 8-byte alignment, the aggregates get padded to a multiple of 8 bytes. The reason for this is so that you can put the structs in an array and have the offset from one array element to the next be equal to sizeof(an individual element).
I think you can save some space in your declaration by flattening it from struct{union{struct{union}}} to simply struct{union{struct}}, duplicating the common fields in each union member. If most instances of this structure use the smaller union members, you might also gain by moving the other fields into a completely separate array, tree, or hashtable which can store the sparse population of larger values efficiently.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden