Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better ways of improving code serialization speed

I have the following code that serializes a List to a byte array for transport via Web Services. The code works relatively fast on smaller entities, but this is a list of 60,000 or so items. It takes several seconds to execute the formatter.Serialize method. Anyway to speed this up?

public static byte[] ToBinary(Object objToBinary)
{
    using (MemoryStream memStream = new MemoryStream())
    {
        BinaryFormatter formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone));
        formatter.Serialize(memStream, objToBinary);
        memStream.Seek(0, SeekOrigin.Begin);
        return memStream.ToArray();
    }
}
like image 751
AngryHacker Avatar asked Jan 01 '26 18:01

AngryHacker


2 Answers

The inefficiency you're experiencing comes from several sources:

  1. The default serialization routine uses reflection to enumerate object fields and get their values.
  2. The binary serialization format stores things in associative lists keyed by the string names of the fields.
  3. You've got a spurious ToArray in there (as Danny mentioned).

You can get a pretty big improvement off the bat by implementing ISerializable on the object type that is contained in your List. That will cut out the default serialization behavior that uses reflection.

You can get a little more speed if you cut down the number of elements in the associative array that holds the serialized data. Make sure the elements you do store in that associative array are primitive types.

Finally, you can eliminate the ToArray but I doubt you'll even notice the bump that gives you.

like image 125
Kennet Belenky Avatar answered Jan 03 '26 07:01

Kennet Belenky


if you want some real serialization speed , consider using protobuf-net which is the c# version of google's protocol buffers. it's supposed to be an order of magnitude faster that binary formatter.

like image 37
geva30 Avatar answered Jan 03 '26 08:01

geva30



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!