3 ms·
”It's just an extra indirection in the data member. It's cheap” That extra indirection often means a cache miss. That isn’t cheap. Accessing each item traverse
by Someone 8y ago
”It's just an extra indirection in the data member. It's cheap”
That extra indirection often means a cache miss. That isn’t cheap. Accessing each item traversed through a pointer can easily halve program speed.
Java tries hard to prevent the indirections (local objects may live in the stack, their memory layout need not follow what the source code say, objects may even only exist in cpu registers)
- repsilat 8y agoHmm... if you were a horrible person you could declare a `char[n]` member instead of a pointer. Then you could placement-new the impl in the constructor, and static-assert that `sizeof(impl)>=n`... No more cache misses :-). :-(
- shereadsthenews 8y agoThat’s not that gross. There are types in Abseil that do it.
- blt 8y agoThat's basically how modules would work, at least if you ignore LTCG.
- yuushi 8y agoThis doesn't take into account the alignment of the type though (you'd want to use std::aligned_storage<sizeof(T), alignof(T)>), but that requires knowing enough about T to be able to use sizeof() and alignof(), which means no incomplete types, bringing us back to where we started.
- vnorilo 8y agoWhen you need this, use aligned storage: https://en.cppreference.com/w/cpp/types/aligned_storage https://en.cppreference.com/w/cpp/types/aligned_storage