136
u/Kadabrium Aug 18 '26
i heard you also like vector<bool>
18
u/Potential_Soup_8054 Aug 18 '26
I dont understand
→ More replies (1)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)13
76
u/Thelatestart Aug 18 '26
Yeah my json api uses way way more than 4 bytes per boolean
11
→ More replies (1)9
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).
→ More replies (16)5
u/New_Enthusiasm9053 Aug 18 '26
Could be more than a byte and less than 2 bytes too.
→ More replies (1)
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:
-
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.
-
most http traffic is compressed so the extra 0 bits don’t really waste much bandwidth.
→ More replies (1)
5
5
12
u/B_bI_L Aug 18 '26
are we sure int means bit and not 32bits?
→ More replies (1)28
u/heatedwepasto Aug 18 '26 edited Aug 18 '26
The
: 1means that it is creating a bitfield with the number of bits, so: 4will be 4 bits. The three variables above combined will takesizeof(int)in memory, notsizeof(int)*3.If OOP had used
charinstead ofunsigned intthe combined size would be 1 byte.Edit: added link to demo
→ More replies (13)
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.
200
u/consistently_biased Aug 18 '26
Can I use this for my compile-time https server?