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.

Max E.
  • 1,837
  • 13
  • 15
  • Does it need to be fast in the compression side or only in the decompression? – Shay Erlichmen Feb 17 '10 at 10:16
  • I would say compression speed is more important. Because different packets are being compressed on the server side for each client per frame (whereas the client needs to only decompress one packet per frame tops.) – Max E. Feb 17 '10 at 17:10
  • Even though this is old, I'm curious: why would you send out whole scripts on a per-frame basis instead of once, when the player connects (or even add some mid-game, but still send each one only once), and then just send a script ID and parameters? – Alex Celeste May 03 '15 at 20:14
  • 1
    My original reason was so I could send slightly different variants of the script (different colors, origins, intensities, etc.) each time. After I really refined my bytecode format, I most of the "scripts" were less than 50 bytes uncompressed, which is somewhat reasonable. However, I did end up caching them client-side and just resending the "arguments" separately each time. And then, after all that, I ended up not using the scripted FX system at all. :) – Max E. May 07 '15 at 21:15

6 Answers6

4

LZO might be a good candidate for this.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

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.
  • 1,837
  • 13
  • 15
  • 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. Feb 22 '10 at 05:53
1

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
  • 167,383
  • 100
  • 513
  • 979
1

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

0

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
  • 76
  • 1
  • 1
    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. Feb 17 '10 at 10:23
0

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
  • 15,109
  • 6
  • 49
  • 73
  • 1
    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 This 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. Feb 17 '10 at 10:34