My question is relatively straightforward, but I haven't been able to find a concrete answer yet.

I'm programming a real-time networked multiplayer game, and I'm finding many examples of places where I could optimize packet size (i.e. minimize packet size) in small ways. As an example, one packet type stores player input (running, jumping, firing a weapon, etc.) along with an integer ID for that player. In practice, there will never be more than four players in a single game, meaning that ID could be represented with only two bits (four possible values) rather than the full 4-byte integer.

My question is, should I send only those two bits across in a network message or just send the full integer?

To clarify, I'm not referring to storing the ID in the main code itself. For that, I'm just using an int. I'm only curious about what I should be sending in a network packet. That's also just one example of a few I've run into.

The reason I ask is that this feels like a "don't prematurely optimize" scenario. Ordinarily, I know to not worry about optimization (within reason) until I've run profilers, but I'm less familiar with networking than "normal" programming. Is it better to optimize packet size as much as possible, or would that be overkill?

Thank you.

  • 1
    \$\begingroup\$ It completely depends on how much data you're sending. If you're only sending a few integers, I would totally not worry about it. \$\endgroup\$
    – Almo
    Commented Jan 3, 2017 at 21:22
  • \$\begingroup\$ That makes sense. I'm sure that I'd likely never run into substantial issues using either approach. I'm more curious about the more theoretically "correct" way (if there is one) and the reasoning behind it. \$\endgroup\$
    – Grimelios
    Commented Jan 3, 2017 at 21:41
  • 1
    \$\begingroup\$ There are few places in game development where there's a universal "correct" way to do something. Everything is trade-offs between runtime efficiency, code complexity, and other concerns. The same general rule applies across domains though: "if it ain't broke, don't fix it." You can profile your network traffic much like you'd profile your CPU or memory use, and use that to identify where you have problems that are worth the dev investment to fix them. You can always go deeper down the bit-packing rabbit hole if you find it's important to your game. \$\endgroup\$
    – DMGregory
    Commented Jan 3, 2017 at 22:34
  • \$\begingroup\$ That's a nice reminder, that there's usually no universal correct approach. Thank you. \$\endgroup\$
    – Grimelios
    Commented Jan 3, 2017 at 22:49
  • \$\begingroup\$ Are you talking about lots of separate small packets? That's always a bad idea. Combine many small packets into fewer larger packets, and optimize data sizes to allow you to combine more. \$\endgroup\$ Commented Jan 4, 2017 at 8:12


You must log in to answer this question.

Browse other questions tagged .