r/ProgrammerHumor • • 16d ago

Meme cppAndItsVectorBooleans

Post image
3.4k Upvotes

214 comments sorted by

View all comments

Show parent comment

1

u/TatharNuar 14d ago

I don't know the reasoning why it would ever be worse, but maybe I'm assuming some abstraction you aren't using.

3

u/fweaks 14d ago

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.

1

u/TatharNuar 14d ago

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.

1

u/SirBackrooms 13d ago

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.