4 ms·
Compilers are extremely limited in how they are allowed to change data layout on your behalf. In C/C++ there’s: per-platform integer sizes (which are easily and
by corysama 7y ago
Compilers are extremely limited in how they are allowed to change data layout on your behalf. In C/C++ there’s: per-platform integer sizes (which are easily and usually forced to be consistent), padding between members of structs, alignment of objects, anything else? I think that’s about it.
Compilers are definitely not allowed to insert a small buffer into an object. Nor even repurpose an int that’s not being utilized completely. Messing with your data under the hood is blasphemous.
Unfortunately data issues have been the top cause of performance problems for a long time and it’s only going to get worse. A cache miss is much, much more expensive than operations people are taught are slow, like divisions, square roots and branches. Meanwhile, in my dozens of interviews with junior programmers I’ve found that the vast majority are only vaguely aware that cache exists at all :(
- chrisseaton 7y agoI don't know if you have more experience than me in C and C++ compiler implementation, but I think you have a lot of options for how you layout memory. The C++ specification doesn't say anything about how objects are laid out does it? ABIs do, but not C++. I've worked with a conforming C implementation that uses Java objects for structs, for example. As long as you match the intended semantics, you can inline a malloc call into an object if you want to.
- corysama 7y agoWell, I know it requires elements to be laid out in the order they are declared, but it is allowed to put gaps between items for alignment. I guess a compiler could insert a 16-byte gap and do whatever it wants in there. I don't expect a great reception for that feature from the community... Then there's the issue of writing a compiler that is smart enough to recognize that you allocate small arrays frequently enough that it's worth it to insert an "alignment wink-wink" buffer into your object :P I do believe there is some work to elide allocations that are short-lived enough that the compiler can strongly guarantee their entire lifetime. For example: inside a small std::vector that lives on the stack (but it's internal buffer would normally be malloced). There's even work on getting this to work in constexpr so that you could have constexpr algorithms allocate temporary memory that does not live past the end of the compile-time algo. However, I was knee-jerking against a wider complaint I've seen frequently along the lines of "Why can't the compiler just optimize my data structures for me to make it go more good?" in terms of AOS to SOA transformations, separating hot-cold data into different arrays, somehow magically getting rid of pointer indirections and other major re-writes. That's a research topic for some libraries and special-purpose languages. But, it's outside of the scope of the C++ compiler.