Live data from Hacker News

The Lost Art of C Structure Packing (2014)

catb.org

111–116 of 116 posts

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

#111

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…

I really don't see how that's good advice. It's paranoia, and paranoia about something (being falsely accused of sexual assault) that very rarely happens. It also strikes me as sexist. Every woman is a potential threat to you?

Its the current culture. Its current accepted to consider every man as a potential rapist, while for men, just the accusation of sexual misconduct is enough to ruin ones life for ever.

It is sexist, but its accepted sexist behavior. Those who would advocate for a less aggressive tone in gender politics get harassed by both camps.

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

#112
post #41

Earlier quoted context omitted.

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…

> false sharing

I wasn't aware of that sort of thing. any resources on learning these sorts of details of performance tuning, aside from hard-earned experience?

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

#113

Earlier quoted context omitted.

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…

> false sharing I wasn't aware of that sort of thing. any resources on learning these sorts of details of performance tuning, aside from hard-earned experience?

I mostly just vacuum up a huge amount of material from as many sources as I can. My focus on game development might help here. A lot of it flows from understanding CPUs from a low level hardware perspective - you might be able to reverse engineer this kind of knowledge from the wikipedia articles, although I haven't tried doing so myself.

E.g. the page for "CPU cache" references:

- CPUs read/write by cacheline

- Caches need to coordinate to avoid stale data through "cache coherence protocols" (which have a cost as mentioned on their own wiki page)

"False sharing" is just the interplay of those two mechanisms in worst case scenarios and such corner cases.

About the only time I've used this knowledge of false sharing has been when implementing a work-stealing task queue system. And I suppose the few times I've written a parallel for loop of some description.

Trying to think of similar performance issues to guide you towards, a few come to mind:

1) CPU caches are basically implemented as fixed sized hashmaps with a really poor hash - the address modulo some power of two, with a fixed limit of collisions supported.

http://www.lshift.net/blog/2013/10/08/cpu-cache-collisions-i...

I've never actually used this knowledge, although I could see it coming up if I were working on the design of a database's in-memory storage or something.

2) Reading "write combined" memory is really bad, including implicitly reading by failing to write entire cachelines (comes up with GPU resources such as textures)

https://fgiesen.wordpress.com/2013/01/29/write-combining-is-...

This one I'm mindful of whenever I'm porting programs to use new graphics APIs, or writing the low level systems that deal with them in the first place. I feel there's at least one more situation where write combined memory has come up for me in practice (since typical memory access is not write combined), but it escapes me at the moment. Fortunately most graphics API docs at least warn you not to read the memory they're pointing you towards, although they're not always as explicit as "memcpy entire cachelines from orbit, just to be sure."

3) Performance of atomics touching multiple cachelines is terrible, when it's even supported:

https://fgiesen.wordpress.com/2014/08/18/atomics-and-content...

Normally I find out that this has been happening when I port a program to ARM and suddenly it crashes doing some kind of atomic operation or lock, because someone reinterpreted a char buffer instead of allocating properly, because cross-cacheline atomics are too crazy for ARM to bother implementing. Things like SSE and AVX also tend to perform... not so great, unless stuff is properly aligned for them.

EDIT: I guess fgiesen is one resource, at least, as it cropped up twice trying to google for sources for the things I'm talking about ;)

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

#114
post #30

Earlier quoted context omitted.

True (for some processors), but if you are doing packaged structs that should imply you have a reason to tell the compiler you know better than it how to write the struct. That most often (in my experience) means the struct is used for data interchange (ie network or files) and so you have to pay that price for byte access because byte access is a requirement. The other time to do that you might do this is when your…

> but if you are doing packaged structs that should imply you have a reason to tell the compiler you know better I am pretty sure this could have been implemented better -- for malloc-ed and statically-allocated structs the compiler can know the alignment of the structure, so should be able to emit optimal code according to each case. Admittedly, probably a very minor improvement. It is the feeling of surprise at the…

If the struct is byte, machine word, byte, machine word there is no way to pull that off.

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

#115
post #83

esr, brilliant as always. However: since shipping the first version of this guide I have been asked why, if reordering for minimal slop is so simple, C compilers don’t do it automatically. The answer: C is a language originally designed for writing operating systems and other code close to the hardware. Automatic reordering would interfere with a systems programmer’s ability to lay out structures that exactly match t…

Brilliant, but crazy.

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

#116

Earlier quoted context omitted.

> false sharing I wasn't aware of that sort of thing. any resources on learning these sorts of details of performance tuning, aside from hard-earned experience?

I mostly just vacuum up a huge amount of material from as many sources as I can. My focus on game development might help here. A lot of it flows from understanding CPUs from a low level hardware perspective - you might be able to reverse engineer this kind of knowledge from the wikipedia articles, although I haven't tried doing so myself. E.g. the page for "CPU cache" references: - CPUs read/write by cacheline - Cach…

Thanks for the details!
Post reply on HN