r/ProgrammerHumor • • Oct 07 '26

Meme wasteOfPreciousSpace

Post image
7.3k Upvotes

244 comments sorted by

View all comments

Show parent comment

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

2

u/Pim_Wagemans Oct 07 '26

But they are really usefull when you need to serialise a bunch of bools for transmission

2

u/Then-Candle8036 Oct 07 '26 edited Oct 07 '26

Whole lot of cpu cycles? Isolating the wanted bool is a bitshift and an & 0b1, and if you have to calculate the bitshift offset one more add. Depending on what youre doing you could also do masked operations.

In any case, the packing of bools increases the arithmetic intensity if your code which is a huge win since 90% of actual, real life programs are not actually compute bound, but memory bound.

2

u/zabby39103 Oct 08 '26 edited Oct 08 '26

Depends on what you do with them (like most things). If they represent things that can be multiplied with bitwise math like address spaces they're super definitely worth it.

Don't use bitfields to save on RAM, use bitfields to use bitwise math. If you're only ever using individual values from the bitfield and they're not related variables then that probably isn't worth it. I'm willing to believe there are some RAM cases in the corner cases, but never needed to personally.

Bitwise math though people, bitwise math is how I made a queue at work over 100x faster in a hot path.

→ More replies (5)
→ More replies (3)