r/ProgrammerHumor • • 10d ago

Meme cppAndItsVectorBooleans

Post image
3.4k Upvotes

214 comments sorted by

View all comments

Show parent comment

47

u/Usual_Office_1740 10d ago

I'm not going to try defend the design of std::vector<bool> because it looks annoying to develop around, but this post got me wondering, so I Googled it. The underlying bit packing behavior and implementation is a remnant of C++98 with addons over years.

This design was set down before algoriths, iterators, ranges and during a time when concurrent programming was not the norm, if it existed at all.

I think this was mediocre idea that has aged like warm milk in a hot car. Not the terrible atrocious design people seem to make it out to be.

44

u/ben_g0 10d ago

The bit packing behaviour can be genuinely useful as it saves memory and uses the cache more effectively. I have no problem with the implementation in and of itself.

The problem IMO is just that it is mapped to the vector<bool> type, while it behaves differently than any other vector in some cases, and its elements aren't really bools and don't act exactly the same. Even without concurrent programming.
The "vector of bits" that vector<bool> actually is, is still very useful to have in the standard library, but would be better as a dedicated bitvector type. And vector<bool> should just act like any other vectors and just store a list of bools.

10

u/Usual_Office_1740 10d ago

That is a reasonable complaint about it. I've only been programming with C++ for a few years. This is one of the many things that gets thrown around a lot in the cpp subreddits but rarely seems to get the context it deserves. I had to learn to frame the comments about performance costs in the same way. There are valid points to what's being said with these comments but there is often more context that much more experienced devs don't specify and it changes how newer developers like myself view things.

I legitimately tried to avoid using std::function and lambdas at one point early on because I read that they were "slow".

9

u/ben_g0 10d ago

About performance costs there is indeed a lot of misinformation and misunderstanding about it on platform like Reddit. A lot of people seem to still think that more CPU cycles means slower, while CPU clock speed is rarely the bottleneck.

Also, it's perhaps a bit of a controversial opinion, but unless what you're doing is very performance critical, I think it's totally fine to use approaches that are not the most optimal but easier to use or that keep the code better organised. Development time often has much more strict limits than compute time, so I think that trades some performance to make it faster or easier to write the program is usually a good trade-off.

3

u/Usual_Office_1740 10d ago

I agree about your maybe controversial opinion on development time and ergonomics being more critical than performance time. I am pushing myself to just get something written and working instead of fixating on whether it is the optimal decision or even a good way to do it.

I read once that there have been studies done in grocery stores where often times to much variety for a single item can reduce the overall sales figures for that item. People can't decide what bag of Lays they want and opt for not getting Lays at all rather than making a decision between one of twenty options. Things sometimes sell better when there are only a few of the most popular flavors because the impulse buy response can kick in.

In C++ there are a dozen ways to do most things and there are trade-offs for most of them. I think I've been struggling with that for a while and your opinion is one way I'm leaning into to pushing passed this observation. Picking what looks to be the most ergonomic option or even just the first option that comes to mind.

As far as performance goes. I was definitely affected by what is said on reddit. I'm a newer developer. I don't want to write buggy sluggish software. If people say that's slow I don't want to do it that way. It wasn't really until I followed a book for writing a terminal based text editor called Kilo earlier this year that I stopped bothering to consider that at all. When I saw how snappy and responsive that project stayed even with the number of memcopies and system calls being done to a heap allocated representation of a 1000k line text document with syntax highlighting every key stroke that I began to get a sense of just how powerful modern CPU's are and how little my struggling with the few hundred extra assembly instructions I'm agonizing over will ever matter in the project's I'm capable of producing.

Those concerns are probably valid for some and I like to think those that make those comments are often people working in high performance industries were their experience has colored there thought process. It is slow to them based on the way they view things and the criteria they've had to learn to evaluate against over years of experience in an industry where that matters. That doesn't mean I should assume it's slow based on what I need.

1

u/ih-shah-may-ehl 7d ago

as it saves memory

You're already programming in C++. I would think that the memory savings of a very specific use of booleans isn't the big deal it is made out to be.

3

u/Scared_Accident9138 9d ago

Concurrency wasn't supported by the C++ standard itself back then, so there also wasn't a need to specify what happens in case of concurrent access

1

u/Usual_Office_1740 9d ago

Yeah. I got my first multi core processor in 2004ish? The standard would have been written in the late 90's. Was concurrent programming something the average developer even considered?

1

u/Scared_Accident9138 9d ago

Wasn't the Linux way to just use fork if you need parallelism back then? (I mean it's still a thing)

1

u/Usual_Office_1740 9d ago

I don't know. I’ve only been programming for a few years. I’ve used fork for capturing stdout write calls for unit testing but that was it.

1

u/ih-shah-may-ehl 7d ago

My big problem with fork is that it requires you to build everything into the same process, then fork, and choose behavior depending on which of the instances you are. In many cases, you want multithreaded parallelism in the same process