简体   繁体   中英

How to optimize this further (speed first, then memory)

I have this small code that takes an object and convert it to a byte[]. Using C# 4.0. Can I optimize this further regarding speed then memory usage? Even small changes would be great - calling this several thousand times+ per second.

    public static byte[] ObjectToByteArray(object obj)
    {
        if (obj == null)
            return null;
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream ms = new MemoryStream();

        using (ms)
        {
            bf.Serialize(ms, obj);  
        }

        return ms.ToArray();
    }

If you want to optimize this further, the first thing you need to know is where you are currently spending the most time. Did you run this through a profiler? What were the results?

I would also ask myself "what concrete benefits will optimizing this provide to my users?". In other words, am I optimizing this because, as an Engineer, I want to perfect the code or because it will provide a real benefit to the people I'm delivering the solution to?

I suspect that you are spending most of your time performing the actual serialization, Changing to a different serializer will likely produce the most benefit. See the following question for alternative, faster serializers:

Fast and compact object serialization in .NET

The problem is that BinaryFormatter is using reflection to read the fields of your objects. Assume you have a simple class with 1 field:

[Serializable]
public class Test
{
    public int A;
}

If you serialize an array of those using BinaryFormatter, it will do something like that for each instance of Test:

int val = (int)typeof(Test).GetField("A").GetValue(obj);
var bytes = BitConverter.GetBytes(val);
stream.Write(bytes, 0, bytes.Length);

The calls to GetField() will consume quite a lot of time. You can significantly improve the speed using 3 ways:

  1. Serialize everything manually. Something similar to this code:

     void SimpleSerialize(Stream stream, Test[] arr) { foreach (var obj in arr) { var bytes = BitConverter.GetBytes(obj.A); stream.Write(bytes, 0, bytes.Length); } } 
  2. Generate a custom serialization class on-the-fly using Reflection.Emit functionality. This is more generic and "clean", but requires a lot of effort.

  3. If you're fine with it, use some third-party serializer that suits your needs.

There's really not much you could do to improve this code. If you are dealing with simple objects you could improve this code by replacing the BinaryFormatter with a BinaryWriter and manually serializing each field/property which would be the fastest serialization you could get. But that's an extreme measure you should take only if you have experimentally determined that this code is somehow a bottleneck for your application. Otherwise you might be wasting your time doing micro-optimizations.

Define a ThreadStatic byte[] as an instance field and use it as a cache. Wrap your memory stream around this cache object. When you use MemoryStream as written in your code, you will be performing tons of allocation, re-allocation, and array copying steps, as MemoryStream will be resizable. There is also the cost of garbage collection to consider.

Possibly, though I'm not sure how well this will work, cache both your formatting objects as ThreadStatic instance fields.

PS, I'm sure it's a mistake, but notice that you're disposing of the MemoryStream before using it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM