How to increase deserialization speed?
Asked Answered
I

6

7

Serializing/deserializing with BinaryFormatter, resulting serialized file is ~80MB in size. The deserialization takes a few minutes. How could I improve on this? Here's the deserialization code:

    public static Universe DeserializeFromFile(string filepath)
    {
        Universe universe = null;

        FileStream fs = new FileStream(filepath, FileMode.Open);

        BinaryFormatter bf = new BinaryFormatter();
        try
        {
            universe = (Universe)bf.Deserialize(fs);
        }
        catch (SerializationException e)
        {
            Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
            throw;
        }
        finally
        {
            fs.Close();
        }

        return universe;
    }

Maybe read all to memory prior to deserializing or use some other serialization technique?

Institutive answered 25/10, 2009 at 11:25 Comment(0)
S
2

Try UnsafeDeserialize. It is said to improve speed.

Smoulder answered 25/10, 2009 at 11:59 Comment(1)
UnsafeDeserialize 460138 ms, Deserialize 459967 ms.. ie. Deserialize was actually faster! I set the headers with the UnsafeDeserialize to null, is this perhaps the reason?Institutive
K
2

I know this is an old question, but stumbled upon a solution that improved my deserialization speed substantially. This is useful if you have large sets of data.

Upgrade your target framework to 4.7.1+ and enable the following switch in your app.config.

<runtime>
    <!-- Use this switch to make BinaryFormatter fast with large object graphs starting with .NET 4.7.2 -->
    <AppContextSwitchOverrides value="Switch.System.Runtime.Serialization.UseNewMaxArraySize=true" />
</runtime>

Sources: BinaryFormatter AppContextSwitchOverrides

Kilar answered 20/12, 2021 at 18:31 Comment(2)
In my case, this slows down serialization speed.Robins
Worked a treat for my application, thank you!Rick
B
0

Please take a look at this thread.

Brieta answered 25/10, 2009 at 11:29 Comment(0)
V
0

Try reading the file into a memory stream first in one go, then deserialize using the memory stream.

Vernievernier answered 25/10, 2009 at 11:53 Comment(1)
If that makes things better rather than worse, then the serialization format sucks. Why do an I/O bound task followed by a CPU-bound task when you could do both, interleaved?Gallardo
L
0

How complex is the data? If it is an object tree (rather than a full graph), then you might get some interesting results from trying protobuf-net. It is generally pretty easy to fit onto existing classes, and is generally much smaller, faster, and less brittle (you can change the object model without trashing the data).

Disclosure: I'm the author, so might be biased - but it really isn't terrible... I'd happily lend some* time to help you try it, though.

*=within reason

Letrice answered 25/10, 2009 at 20:47 Comment(0)
M
-1

Implement ISerializable in the Universe class

Malik answered 25/10, 2009 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.