[Python-ideas] improving C structs layout

Antoine Pitrou solipsis at pitrou.net
Wed May 8 18:59:09 CEST 2013


On Wed, 8 May 2013 09:17:27 -0700
Andrew Barnert <abarnert at yahoo.com> 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.

Per-platform ABI standards. Compilers aren't generally free to invent
things if they want to be interoperable with each other.

> 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?

Well, you can be sure that int <= long, and in most cases you can assume
other inequalities such as int <= pointer and int <= double (and even
pointer <= double). The long <=> pointer relationship is less
predictable, but on common platforms long <= pointer.

So, double then pointer then long then int.

(of course, there may be other concerns such as ensuring proximity of
fields which are often looked up together)

Regards

Antoine.





More information about the Python-ideas mailing list