Live data from Hacker News

The Lost Art of C Structure Packing (2014)

catb.org

91–100 of 116 posts

Re: The Lost Art of C Structure Packing (2014)

#91

Sigh... apparently this is now enough of rocket science to be considered for posting on HN... People using packing pragma of GCC should also beware -- an access to a field of a packed structure will be done bytewise, whether a variable happens to be actually aligned or not (I guess the compiler simplified its life by assuming no variable is ever aligned in packet structs), so memory size would go down but CPU use mig…

On Intel CPUs there's virtually no cost for misaligned access. Of course, if your misalignment results in your data spanning 2 cache lines instead of 1, that'll be costly, but that's regardless of alignment.

Re: The Lost Art of C Structure Packing (2014)

#92
post #41

TL;DR: to get smaller structures, don't do stuff like this: struct foo { char a; int b; char c; int d; }; but this: struct foo { int b; // or int b, c; int c; char a; char c; }; Basically if you sort the types by size in reverse descending order, you get optimal packing without messing with compiler-specific packing extensions that skew alignment and possibly bloat code. The worst that you will get is padding at the…

Alright this might be a stupid question, but if packing is about the order in which the fields are arranged... shouldn't the compiler optimize that? I mean, it does much more complex optimizations already doesn't it?

Then the memory layout of the struct will be different. So when you serialise it read it back it would be different, if you perhaps used a different non optimising compiler.

Re: The Lost Art of C Structure Packing (2014)

#93

Earlier quoted context omitted.

It's not guaranteed (as in "money back" or whatever). It's merely required by ISO C for conformance. If a struct is declared like this: struct foo { whatever_type_t first_member; // ... }; then an array of this type can also be declared: struct foo farray[42]; A conforming ISO C implementation has to ensure that farray[1].first_member, and farray[2].first_member, and so on, are all allocated such that they meet the a…

> A conforming ISO C implementation has to ensure that farray[1].first_member, and farray[2].first_member, and so on, are all allocated such that they meet the alignment requirements fro whatever_type_t. It cannot be that farray[0].first_member is accessible, but farray[1].first_member throws an alignment exception or whatever. Programmers should not have to do anything to ensure this. The alignment requirement is pl…

I like how I'm getting downvoted, yet nobody is telling me how I'm wrong.

Re: The Lost Art of C Structure Packing (2014)

#94
post #84

ESR is a good programmer, and this is a handy guide for those who don't know how to pack structures. He's also insane, so don't trust anything he says. Oh, you don't think he's insane? He thinks there's a conspiracy amongst women in open source to discredit Linus Torvalds. No, I'm not joking. I wish I was.

Here are ESR's words on the subject: http://esr.ibiblio.org/?p=6907

However, ignore the way it's worded and the overall message is good advice whether there's a conspiracy or not (which I agree, there is likely not). If you're a male whose name I'd likely recognize, the only woman with whom you should ever be in a room alone is your SO. Someone might still accuse you of sexual assault, but it'll be pretty darned hard to prove if it can never be demonstrated that the two of you were alone together. Got the idea from the Rev. Billy Graham. Ever notice that no one (to my knowledge) accused him of sexual assault while his peers (for lack of a more accurate word) were making the news regularly in the 90s? Because he did exactly that.

Maybe it sounds harsh or paranoid, I dunno, but it's not that hard to implement and is easier than dealing with being falsely accused, no matter how small the odds.

Re: The Lost Art of C Structure Packing (2014)

#95
post #72

The bit-field feature of C structs is underutilized. People are still writing hex constants and using AND and OR to clear and set bits. Let the compiler do that; it's more readable. As of C99, there's named structure initialization, which makes bitfield constants more readable.

Very annoying when you want to pass the flags field to a function, however.

Right, because a structure initializer in C is not a first-class object. You can't use it in an expression.

Re: The Lost Art of C Structure Packing (2014)

#96
post #84

Earlier quoted context omitted.

Here are ESR's words on the subject: http://esr.ibiblio.org/?p=6907

However, ignore the way it's worded and the overall message is good advice whether there's a conspiracy or not (which I agree, there is likely not). If you're a male whose name I'd likely recognize, the only woman with whom you should ever be in a room alone is your SO. Someone might still accuse you of sexual assault, but it'll be pretty darned hard to prove if it can never be demonstrated that the two of you were a…

Well, yes. It's the conspiracy part that's insane.

Re: The Lost Art of C Structure Packing (2014)

#97
post #41

TL;DR: to get smaller structures, don't do stuff like this: struct foo { char a; int b; char c; int d; }; but this: struct foo { int b; // or int b, c; int c; char a; char c; }; Basically if you sort the types by size in reverse descending order, you get optimal packing without messing with compiler-specific packing extensions that skew alignment and possibly bloat code. The worst that you will get is padding at the…

Alright this might be a stupid question, but if packing is about the order in which the fields are arranged... shouldn't the compiler optimize that? I mean, it does much more complex optimizations already doesn't it?

Aside from the ABI, file I/O, and memory mapping concerns voiced by others, it's also not always a performance optimization: Occasionally you really want two members of a struct to e.g. reside in different cachelines to avoid "false sharing" murdering your performance in multithreaded code (where multiple cores repeatedly fight over who owns / can update the cacheline in question.)

Modern compilers all have warnings you can opt into, to alert you when the compiler inserts implicit padding, if you want to ensure things get packed nicely.

Re: The Lost Art of C Structure Packing (2014)

#98

Earlier quoted context omitted.

However, ignore the way it's worded and the overall message is good advice whether there's a conspiracy or not (which I agree, there is likely not). If you're a male whose name I'd likely recognize, the only woman with whom you should ever be in a room alone is your SO. Someone might still accuse you of sexual assault, but it'll be pretty darned hard to prove if it can never be demonstrated that the two of you were a…

Well, yes. It's the conspiracy part that's insane.

LOL. Okay, fair enough. I got so stuck on "but that's good advice for anybody!" that I apparently had a huge blind spot for the whack-a-doodle basis for this advice.

Re: The Lost Art of C Structure Packing (2014)

#99
post #32

In this thread: people who do this for a living making people who don't feel dumb. Guys, it's called "The Lost Art of C Structure Packing" for a reason. Python and Ruby guys have no idea. Even most C or C++ devs wouldn't know. Take a chill pill.

I learned it a few years ago in undergrad..
Post reply on HN