[Python-ideas] improving C structs layout

MRAB python at mrabarnett.plus.com
Wed May 8 19:10:39 CEST 2013


On 08/05/2013 17:17, Andrew Barnert wrote:
> On May 8, 2013, at 6:52, Antoine Pitrou <solipsis at pitrou.net> wrote:
>
>> On Wed, 08 May 2013 15:45:09 +0200
>> Alfredo Solano <asolano at icai.es> wrote:
>>> Hi,
>>>
>>> Interesting observation, but isn't C struct alignment platform/compiler
>>> dependent?
>>
>> The ABIs are standardized, so I would answer no.
>
> What standard are you talking about? There's certainly no ABI standard that covers both Win64 and ARM7 Linux.
>
> There are very definitely different packing and alignment rules for different platforms that python runs on.
>
>> Even if they weren't, there are common sense rules to minimize padding,
>> such as to put fields of the same width next to each other (e.g. put
>> chars together instead of intermingling them with ints and floats).
>
> This is true... But you have to keep in mind that the width of different types is itself platform-dependent. If you've got an int, a long, a pointer, and a double, how do you pack them in a way that makes sense for all platforms, or even just the big 3 of x86_64 Mac/Linux/BSD, Win64, and Win32?
>
> All that being said, I think the right thing is to abandon the pretense of portability and look at the actual platforms that matter, and prioritize accordingly. If something is ideal on both Win32 and x86_64 Mac/Linux/BSD, good on Win64, no worse than today on ARM7 and x86 Linux/BSD, but worse on, say, 32-bit PowerPC Linux/AIX... That's probably a tradeoff worth having, right?
>
If you're dealing with members whose sizes are powers of 2, I think
that ordering them in decreasing size works pretty well. In fact, I
think that it might be a good compromise even if the sizes of some of
the members aren't a power of 2.



More information about the Python-ideas mailing list