r/ProgrammerHumor • • Oct 07 '26

Meme wasteOfPreciousSpace

Post image
7.3k Upvotes

244 comments sorted by

View all comments

Show parent comment

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.

1

u/ligma_then_sugma Oct 10 '26

does this mean that linked lists are based and not cringe?

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?

1

u/danielcw189 Oct 07 '26

I am also not an expert, but ...

are you talking about fetching 2 bytes at the same time? or one by one?

either way, memory bandwidth is a premium, and your CPU can already fetch the next useful block of memory, while you are doing some bitmask math for the bools.

4

u/wayzata20 Oct 07 '26

You fetch memory in blocks anyway though. You’re not saving any bandwidth by using 1 byte vs 8 bytes if you have, say, 8 bools.

8

u/American_Libertarian Oct 07 '26

A cache line is 64 bytes. If those extra 8 bytes push you into the next cache line, you absolutely do pay a penalty

1

u/danielcw189 Oct 08 '26

you ignored my question:

are you talking about fetching 2 bytes at the same time? or one by one?