Forum C# Different value types in message?

Different value types in message?

Postby playstation3Dood » March 23rd, 2010, 9:34 am

I was taking a more in-depth look at trying to optimize messages to use less bandwidth, and i see there are a ron of different tyes of info that can be included in a message: boolean, bytearray, double, float, hashcode, int,integer,long,string,uint,ulong,unsignedinteger,unsignedlong. me not being an uber programmer, I can't make heads or tails from all this. which would be the least bandwidth-optimized to send a small number (<100)? large numbers (millions)? decimals, short/long strings? and whats the difference between int and uint? are uint and unsignedint the same? and what on earth are doubles, floats, hashcodes, and longs(and ulongs)? i did a bit of research on the internet, but still can't wrap my head around it. wish it was as simple as AS2 :lol:

sorry i know thats a lot of questions, but i'm probably going to be sending lots of messages to/from the server for my game and i want them to be as fast and small as possible.

oh and one more :) , am i correct in assuming the same types have the same properties (ie bandwidth, capacity etc.) in AS3? thanks a bunch.
playstation3Dood
 
Posts: 49
Joined: February 23rd, 2010, 7:39 am

Re: Different value types in message?

Postby Toby » March 23rd, 2010, 12:26 pm

A uint and an unsigned int are exactly the same thing. Basically, when you have an integer value, a value is taken up in memory for the computer to determine whether or not the value is positive or negative. A uint, on the other hand, cannot be negative, so there is more space free (because no +/- is being stored) to store larger values. Uints can store values of 0 to 2^32 whereas an integer can store +/-2^31.

You can use google to find out what the others are. Really, though, the best way to optimise your data is to start by looking at how often it's sent. Try to cut down on redundant messages (things the other clients already know) and the frequency of which you send them out. If you were working on a multiplayer paint program, for example. You may be sending:
Code: Select all
(x, y, brushType, brushColour, brushThickness)

for each move of the mouse. A more efficient way would to, obviously, only send the x and y values and instead send the thickness, colour and type to all clients as it is changed.
Things like that are the best ways to minimise bandwidth usage.
User avatar
Toby
 
Posts: 86
Joined: January 14th, 2010, 4:01 pm

Re: Different value types in message?

Postby playstation3Dood » March 23rd, 2010, 12:31 pm

Ok thanks for the help Toby, i'm always trying to optimize my SWF more and more. I have already thought about message frequency/redundancy, so now i'm focusing on message size. so uint would be the best type for just about anything above zero that's not too big. is there anything even smaller, for like double digitstuff? or how about decimal numbers?
playstation3Dood
 
Posts: 49
Joined: February 23rd, 2010, 7:39 am

Re: Different value types in message?

Postby Toby » March 23rd, 2010, 12:44 pm

Well, odds are you won't be using numbers big enough to require a uint anyway.
(http://en.wikipedia.org/wiki/ActionScript#Data_types)
ActionScript 3 primitive (prime) data types (see Data type descriptions)
Boolean - The Boolean data type has only two possible values: true and false or 1 and 0. No other values are valid.
int - The int data type is a 32-bit integer between -2,147,483,648 and 2,147,483,647.
Null - The Null data type contains only one value, null. This is the default value for the String data type and all classes that define complex data types, including the Object class.
Number - The Number data type can represent integers, unsigned integers, and floating-point numbers. The Number data type uses the 64-bit double-precision format as specified by the IEEE Standard for Binary Floating-Point Arithmetic (IEEE-754).
String - The String data type represents a sequence of 16-bit characters. Strings are stored internally as Unicode characters, using the UTF-16 format. Previous versions of Flash used the UTF-8 format.
uint - The uint (Unsigned Integer) data type is a 32-bit unsigned integer between 0 and 4,294,967,295.
void - The void data type contains only one value, undefined. In previous versions of ActionScript, undefined was the default value for instances of the Object class. In ActionScript 3.0, the default value for Object instances is null.

Really, the only advantage is using int instead of number because number is 64-bit and an int is 32-bit. Flash doesn't really have many data types to handle numbers... (remembering, of course, ints can't have decimal values)
User avatar
Toby
 
Posts: 86
Joined: January 14th, 2010, 4:01 pm

Re: Different value types in message?

Postby playstation3Dood » March 23rd, 2010, 1:12 pm

Dude, you're a huge help. Thanks :D
playstation3Dood
 
Posts: 49
Joined: February 23rd, 2010, 7:39 am

Re: Different value types in message?

Postby Toby » March 23rd, 2010, 3:39 pm

Also, remember. If you're really desperate about saving those few bits of bandwidth, sending decimals (as a Number) is 64 bit (32 bits for the value, 32 bits for the power to raise it to) whereas sending rounded values (as an integer) is 32-bit. Going back to the brush example, you could have float values for brush sizes (3.5px width) which would be 64-bits per send. If, instead you had the different brush sizes stored serverside (int[] brushSizes = {1.5, 5, 10.2};) and then the clients just send the index for that brush (i.e. brush 2 would be 10.2) then you've just saved yourself 32-bits.

If we were sending brush-size each tick, it would eventually build up. So think about this for things like bullets being fired or change in ship rotation, where the angle doesn't actually have to be pixel-perfect. You're only going to really benefit from all this messing about if the type of message is being sent a lot.

Again, this really is a very nitpicky thing to be doing. Good for scalability, sure. But if you ever use a lot of bandwidth then chances are you will be able to monetise the game and then get enough funds to purchase a higher plan on IO.
User avatar
Toby
 
Posts: 86
Joined: January 14th, 2010, 4:01 pm

Re: Different value types in message?

Postby Oliver » March 23rd, 2010, 7:51 pm

I second what toby is saying: Don't spend time on optimizing the individual values in the messages, rather send fewer messages.

That being said, i'd like to clarify something: Player.IO uses a highly optimized binary format for the messages, were smaller values take up less space than larger values.

For instance, if you send an int in the range 0-63, it will only take up a single byte. The downside is that the maximum value will take up 5 bytes in total (including type information), but how often do we really send the maximum integer value over the wire?

Doubles and floats will always take up 8 or 4 bytes.
Boolean values take up one byte.
Strings and bytearrays variable according to length.
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am

Re: Different value types in message?

Postby playstation3Dood » March 24th, 2010, 1:34 am

great to hear about the optimized messages Oliver, so it seems int could be as little as 1 byte. that's great, as most of my messages will probably have ints smaller than 63, as i am using the approach Toby just mentioned, of havng sending indexes rather than full values.

thanks both of you
playstation3Dood
 
Posts: 49
Joined: February 23rd, 2010, 7:39 am

Re: Different value types in message?

Postby NPSF3000 » March 24th, 2010, 7:42 am

Don't worry about it, pretty much everything you will be sending will be an Integer, Single or String. Message frequency and reducing redundancy is enough. Any time spent on choosing a signed byte over a integer for example would be much better spent elsewhere.

If you get massively popular (tens of thousands of users), or really run into some unavoidable bandwidth usage (unlikely) its worthless.
NPSF3000
 
Posts: 41
Joined: March 17th, 2010, 11:14 am


Return to C#



cron