I'd rather have my bools packed, since I generally work with microcontrollers and need every byte I can save. For some reason, the compiler that comes with Arduino IDE tried to store my hundreds of bools bytewise and ran out of memory, and I had to make a struct to get them to pack the way I wanted. Also, packed bools can be set or unset together in a single operation, so it can be a lot faster.
If you are setting or reading a single bit, it is usually less performant because it requires more operations.
Also it can no longer be pointer iterated the same as any other vector could be, breaking templating code. You have to keep making special case code just for it.
And thats if you even know about it, since it's not clear just looking at the type that its completely different from every other specialization of its template.
Bitwise logic operations are supposed to be the fastest operations a CPU can run, though. Ideally you want to group up bits you're likely to read together into the same word so a single instruction reads all of them. Same goes for setting bits using a bitmask. You can still pointer iterate, but you're doing it per-word rather than per-bool. You'd get a speedup equal to the word size because you're effectively parallelizing your writes with a single bitwise logic instruction.
I personally tend to use C more than C++, and HDLs even more than that, but my first impression of bool was that it was meant to be a single bit in size so you could do things like that.
This seems like a special case that would benefit from its own type like bitset rather than having vectors arbitrarily work differently for a single type and break stuff unless you know to take care of it.
View all comments
12
u/TatharNuar 15d ago
I'd rather have my bools packed, since I generally work with microcontrollers and need every byte I can save. For some reason, the compiler that comes with Arduino IDE tried to store my hundreds of bools bytewise and ran out of memory, and I had to make a struct to get them to pack the way I wanted. Also, packed bools can be set or unset together in a single operation, so it can be a lot faster.