Okay, my mission today was to start reducing the size of messages that the server is sending to the clients. My hope is that doing so will improve the performance of the application as more people connect. I think it was a good day!
A quick reminder on why the message size is so important
The size of these messages is critical because we are trying to send them, over the internet, at a rate of twenty times a second. Plus, you have to send one message out per connected client.
Why twenty times per second? Well, it’s kind of arbitrary. Since we are trying to run the game engine at sixty frames a second, twenty times a second means we are sending an update every third frame.
Packing and unpacking the messages
I implemented a couple of new utility functions that pack a JSON structure into a raw, nested set of arrays and then unpack the arrays to create the original JSON structure again. I then use them to “pack” and “unpack” the game state before sending it to the client and unpack it when received from the server.
The big advantage here is that the nested arrays do not contain all the strings for the element names that were in the original JSON. Implementing this reduced my message sizes by about 50%!
Real world results
The results for the game running from the server are mixed. It does seem to run better with up to four connections. After that performance still begins to degrade again. But progress is progress and I’m pretty happy with the code I got written.
Also, there seems to be a pattern I’m seeing. Sometimes the game’s performance seems to lag more than others. Especially in the evenings. Even with just one connection. This leads me to believe that either the game performance is degrading when there is more latency in the network in general, or on my host’s servers, or maybe there is more load on the shared environment.
There are several more optimizations I can make to the messages. I can enumerate values that are equal to repeating strings, I can try rounding some of the floating point numbers (although I have tried doing this before and the animation got really jittery) and I can also remove game objects from the game state that don’t need to be kept in sync. Like the particles in the explosions.
But, it might be an even better use of time at this point to instrument the application so that I can collect better evidence to figure out what is going on. I’ve already created a Loggly account that I can send log messages to and build some analysis around. Next I just have to select a client package to use and then start setting up the calls to the logger.