top | item 43425145

(no title)

Fell | 11 months ago

Most video games compress booleans to bitfields in the save files to save space. Depending on the game, a save file can contain thousands of objects, so it makes sense to keep it small.

At runtime, booleans are 1 byte each. The additional work of shifting and masking to get to the bit you want simply isn't worth it. When reading from memory, the CPU will read a whole cache lines of 64 bytes anyways. So unless you want to process a lot of booleans (and only booleans) at once, it's simply not worth having them 1 bit each.

Because aligned data runs faster, a single boolean sandwiched inbetween integers or pointers can even take up 4 or 8 bytes of memory.

Note: Some languages have built-in optimisations where an array of booleans is actually 1 bit each. C++'s `std::vector<bool>` is such a case and I'm sure there are more.

I've seen people advise to never use booleans, simply because a number (or enum) can convey more meaning while taking up the same amount of space.

discuss

order

cityofdelusion|11 months ago

Bit fields are also common in live service network communication and other large data arrays like graphics calls in video games. Depending on what is being done, packets may not be inflated into proper data structures and left as raw bits to remove processing overhead. That’s my experience with actual 1 bit booleans, but yeah the vast majority of the time the “higher level” byte abstraction is just fine.