You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@harmony.apache.org by "Geir Magnusson Jr." <ge...@apache.org> on 2005/10/05 14:32:52 UTC

opinions on structure packing in C?

I'm working with Dan's bootVM to get to run on Windows, and have been  
thinking about the use of -fpack-struct vs #pragm pack() vs just not  
packing and writing code that is a little slower, and a little more  
verbose, but seemingly more robust and maintainable due to less  
information about context needed to avoid surprises.

I've reconfirmed that packing structs is deadly when you want to call  
into standard libs

struct stat foo;

stat("foo.txt", &foo);

does some bizarre things :)

So how do people deal with this these days?  I know fashions change,  
and I haven't done C in anger for > 6 years...  (I still have the  
shakes when looking at it - I'm past the screaming stage...)

geir

-- 
Geir Magnusson Jr                                  +1-203-665-6437
geirm@apache.org



Re: opinions on structure packing in C?

Posted by Archie Cobbs <ar...@dellroad.org>.
Robin Garner wrote:
>>So how do people deal with this these days?  I know fashions change,
>>and I haven't done C in anger for > 6 years...  (I still have the
>>shakes when looking at it - I'm past the screaming stage...)
> 
> 
> "Premature optimization is the root of all evil."

Agreed.. it's way too early to worry about that kind of optimization,
especially since that one should be easy to retrofit later.

-Archie

__________________________________________________________________________
Archie Cobbs      *        CTO, Awarix        *      http://www.awarix.com

Re: opinions on structure packing in C?

Posted by "Geir Magnusson Jr." <ge...@apache.org>.
On Oct 5, 2005, at 9:04 AM, Robin Garner wrote:

>
>
>> So how do people deal with this these days?  I know fashions change,
>> and I haven't done C in anger for > 6 years...  (I still have the
>> shakes when looking at it - I'm past the screaming stage...)
>>
>
> "Premature optimization is the root of all evil."
>
> If struct packing produces a measurable speedup, do it; if not, don't.
> But actually take the time to measure it.
>

I wasn't clear, I guess.  I'm not optimizing.  I'm looking at code  
that already does this through global pack-struct flag for the  
compiler, and I needed to get away from that w/o doing a rewrite.

geir


-- 
Geir Magnusson Jr                                  +1-203-665-6437
geirm@apache.org



Re: opinions on structure packing in C?

Posted by Robin Garner <ro...@anu.edu.au>.
> So how do people deal with this these days?  I know fashions change,
> and I haven't done C in anger for > 6 years...  (I still have the
> shakes when looking at it - I'm past the screaming stage...)

"Premature optimization is the root of all evil."

If struct packing produces a measurable speedup, do it; if not, don't. 
But actually take the time to measure it.

Robin


Re: opinions on structure packing in C?

Posted by "Geir Magnusson Jr." <ge...@apache.org>.
On Oct 5, 2005, at 9:44 AM, Tim Ellison wrote:

> Geir Magnusson Jr. wrote:
>
>> I'm working with Dan's bootVM to get to run on Windows, and have been
>> thinking about the use of -fpack-struct vs #pragm pack() vs just not
>> packing and writing code that is a little slower, and a little more
>>
>
> I'd dispute that packing always produces faster code, but...

I wasn't suggesting the packing itself made things faster, but if a  
1000 field structure :b maps directly onto a buffer, memcpy() is your  
friend...

>
>
>> verbose, but seemingly more robust and maintainable due to less
>> information about context needed to avoid surprises.
>>
>> I've reconfirmed that packing structs is deadly when you want to call
>> into standard libs
>>
>> struct stat foo;
>>
>> stat("foo.txt", &foo);
>>
>> does some bizarre things :)
>>
>> So how do people deal with this these days?  I know fashions change,
>> and I haven't done C in anger for > 6 years...  (I still have the
>> shakes when looking at it - I'm past the screaming stage...)
>>
>
> Just use the compiler's default (ANSI) layout behavior, and then
> explicitly pack a struct using pragmas only where required -- usually
> for layout compatibility with an external binary.
>
> As you illustrate above, packing somebody else's struct definition can
> bring you into conflict with their binary's view of the world.
>

Yep - that's what I've done w/ bootJVM, and it works just fine so far..

geir

-- 
Geir Magnusson Jr                                  +1-203-665-6437
geirm@apache.org



Re: opinions on structure packing in C?

Posted by Tim Ellison <t....@gmail.com>.
Geir Magnusson Jr. wrote:
> I'm working with Dan's bootVM to get to run on Windows, and have been 
> thinking about the use of -fpack-struct vs #pragm pack() vs just not 
> packing and writing code that is a little slower, and a little more 

I'd dispute that packing always produces faster code, but...

> verbose, but seemingly more robust and maintainable due to less 
> information about context needed to avoid surprises.
> 
> I've reconfirmed that packing structs is deadly when you want to call 
> into standard libs
> 
> struct stat foo;
> 
> stat("foo.txt", &foo);
> 
> does some bizarre things :)
> 
> So how do people deal with this these days?  I know fashions change, 
> and I haven't done C in anger for > 6 years...  (I still have the 
> shakes when looking at it - I'm past the screaming stage...)

Just use the compiler's default (ANSI) layout behavior, and then
explicitly pack a struct using pragmas only where required -- usually
for layout compatibility with an external binary.

As you illustrate above, packing somebody else's struct definition can
bring you into conflict with their binary's view of the world.

Tim

> geir
> 

-- 

Tim Ellison (t.p.ellison@gmail.com)
IBM Java technology centre, UK.

Re: opinions on structure packing in C?

Posted by Enrico Migliore <en...@fatti.com>.
> I'm working with Dan's bootVM to get to run on Windows, and have been  
> thinking about the use of -fpack-struct vs #pragm pack() vs just not  
> packing and writing code that is a little slower, and a little more  
> verbose, but seemingly more robust and maintainable due to less  
> information about context needed to avoid surprises.
>
> I've reconfirmed that packing structs is deadly when you want to call  
> into standard libs
>
> struct stat foo;
>
> stat("foo.txt", &foo);
>
> does some bizarre things :)
>
> So how do people deal with this these days?  I know fashions change,  
> and I haven't done C in anger for > 6 years...  (I still have the  
> shakes when looking at it - I'm past the screaming stage...)
>
> geir
>
hi,

   We should compile the code using the same C flags that the libc uses.

    Parameter marshalling is another source of problems: if we want to 
optimize
    our C code for speed (parameters passed through registers), we will 
have to recompile the libc.

 Enrico