r/ProgrammerHumor • • Oct 07 '26

Meme wasteOfPreciousSpace

Post image
7.3k Upvotes

244 comments sorted by

View all comments

712

u/Large-Assignment9320 Oct 07 '26

Just back all 64 bools inside a good old perfectly aligned 64bit.

277

u/PhilippTheProgrammer Oct 07 '26

Bitfields might seem more efficient at first glance, but in practice they often cause more trouble than they are worth. And even a flawless implementation usually ends up buying a couple bytes of RAM for a whole lot of CPU cycles.

174

u/American_Libertarian Oct 07 '26 • Bravo

You’re being flippant, but in the world of latency sensitive computing that’s huge. Memory access is VERY expensive, and cpu math is basically free. Packing bools is an easy big win when performance matters.

64

u/Kitchen-Layer5646 Oct 07 '26

even when latency isnt a concern, when you're limited in bandwidth it can have a huge effect!

29

u/Firewolf06 Oct 07 '26

also crossing the threshold of fitting n+1 copies of a struct into a cache line, when working with arrays

75

u/EatingSolidBricks Oct 07 '26 • Popcorn

Hehe bit flippant

25

u/wobblyweasel Oct 08 '26

(⁠ノ⁠´⁠・⁠ω⁠・⁠)⁠ノ⁠ ⁠ミ⁠ 🩳

13

u/turtle_dragonfly Oct 07 '26

siiiggghhh.... upvote

11

u/Spaciax Oct 07 '26

not to mention, for bitwise operations haven't CPUs been super duper fast for a while now?

9

u/wayzata20 Oct 07 '26

I’m not a low-level memory expert, but surely it takes the same amount of time to grab 2 bytes over a single byte? So I don’t think the memory latency really matters here.

47

u/American_Libertarian Oct 07 '26

It’s not about saving literally one byte. But if you have a data structure that uses a lot of bools, compacting them can be the difference on fitting in a cache line or not. Or if you have a large array, you can fit multiple structs in a cache line.

Moral is: keeping your data compact is really really really important for performance. And bitfields are a good tool to keep data compact.

28

u/ScrotumNipples Oct 08 '26

How dare you preach best practices in my AI slopcoding meme sub.

→ More replies (1)

14

u/thoeoe Oct 07 '26

We had a (memory mapped) flatfile of 192 entries with 18 Bool flags each. Making them bitfields saved nearly 3k bytes

5

u/CC-5576-05 Oct 07 '26

If you have a lot of bools were not talking about one extra bytes, it's 8 times as many bytes. And how many times are you moving those extra bytes?

→ More replies (4)
→ More replies (1)

25

u/VictoryMotel Oct 07 '26

This is completely ridiculous. You might only be thinking of a few flags in a struct but if you think about things like a black and white array / image, there are plenty of times you want to pack in bits.

for a whole lot of CPU cycles.

Absolutely not. Bit shifting is as cheap as it gets, accessing memory is expensive. You could do 1500 bit shifts in the time it takes to get a single byte from memory that isn't in the TLB. (You can't actually make a cpu only get a single byte though. It will always access two cache lines at a minimum, each being 64 bytes).

→ More replies (7)

19

u/Kitchen-Layer5646 Oct 07 '26

reducing the size of a struct of a few bytes can give a huge amount of performance in accessing arrays when you're memory bound

82

u/EatingSolidBricks Oct 07 '26

Bit Fields are blazingly fast when their fast and dogshit slow when there slow

103

u/diacachimba Oct 07 '26

That's they're problem.

45

u/EatingSolidBricks Oct 07 '26

Holy shit I've written all variations of the wrong thing, look i type fast not correctly

→ More replies (1)
→ More replies (4)

12

u/BriefSpecial420 Oct 07 '26

It all depends on CPU and architecture. People have no clue how many resource-constrained microcontrollers are around us nowadays. Even good old 8051 will be around for bunch of years until minimal variants of riscv may dominate the market. Sometimes we still have to deal with systems that have KBs of total memory at most.

Bit fields rarely fit the needs due to lack of placement warranty. Good old bitmasks seem to still be the golden standard when it comes to operating on bits in registers.

4

u/bureaucrat473a Oct 07 '26

A couple bytes, in this economy? Sign me up.

3

u/RandallOfLegend Oct 08 '26

Yet all CNC machines run on bit fields for IO

→ More replies (11)

6

u/ProgrammersPain123 Oct 07 '26

Aligned memory? In this economy???

2

u/99_in_eating Oct 07 '26

bitboards go brrr

→ More replies (1)