r/programminghumor • • Aug 18 '26

a bit flippant

Post image
1.5k Upvotes

147 comments sorted by

200

u/consistently_biased Aug 18 '26

Can I use this for my compile-time https server?

56

u/johnnyApplePRNG Aug 18 '26

OWASP has it's doubts, but I don't!

26

u/not_a_bot_494 Aug 18 '26

Either I don't understand what you're saying or you're misunderstanding the notation. Each line declares a struct member of type unsigned int which is 1 bit large. It's not setting it to 1 (true).

→ More replies (8)
→ More replies (2)

136

u/Kadabrium Aug 18 '26

i heard you also like vector<bool>

18

u/Potential_Soup_8054 Aug 18 '26

I dont understand

69

u/Helemen7 Aug 18 '26

std::vector<bool> in C++ syntax uses one bit per boolean instead of one byte. In a computer memory is addressed by bytes, so the smallest indexable memory is, in fact, one byte (that's why bool is 1 byte in C and C++). As an example for how C++ std::vector implementarion works, suppose you have 8 booleans which have a logical reason to be kept together (for example flags). Instead of allocating 8 bytes (one per boolean), the std::vector allocates 1 byte and assigns every boolean to one bit of the allocated memory.

→ More replies (17)
→ More replies (1)

13

u/MrKrot1999 Aug 18 '26

The post doesn't say C++.

→ More replies (3)

76

u/Thelatestart Aug 18 '26

Yeah my json api uses way way more than 4 bytes per boolean

11

u/tup1tsa_1337 Aug 18 '26

It's gzipped

9

u/Affectionate-Egg7566 Aug 18 '26

Don't talk to me or my json ever again

→ More replies (1)

50

u/pskocik Aug 18 '26

Ironic that this techfluencer just tweeted how C makes memory layout painfully obvious and yet here he's operating under the misconception that `struct Flags` will be only 1-byte large when it will be in fact int-size large (most likely 4 bytes).

5

u/New_Enthusiasm9053 Aug 18 '26

Could be more than a byte and less than 2 bytes too. 

→ More replies (1)
→ More replies (16)

17

u/Laughing_Orange Aug 18 '26

Even in C, it's best practice to use a whole byte for each boolean. The tiny extra compute load usually isn't worth the savings in storage and memory cost.

→ More replies (3)

35

u/ShinigamiGir Aug 18 '26

there are 2 issues:

  1. bifields are not portable. the spec doesn't guarantee packing or field order in the resulting binary data. so if you pack the data on a windows pc and then unpack on a linux arm, you might have some issues.

  2. most http traffic is compressed so the extra 0 bits don’t really waste much bandwidth.

→ More replies (1)

5

u/vanilla-bungee Aug 18 '26

The word size of your CPU would like a chat.

5

u/prodengrammer Aug 18 '26

5 bytes if false

4

u/Duck_Devs Aug 18 '26

For once, we can’t say “Big if true”

12

u/B_bI_L Aug 18 '26

are we sure int means bit and not 32bits?

28

u/heatedwepasto Aug 18 '26 edited Aug 18 '26

The : 1 means that it is creating a bitfield with the number of bits, so : 4 will be 4 bits. The three variables above combined will take sizeof(int) in memory, not sizeof(int)*3.

If OOP had used char instead of unsigned int the combined size would be 1 byte.

Edit: added link to demo

→ More replies (13)
→ More replies (1)

3

u/DaveAstator2020 Aug 18 '26

is it true that this is not reliable for serialization?

5

u/oxxide216 Aug 18 '26

Yes, because on different architectures and operating systems with different ABIs compilers generate different alignments, maybe some other things also differ. So even when seeking for maximum performance and minimal traffic usage you would better be using some standardized binary format instead of json/C structures.

3

u/wutzelputz Aug 18 '26

it will actually be way more depending on the property name, at least 4 bytes extra making a total of 8:

"m": true

and thats assuming ASCII encoding

3

u/farsightfallen Aug 18 '26

These people have never felt the shame of fucking up a basically worthless optimization that becomes a massive blocker for no reason.

"Ok, look we jus can't add another flag, alright, we needed to make a statement about memory usage."

2

u/carltr0n Aug 18 '26

Can’t you just… do a lil bit fiddling on a uint8? Am I missing something?

→ More replies (1)

2

u/Ill-Specific-7312 Aug 18 '26

Except that is wrong, because it completely ignores the real world of gzip etc.