I am passed a byte array which is then converted to arrays of primitives using System.Buffer.BlockCopy. Essentially my code looks like this:
void Convert(byte[] b)
{
int[] i1 = new int[100]; // real arrays much larger
double[] d1 = new double[100];
int iPos=0, iSize;
iSize = i1.Length * sizeof(int);
System.Buffer.BlockCopy(b, iPos, i1, 0, iSize);
iPos += iSize;
iSize = d1.Length * sizeof(double);
System.Buffer.BlockCopy(b, iPos, d1, 0, iSize);
iPos += iSize;
//etc: lots of arrays
b=null;
}
This is quite performant, but it's memory usage is obviously 2x the size of my byte array until b is freed.
Is there any way to directly cast sections of the byte array to my primitive arrays? One that does not involve copying the data (therefore doesn't double the memory usage), and presumably is even faster?
double[]and instead word ondouble*. | There is also a really ugly hack that re-interpret casts the arrays, but that's a really bad idea, so you shouldn't use that.Streamsand multi-threading you can make it much faster with smaller memory footprint.