Page 1 of 1

Best practices for reading and writing byte arrays

Posted: Tue Dec 24, 2019 6:01 am
by MaximumFirecracker
I'm getting some slow performance when reading and writing large byte arrays using ES2. I notice in the code that ES2 seems to write and read/parse one byte at the time using a loop. Clearly, since System.IO.File.ReadAllBytes is much, much faster, ES2 should be able to read and write byte arrays very fast, too.

Is there any best practices for this? Currently, I've been using ReadArray<byte> and WriteArray<byte>, but maybe there's a better way for byte arrays?

Thank you.

All the best and Merry Xmas!

Re: Best practices for reading and writing byte arrays

Posted: Tue Dec 24, 2019 8:51 am
by Joel
Hi there,

You should use ES2.SaveRaw and ES2.LoadRaw if you want to write bytes directly to a file.

All the best,
Joel

Re: Best practices for reading and writing byte arrays

Posted: Tue Dec 24, 2019 9:13 pm
by MaximumFirecracker
I need this to be part of a ES2Writer and have it retrievable by ES2Data. Is it not possible to WriteRaw with tag?

Re: Best practices for reading and writing byte arrays

Posted: Wed Dec 25, 2019 11:07 pm
by MaximumFirecracker
I suppose one could create a custom ES2Type wrapper class for byte[] that uses writer.WriteRaw for the byte array. Then you would be able to save byte arrays fast and with tag support.

Re: Best practices for reading and writing byte arrays

Posted: Thu Dec 26, 2019 5:24 am
by MaximumFirecracker
I added a custom wrapper type and some extension methods, so now I can save byte arrays fast with ES2Writer.SaveBytes and ES2Data.LoadBytes. Speedup is around 10x.

All the best.
Happy new year in advance!

Re: Best practices for reading and writing byte arrays

Posted: Fri Dec 27, 2019 11:58 am
by Joel
MaximumFirecracker wrote:I added a custom wrapper type and some extension methods, so now I can save byte arrays fast with ES2Writer.SaveBytes and ES2Data.LoadBytes. Speedup is around 10x.

All the best.
Happy new year in advance!
This is what I would have suggested, so glad you came across this before the Christmas break got in the way :)

Happy new year in advance to yourself also!

All the best,
Joel