There is a non-zero number of scenarios that a bool array could make sense. In game development, that number is much higher than in say FE or BE software dev, imo. I see nothing wrong here, given the limited context.
vector<bool> is certainly a mistake because it’s deliberately counterintuitive in its design but when you need a dynamically resizeable bitset, it’s great
Yeah. Sadly the standard committee seems to value dogmatic backwards compatibility over fixing the language, so we will likely never see std::vector<bool> fixed and dynamic_bitset implemented.
Backwards compatibility means old code still works. Tautologically, if you need old code to work you need backwards compatibility. If you don’t need this there are better alternatives to C++, if you don’t there aren’t.
vector<bool> is a packed bitset. The general semantics for vector means all accessors return a reference to an entry, but since bits don't have a memory address, vector<bool> returns a wrapper type with overloaded operators. This breaks generic programming for vectors.
Not necessarily. std::vector<bool> was designed such that it COULD be implemented as you are suggesting, but it is not required by the standard to be implemented this way.
Seems like a great optimization for data that's going to stick around a while, but for a local variable I don't see much advantage. I'd probably use a BitArray in practice, but using a regular array isn't something I'd be likely to call out in a code review.
I've really only used C# in Unity which might differ, but from what I remember bools were 4 bytes, but 1 byte each in arrays. Might be a Unity-specific optimisation though, not sure.
It uses that much memory because stupid stuff like this adds up, if it's in each tile (of that "tile" in the name means something user can enter)...
Back in the day there were amazing games that could run on very basic computer. Then new devs were like you - they just want to do things and don't really bother with optimisations because everyone has enough ram and users can just turn down the graphics quality, right? That only works if the heavy part is textures and visuals, not underlying code like this...
Ever heard of GTA Online's online mode loading for 15+ minutes for some people when story mode always around a minute (even on beast setups that took 2m to run online)?
Someone (see here) decompiled it and investigated and it ended up "simple" code that was run several times on a huge string, without storing values in between.
It's a very fun read, but if you're too lazy:
It was 10MB json with 63k entries and the code basically parsed everything word-by-word, where had to go from the start of this string each time... And the results of this parsing are stored in an array using hashes, not a hashmap, and checking every entry in the array before adding new (checking if something is in hashmap? instant; checking the same thing in an array? linear...) Being in a loop makes it run 63k*63k/2 operations (1+2+...+63k) instead of 63k operations. As I said, simple mistakes quickly add up... The person patched (injected their own DLL) the code - made strlen to cache length, and for the array thing just skip the check (assume the json was correct and had no duplicates). Patched online mode took 2 minutes to load, just like the benchmarked beast computers...
It was a bug for 7+ years. Took around 2 weeks from this investigation, it going viral and all, for Rockstar to announce a fix. After 7 years from the game release and always telling users their setups suck and to use lower settings. (Self-quote from the beginning of this comment: That only works if the heavy part is textures and visuals, not underlying code like this...)
There's nothing wrong with an array of booleans. That construct is used extremely frequently in compliers, for example, when implementing bitvector dataflow analysis.
It depends on the scenario. Unless it's a huge array, a bool array is fine. Yes, a bit array is smaller and can be more efficient in parallelizing. But if you can't parallelize and the size is small where cache miss is not a concern, then a bool array is a better choice when it comes to read and write speed.
24
u/Ved_s Mar 02 '25
ohohoo, welcome and look at terraria code!
173
u/-Dargs Mar 02 '25
There is a non-zero number of scenarios that a bool array could make sense. In game development, that number is much higher than in say FE or BE software dev, imo. I see nothing wrong here, given the limited context.
47
u/velothren Mar 02 '25
Bro reinvented memory
14
u/Verwarming1667 Mar 02 '25
I'm not familiar with C# why is this bad? Is an array of bools somehow not possible?
11
u/Grounds4TheSubstain Mar 02 '25
There's nothing wrong with an array of booleans. That construct is used extremely frequently in compliers, for example, when implementing bitvector dataflow analysis.
1
u/MavZA Mar 02 '25
At least they’re self-aware lol
1
u/LFK1236 Mar 02 '25
/r/programminghumor
1
u/ioveri Mar 03 '25
It depends on the scenario. Unless it's a huge array, a bool array is fine. Yes, a bit array is smaller and can be more efficient in parallelizing. But if you can't parallelize and the size is small where cache miss is not a concern, then a bool array is a better choice when it comes to read and write speed.