Fast serialization/deserialization of structs

Posted by user256890 on Stack Overflow See other posts from Stack Overflow or by user256890
Published on 2012-03-30T14:32:10Z Indexed on 2012/03/30 17:29 UTC
Read the original article Hit count: 177

Filed under:
|
|
|

I have huge amont of geographic data represented in simple object structure consisting only structs. All of my fields are of value type.

public struct Child
{
   readonly float X;
   readonly float Y;
   readonly int myField;
}

public struct Parent
{
   readonly int id;
   readonly int field1;
   readonly int field2;
   readonly Child[] children;
}

The data is chunked up nicely to small portions of Parent[]-s. Each array contains a few thousands Parent instances. I have way too much data to keep all in memory, so I need to swap these chunks to disk back and forth. (One file would result approx. 2-300KB).

What would be the most efficient way of serializing/deserializing the Parent[] to a byte[] for dumpint to disk and reading back? Concerning speed, I am particularly interested in fast deserialization, write speed is not that critical.

Would simple BinarySerializer good enough? Or should I hack around with StructLayout (see accepted answer)? I am not sure if that would work with array field of Parent.children.

UPDATE: Response to comments - Yes, the objects are immutable (code updated) and indeed the children field is not value type. 300KB sounds not much but I have zillions of files like that, so speed does matter.

© Stack Overflow or respective owner

Related posts about c#

Related posts about Performance