r/cpp • Meeting C++ | C++ Evangelist • Sep 24 '26

About alignment, struct layout and the cache...

https://meetingcpp.com/blog/items/About-alignment--struct-layout-and-the-cache---.html
36 Upvotes

27 comments sorted by

View all comments

Show parent comment

2

u/pdp10gumby Sep 26 '26

Lists can be as dense as an array, and asymptote towards array size even when manipulated, using CDR-coding, a technique used back in the 70s.

short: instead of having a mode of value and next, you just put the values successively in memory and have a flag (ideally embedded into the value, or even easier, into the value pointer if that’s the representation’s that says, “the next value is at the next location in memory). If you want to splice a new value in or out you have to make two conventional nodes, but depending on your datastructure that may be an uncommon case.

I’ve done this in C++ when I have a free bit available in the range of the content (always true with a pointer these days, but also with, say, an enum).

There’s a crappy Wikipedia page on this topic: https://en.wikipedia.org/wiki/CDR_coding

2

u/ReDucTor Game Developer | quiz.cpp-perf.com Sep 27 '26

> asymptote towards array size

I've never used them but looking at them this wouldonly be the case if you have mostly long unblock blocks.

Additionally if you use a branch for inspect the node tag then your going to run into potential branch mispredictions, if you use branchless then you just end up with a long data dependency

2

u/pdp10gumby Sep 27 '26

Well sure, I"m not sure what your point is. As with every datastructure you have to choose based on optimal performance in a typical working set. I don't use linked lists often (few should) but of course in some cases they are the right choice, and if your use case reduces memory move upon updates that can be good or bad.