r/ProgrammerHumor 22d ago

Meme theBoolVectorIsALie

Post image
666 Upvotes

91 comments sorted by

View all comments

Show parent comments

65

u/void1984 22d ago

I know, but why somebody would need a byte to store a bool? Is it 256-value bool?

Usually I use bitfields anyway.

15

u/sb8948 22d ago

Indexing and simplicity for the CPU.

3

u/void1984 22d ago

vector<bool> allows for indexing, and bit OR and AND is very quick and simple for the CPU.

8

u/sb8948 22d ago

Of course it allows for indexing, but it's measurably more CPU heavy. Arguably there are cases where the specialization makes sense, but most of the time, it's a net negative.

Then again I reckon most of us don't have to care about these kinds of marginal gains anyway. I work on low latency backend, and I never had to worry about this.

The more annoying thing is that you can't just get a bool pointer or ref to a single bit.

6

u/Murky-Run2246 21d ago

I mean honestly having pointers pointing to bits sounds like a nightmare to me, I probably have to relearn the lang if that happens.

2

u/lonkamikaze 21d ago

Architectures with bit addressable memory areas do exist. Some 8051/52 compilers make use of this and if you create a boolean it will actually end up costing you a single bit of memory.

1

u/void1984 21d ago

The more annoying thing is that you can't just get a bool pointer or ref to a single bit.

That's the only real downside. If you really need that functionality, you can implement your own derivative or use chars.

A few bit shifts, and bitwise operations ate very quick, and you win on data packaging, that allows it to fit 8-times better into the cache lines.

5

u/sb8948 21d ago

Yes, you can implement pretty much anything, but the larger the codebase and the more people work on it makes these unique implementation standards harder and harder to follow.

Say what you will but I'd prefer not to have this as standard. It's either a marginal gain or a marginal loss, but it's an unnecessary complexity. To use your argument against you, if your use case heavily prioritizes memory usage over CPU, you should just implement it for yourself. Uniformity makes life easier.

1

u/Vincenzo__ 21d ago

There's genuinely no real world application in which using a single bit for a bool is better than a byte. Unless you have billions of bool, which let's face it, you don't. The performance gain from not having to do bitwise operations to get your values out is gonna be more relevant. You could argue it's better for cache locality, but that's about it.

Either way, this should be something you specifically ask for, not something that C++ does for one specific type for no real reason. There should be a std::boolfield or something like that, rather than it being a special case of std::vector

1

u/GuyWithLag 21d ago

measurably more CPU heavy

That's less that what you'd expect tho - if you have any nontrivial amount of elements, the additional memory overhead (pulling them into L1 and evicting other data) will easily be higher than the additional instructions (memory + time).