TLDR: Propsed changes would reduce UNET bandwidth by ~10x (depending on usage).
After looking at how UNET uses Messages and OnSerialize/OnDeserialize to handle NetworkBehaviour SyncVars I think they are pretty inefficient.
There is a 32bit dirty mask sent for each NetworkBehaviour synced script on an object. Anytime a network package is sent for that object I think a 32bits are sent for each attached synced script. That means if you have 10x scripts that 10x32 = 320 bits is sent before any payload is added.
In addition, bools are sent as 8bits instead of 1bit. Stream classes use 8bits as their minimum resolution and I speculate this is the reason for this (it’s always easier to work with whole bytes).
So lets give an example of how UNET currently works with a worst-case scenario showing the inefficiency and proposed improvement.
-You have 10x scripts each with a SyncInt, SyncBool, SyncFloat, and SyncVec3.
-You change a SyncBool on NetworkBehaviour #2 to true.
-When a object network package is sent the data will be this:
Msg.Type 16bits Ushort
NetworkIdentity.netID (32bits?)
Script 1: 32bits: dirty mask = 00000000 00000000 00000000 00000000
Script 2: 40bits: dirty mask = 01000000 00000000 00000000 00000000 + bool data 10000000
Script 3-10: 32bits each: dirty mask = 00000000 00000000 00000000 00000000
Total of 376bits of data is sent to transmit 1 bit (the bools on script #2) data.
After looking at this I think it would be beneficial for UNET to change a few things.
1: NetworkReader/Writer should be able to write bools as bits (not bytes). Example here: Bit-Based BinaryWriter in C# - Stack Overflow
2: Each NetworkBehaviour script should have its own dirty bit.
3: The SyncVar dirty mask should be variable size and match the number of SyncVars
4: SyncBools should flag the NetworkBehaviour as dirty, but should NOT write to the SyncVarDirtyMask because the dirty flag is just as much bandwidth as the payload (1 bit). Their data should always be sent anytime the NetworkBehaviour flag is dirty.
So lets repeat the example above.
Msg.Type 16bits Ushort
NetworkIdentity.netID (32bits?)
Script 1: 1bits: NetworkBehaviour dirty = 0
Script 2: 1bits: NetworkBehaviour dirty = 1. SyncVarDirtyMask = 000. bool data 1
Script 3-10: 1bits each: NetworkBehaviour dirty = 0
Total: 62bits instead of 376bits!
Why so much less bandwidth with these changes?
Scripts 1, and 3-10 only sent 1bit (a single 0) instead of 32bits of zero’s.
Script 2’s dirty mask is only 3bits (1 for int, 1 for vec3, and 1 for float) instead of 32bits
Script 2’s bool payload is 1bit instead of 8bits
In addition these changes would allow for >32 SyncVars on 1 script.
This illustrates a worst-case UNET scenario… but in typical usage I think this type of change might result in ~3x or so reduction in bandwidth (it depends greatly on usage).
5: Additionally instead of Ushort Msg.Type this could also be variable length based on the number of registered messages. i.e. if there are <= 128 message types then this can be packed into 7bits (2^7) instead of using a constant 16bits. <=512 message types would use 9 bits (2^9)
6: Similary (although this may be harder because it’s constantly changing) the NetworkIdentity.netID could be reduced from 32 bits to a smaller number based on the number of current network objects. i.e. if there are only 16 objects in a scene then 4bits (2^4) could be used to send the ID instead of a constant 32bits.
With all these changes:
Msg.Type 7bits (variable length. Assume <= 127 message types)
NetworkIdentity.netID (4bits. Assume there are <=16 objects, would be 5bits for <=32 objects etc.)
Script 1: 1bits: NetworkBehaviour dirty = 0
Script 2: 1bits: NetworkBehaviour dirty = 1. SyncVarDirtyMask = 000. bool data = 1
Script 3-10: 1bits each: NetworkBehaviour dirty = 0
Total: 25bits instead of 376bits!
DISCLAIMER: I think this is the way things work with UNET, but I am not 100% certain. Let me know if anything is incorrect.
Hope this makes sense and thanks for reading. If anybody knows any ways to get this in front of a UNET dev please let me know.
EDIT DirtyMask is written as UPacked32 which will typically consume 8bits not 32bits.