Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: 8 bit bools in packets instead of 1 bit  (Read 5127 times)

0 Members and 2 Guests are viewing this topic.

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
8 bit bools in packets instead of 1 bit
« on: July 17, 2014, 10:19:39 pm »
Why does the Packet class send 8 bits for bool data instead of 1?
Code: [Select]
Packet& Packet::operator <<(bool data)
{
    *this << static_cast<Uint8>(data);
    return *this;
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: 8 bit bools in packets instead of 1 bit
« Reply #1 on: July 17, 2014, 10:36:36 pm »
It's the same reason why bool has 8 bits and not 1. The byte is the lowest addressable unit, and sockets or streams in general work on bytes. If you inserted a single bit into a byte buffer, that would change the alignment of all the following bytes, meaning that 7 bits are in one byte and 1 in the other. This would not only make everything more complicated, but horribly inefficient. Computer architecture is conceived and optimized to work on bytes.

If you need to save space for transmission, you can do it on user side by packing multiple bits into an integer, using bitwise operators. An interesting article in this context is byte order fallacy.
« Last Edit: July 17, 2014, 10:39:18 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Re: 8 bit bools in packets instead of 1 bit
« Reply #2 on: July 18, 2014, 11:48:05 pm »
I observed some open source game projects to use bit streams, and so I was inclined to ask. Thanks for the response!

Edit: Sort of curious about "horribly inefficient" (net resources more valuable usually?)

Edit: Also, I wonder if typical network compression on outgoing packets somehow fixes this
« Last Edit: July 19, 2014, 12:07:29 am by Wizzard »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: 8 bit bools in packets instead of 1 bit
« Reply #3 on: July 19, 2014, 08:56:56 am »
What I meant is that it's usually not worth doing this kind of optimization on SFML's abstraction layer or below, because it doesn't have enough knowledge about how to efficiently pack data together. For example, re-aligning everything because of a single bool would be a very bad idea (especially because the same number of bytes is still required).

However, on the application layer, you know how to compose the data, and have more sophisticated possibilities:
  • Use integers with bitwise arithmetic
  • Use bitsets (std::bitset, boost::dynamic_bitset, std::vector<bool>)
  • Compression algorithms like those from zlib.
« Last Edit: July 19, 2014, 08:59:40 am by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Wizzard

  • Full Member
  • ***
  • Posts: 213
    • View Profile
Re: 8 bit bools in packets instead of 1 bit
« Reply #4 on: July 20, 2014, 03:47:34 pm »
Thanks again! You've helped me think a few ideas through

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: 8 bit bools in packets instead of 1 bit
« Reply #5 on: July 21, 2014, 09:19:25 pm »
This smells of premature optimization to me.
Why are you even worrying about little things like this? Unless a profiler told you that this is your performance bottleneck or all you are ever doing is exchanging bools, then this is likely to be of no concern what so ever in the real world.
If you are employing any kind of compression to your serialized output stream, then that's going to take care of the redundant bool bits for you easier than anything else. Even if you are not, than the rate of bools to other significant amount of data is likely to be so low that a few wasted bits are going to be lost in the noise.
Seriously, don't worry about it and don't waste your time optimizing it - in the grand scale of things it is going to be utterly irrelevant and a waste of time.