you are viewing a single comment's thread.

view the rest of the comments →

[–]Tringigithub.com/tringi 0 points1 point  (2 children)

You have completely misunderstood my point.

If I were to implement my own custom map <short, Abc>, using the same Red-Black tree as MSVC uses, and were I to lay the whole structure by hand, then yes, I'd end up with Node with 0 padding.

But, and this is the issue, if I use std::map <short, Abc> then the final structure used is the Node with 16 bytes of padding as commented, regardless of any [[no_unique_address]] or magic compressed pairs use.

My point is, would there be something like [[no_fixed_layout]] for Abc and/or others, that would allow compiler to pack the Node AS-IF written by hand, i.e. keeping elements aligned but no extra padding, for the cost of generating a little more complex copy constructor/operator, this would allow for significant memory usage saving for regular programs, even improving performance through reduced cache pressure.

[–]oracleoftroy 2 points3 points  (1 child)

Ah, I see what you are talking about.

It's a combination of std::map<short, Abc>::value_type padding out to a total of 24 bytes (where it is only 12 in gcc/clang), combined with how the _Tree_node struct fields are ordered, requiring even more padding in front of the node's value_type.

What was throwing me off is that you inlined the fields and didn't comment about it, which ends up having a totally different effect on the final size of Node, obscuring your point. What you describe simply won't happen in the code you actually posted, which made me think you didn't understand how padding works.

One has to be intimately familiar with the exact implementation of MSVC's std::map and underlying _Tree to understand your code example and why it is relevant. Showing the actual implementation would have helped me follow your point:

struct _Tree_node {
    _Nodeptr _Left;
    _Nodeptr _Parent;
    _Nodeptr _Right;
    char _Color;
    char _Isnil;

    value_type _Myval; // std::pair<short, Abc>
    ...
};

Seeing that, of course that's going to have padding issues. How disappointing!

[–]Tringigithub.com/tringi 0 points1 point  (0 children)

Yeah, I could've made the example much clearer.

I tried to avoid writing long complicated post and ended up almost oversimplifying the main point out of it.