Saving files in unity: allocation struggles

Hi there,
I really need help to figure out a big issue I can’t find much about anywhere on the internet after weeks of research; how to properly compress chunk into memory and how to properly decompress them. We constantly need to read/write new and old chunk from and to memory, that’s we need a fast and low alloc solution.

What we already have:

  • A method that, given a memory stream, will deserialize the bytes into the chunk class.
  • A working but slow, memory consuming solution using File.ReadAllBytes/File.WriteAllBytes, a lot of stream.ToArrays that create new byte arrays each time they get called. The compression/decompressing is done using gzip streams but they create a lot of garbage too.

Notes:

  • The chunk to unload can have from 1kb to 1mb of data (uncompressed) based on what is stored in it

Have you attached the profiler and got a feeling for what the problem parts are?