views:

169

answers:

6

I am working on a game engine which is loosely descended from Quake 2, adding some things like scripted effects (allowing the server to specify special effects in detail to a client, instead of having only a limited number of hardcoded effects which the client is capable of.) This is a tradeoff of network efficiency for flexibility.

I've hit an interesting barrier. See, the maximum packet size is 2800 bytes, and only one can go out per client per frame.

Here is the script to do a "sparks" effect (could be good for bullet impact sparks, electrical shocks, etc.) http://pastebin.com/m7acdf519 (If you don't understand it, don't sweat it; it's a custom syntax I made and not relevant to the question I am asking.)

I have done everything possible to shrink the size of that script. I've even reduced the variable names to single letters. But the result is exactly 405 bytes. Meaning you can fit at most 6 of these per frame. I also have in mind a few server-side changes which could shave it down another 12, and a protocol change which might save another 6. Although the savings would vary depending on what script you are working with.

However, of those 387 bytes, I estimate that only 41 would be unique between multiple usages of the effect. In other words, this is a prime candidate for compression.

It just so happens that R1Q2 (a backward-compatible Quake 2 engine with an extended network protocol) has Zlib compression code. I could lift this code, or at least follow it closely as a reference.

But is Zlib necessarily the best choice here? I can think of at least one alternative, LZMA, and there could easily be more.

The requirements:

  1. Must be very fast (must have very small performance hit if run over 100 times a second.)
  2. Must cram as much data as possible into 2800 bytes
  3. Small metadata footprint
  4. GPL compatible

Zlib is looking good, but is there anything better? Keep in mind, none of this code is being merged yet, so there's plenty of room for experimentation.

Thanks, -Max

EDIT: Thanks to those who have suggested compiling the scripts into bytecode. I should have made this clear-- yes, I am doing this. If you like you can browse the relevant source code on my website, although it's still not "prettied up."
This is the server-side code:
Lua component: http://meliaserlow.dyndns.tv:8000/alienarena/lua_source/lua/scriptedfx.lua
C component: http://meliaserlow.dyndns.tv:8000/alienarena/lua_source/game/g_scriptedfx.c
For the specific example script I posted, this gets a 1172 byte source down to 405 bytes-- still not small enough. (Keep in mind I want to fit as many of these as possible into 2800 bytes!)

EDIT2: There is no guarantee that any given packet will arrive. Each packet is supposed to contain "the state of the world," without relying on info communicated in previous packets. Generally, these scripts will be used to communicate "eye candy." If there's no room for one, it gets dropped from the packet and that's no big deal. But if too many get dropped, things start to look strange visually and this is undesirable.

+3  A: 

LZO might be a good candidate for this.

Ignacio Vazquez-Abrams
Wow, that does look good! Thanks!
Max E.
+1, LZO is just the right tool for this job
axel_c
+1  A: 

zlib might be a good candidate - license is very good, works fast and its authors say it has very little overhead and overhead is the thing that makes use for small amounts of data problematic.

sharptooth
A: 

I would be inclinded to use the most significant bit of each character, which is currently wasted, by shifting groups of 9 bytes leftwards, you will fit into 8 bytes.

You could go further and map the characters into a small space - can you get them down to 6 bits (i.e. only having 64 valid characters) by, for example, not allowing capital letters and subtracting 0x20 from each character ( so that space becomes value 0 )

You could go further by mapping the frequency of each character and make a Huffman type compression to reduce the avarage number bits of each character.

I suspect that there are no algorithms that will save data any better that, in the general case, as there is essentially no redundancy in the message after the changes that you have alrady made.

bakerian
This is already being "compiled" (or perhaps should I say "assembled") server-side into a very compact bytecode-style format. In that example, the size of just the plaintext is 1172 bytes, prohibitively big. Thanks for the answer though. :)
Max E.
A: 

How about sending a binary representation of your script?

So I'm thinking in the lines of a Abstract Syntax Tree with each procedure having a identifier.

This means preformance gain on the clients due to the one time parsing, and decrease of size due to removing the method names.

Davy Landman
I should have made this clear-- yes, I am doing this. If you like you can browse the relevant source code on my website, although it's still not "prettied up."This is the server-side code:Lua component: http://meliaserlow.dyndns.tv:8000/alienarena/lua_source/lua/scriptedfx.luaC component: http://meliaserlow.dyndns.tv:8000/alienarena/lua_source/game/g_scriptedfx.cThis gets a 1172 byte source down to 405 bytes-- still not small enough. Great idea though; It was great enough that I thought of it too! ;)
Max E.
okay, than add it to your question ;-)
Davy Landman
+1  A: 

you should look at OpenTNL and adapt some of the techniques they use there, like the concept of Network Strings

fuzzy lollipop
Interesting stuff. Thanks!
Max E.
+1  A: 

FINAL UPDATE: The two libraries seem about equivalent. Zlib gives about 20% better compression, while LZO's decoding speed is about twice as fast, but the performance hit for either is very small, nearly negligible. That is my final answer. Thanks for all other answers and comments!

UPDATE: After implementing LZO compression and seeing only sightly better performance, it is clear that my own code is to blame for the performance hit (massively increased number of scripted effects possible per packet, thus my effect "interpreter" is getting exercised a lot more.) I would like to humbly apologize for scrambling to shift blame, and I hope there are no hard feelings. I will do some profiling and then maybe I will be able to get some numbers which will be more useful to someone else.

ORIGINAL POST:

OK, I finally got around to writing some code for this. I started out with Zlib, here are the first of my findings.

Zlib's compression is insanely great. It is reliably reducing packets of, say, 8.5 kib down to, say, 750 bytes or less, even when compressing with Z_BEST_SPEED (instead of Z_DEFAULT_COMPRESSION.) The compression time is also pretty good.

However, I had no idea the decompression speed of anything could even possibly be this bad. I don't have actual numbers, but it must be taking 1/8 second per packet at least! (Core2Duo T550 @ 1.83 Ghz.) Totally unacceptable.

From what I've heard, LZMA is a tradeoff of worse performance vs. better compression when compared to Zlib. Since Zlib's compression is already overkill and its performance is already incredibly bad, LZMA is off the table sight unseen for now.

If LZO's decompression time is as good as it's claimed to be, then that is what I will be using. I think in the end the server will still be able to send Zlib packets in extreme cases, but clients can be configured to ignore them and that will be the default.

Max E.
Wow, 1/8 of a second? That's *awful*. I had no idea deflate was that bad...
Ignacio Vazquez-Abrams
Well, it might not be *that* bad, I haven't actually timed it. It's entirely possible that there is some kind of tuning I'm not doing. I'll have a look at some other codebases and documentation to see. In any case, the results do look pretty fishy, I might do some timing with gzip and some test data to see if it can really get so slow. I suspect that this is just overhead, and if I was decompressing something 100 times bigger it would take about the same amount of time. Or *something*, I just can't believe the algorithm powering gzip is so slow.
Max E.