简体   繁体   中英

Kryonet: Best Data Structure?

I'm writing a graphical, online program in Java and using Kryonet library to handle the networking.

I believe this library requires the packets to be instances of classes. I am not sure of this, however have not found any documentation suggesting otherwise.

I know a little bit about networking and memory efficiency but do not understand this library thoroughly and therefore am not sure if standard approaches will be effective.

My best guess for an efficient data structure is to break my Vec2 objects down into floats representing the X and Y properties respectively. Logically, it should be efficient to store these values in a single float array and then transfer that as a packet.

I'm not that familiar with memory management as it pertains to object oriented programming. Since this 'packet' is technically an instance, I'm guessing this would make the 'memory arrangement' more similar to a 'C structure'. In addition, I have no idea how Kryonet handles the data, so it's all very theoretical.

My question is: Would cramming all of the data into a single float array be more efficient than say, arranging all of the properties as individual members of the class? Or perhaps sets of arrays rather than just one?

Thanks in advance.

There is no reason to mangle your fields into an array: the array will lend you nothing in terms of space or time efficiency. The big exception to that is text-based representations such as JSON, which must include the names of "fields" along with their values. I'm specifically referring to binary representations where the structure of the data in question is known to all parties. Based on my very quick look at Kryonet, it looks like the representation is, in fact, a binary one .

As an example, imagine an object with a schema similar to:

{
  x: 5.483,
  y: 0.7245
}

This object has two floating-point fields. A binary representation could be exactly 8 bytes long - four bytes for each float. This assumes all parties are aware that a packet consists of two floats. Alternatively, you could annotate the binary representation with the types. These first version could look something like:

0000000 af40 bc74 393f d578
0000008

On the other hand, a text representation could be something like JSON:

{"x":5.483,"y":0.7245}

This representation has variable size depending on the exact value (not related to magnitude) of the floats in question. Compare this to encoding it as an array in JSON:

[5.483,0.7245]

This would always be smaller than the object variant, but not necessarily shorter than the binary representation. Moreover, in opposition to the binary representation, the text representation has variable size, which would either need to be included (length-prefixed), or marked (with a null character, for instance).

As for network speed, it depends on exactly what you're doing and how you define speed. You could say that speed is the rate at which you can transfer lots of data in terms of bytes/second. You could also say that speed is the amount of time it takes for one packet to reach its destination. Another metric is round-trip time (RTT), which is normally (but not always) roughly twice that of a single trip in either direction between the two programs in question.

If you're trying to maximize the rate of data/time, you'd want data to take up less space - to have a smaller representation of the data. If you're trying to minimize time between points, the same thing goes, but with an important caveat: networking protocols such as TCP generally implement Nagle's algorithm, which will wait to send data for a short period of time and send multiple small packets together (this reduces network congestion and can be disabled).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM