Neal Becker wrote:
> Martin C. Martin wrote:
> 
>>
>> Robert Bradshaw wrote:
>>> On Apr 9, 2008, at 8:25 AM, Martin C. Martin wrote:
>>>> Hi,
>>>>
>>>> There's a potential optimization I mentioned on the Lisp inspired
>>>> transforms page, where you could reorder bitfields in order to pack them
>>>> most efficiently.  Eerily, someone at my job just committed something
>>>> that did just that.  We have a custom defstruct, called defstruct-bv,
>>>> which allows you to specify bit fields.  (Lisp doesn't come with
>>>> bitfields.)  Here's the checkin message:
>>>>
>> ...
>>> I believe in Cython we would just rely on the C compiler to make
>>> optimizations of this nature.
>> That's the point, C prescribes that it *can't* make such optimizations.
>>   C fields (including bitfields) must appear in memory in the order they
>> appear in the source code.
>>
>> For bit fields, this is an advantage when you're, say, parsing the
>> header of some binary file, or writing a device driver.  But if you just
>> want a struct that stores some data compactly, the C compiler can't help
>> you out in that case.
>>
>> Best,
> 
> What about gcc's 'packed' attribute?
> 
> `packed'
>      The `packed' attribute specifies that a variable or structure field
>      should have the smallest possible alignment--one byte for a
>      variable, and one bit for a field, unless you specify a larger
>      value with the `aligned' attribute.
> 
>      Here is a structure in which the field `x' is packed, so that it
>      immediately follows `a':
> 
>           struct foo
>           {
>             char a;
>             int x[2] __attribute__ ((packed));
>           };

Access to non-word aligned ints can be very slow, that's why packed 
isn't the default.

The use case is more like:

struct foo {
    int a;
    void *p;
    int b;
}

On a 64 bit machine with 32 bit ints, that structure takes up 3 words 
(24 bytes).  Instead, if you wrote it as:

struct foo {
    int a;
    int b;
    void *p;
}

Everything is properly aligned, and it only takes 2 words (16 bytes).

Best,
Martin
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to