A way to avoid wasting memory
At the moment, I've got a system in place where I record a gamepad state in a struct and store this in a list of states to record the input for the duration of a videogame. The full state of the pad takes up 192bits per frame but this is a little wasteful. For 开发者_开发百科example, if the analog triggers on the pad aren't being pressed they still take 32bits up, each, in storage. So obviously I'm looking to save some space.
I had tried setting this to NULL but it appears to have no effect on the size of the binary file that the system saves at the end of the match.
What alternatives are available in a situation where you only need to record or save certain values but retain the integrity of a data structure?
EDIT:
I think I've found a solution; of sorts. Earlier I tried setting values to NULL by overriding the standard float with System.Nullable in the struct. My idea is that setting this to a NULL value in the struct will serialize much smaller. I'm under the impression that NULL is recorded as 4bits. It might be more, it probably is. Anyway, the code I had earlier had a rather glaringly obvious fault so I've gone back and fixed it again. Now I'm getting much much smaller replays and the accuracy seems to be just as good so I'm going to assume that the NULL trickery is doing something properly.
Not in a struct, I think. You may need a bit more complicated protocol.
For example (very rough and not thought through) (WHAT 3xough):
The first byte contains the state of 8 buttons, one bit for each button. If a bit for a button is set to 1 the client (I call it the client for now) expects more specific data for that button, like direction and such, in a specific order.
This is what you do.
You use a bit stream to only write the bits of information that makes a difference. In the case where you have lots of sparse data you write out a bit mask before anything else, that tells you which of the following bits contain data.
This way, something like a vector can be reduced to just 3 zero bits. If the vector is empty/zero. If only one component of the vector is valid then that's 3 bits + only the size of that component. On top of this you can of course add compression, however, at some point the distribution of bits (when your being cleaver about it) becomes very even. This makes general purpose compression methods less optimal and they might even increase the size of your output instead.
And what you do is that you periodically write this stuff out to disk, so that you never keep a huge chuck of it in memory.
It's possible to split your struct and/or use a compression.
But the major saving could be to save during the game, not all at the end.
You could store only deltas maybe? In many situations, that gives a nice compression.
For your bit type states, it doesn't change size, only meaning.
For your float types though, it reduces to 1 bit in most cases, and only increase by 1 bit in the other cases.
精彩评论