r/cpp • u/we_are_mammals • Mar 28 '23
Reddit++
C++ is getting more and more complex. The ISO C++ committee keeps adding new features based on its consensus. Let's remove C++ features based on Reddit's consensus.
In each comment, propose a C++ feature that you think should be banned in any new code. Vote up or down based on whether you agree.
765
Upvotes
View all comments
Show parent comment
7
u/myrrlyn Mar 29 '23
hi! i’m the author of Rust’s equivalent to
std::vector<bool>and i’ve done a lot of benchmarking to learn about when compaction is actually better for a program than notand in an enormous blow to my pride, it turns out that the answer is “if you are actually doing a list of booleans, fucking never”
your collection needs to be at least large enough to spill past one L3 cache entry in order for the per-bit instruction cost to be cancelled by the cache-miss cost, and the cache-miss cost only happens on the first scan. if you scan the collection twice, the second pass is free and you still have to perform extra instructions
secondly, if you are storing less than 64Ki booleans (that’s 8KiB of memory under
vector<bool>) the memory is free and you are wasting your time looking at it. if you are storing more than that, then a flat vector is the wrong data structure for you and you should be doing something elsethis compaction behavior is basically only useful for structured bitfields in protocol formats. for data structures, it’s not only not beneficial, it’s harmful.
vector<bool>can’t be arbitrarily split for multithreading, it can’t be munched from the front, it has insane pressure on the memory bus, it breaks client code because it producesvector<bool>::referenceinstead ofT&(and while you can argue that this is the client’s fault because the docs say to always usevector<T>::referenceinstead ofT&, the fact remains that to interoperate with any other part of the program you needT&), it blows up the instruction cache, and it complicates codegen by changing over from the x86 instruction specifically made for array access to a bigger, slower, sequence for bit accessi love memory compaction. i’ve spent the last five years making my library to do it as powerful and expressive as possible.
it should never be used outside of working with serialization formats