Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to avoid serialization/deserialization and to share big memory object with Memory-mapped files (MMF)?

I need to pass a C# memory object from one process to another (IPC)

I've just tried to serialize this object in a file and then deserialize it in my 2nd process with binary serialization (BinaryFormatter) in order to have good performance.

Unfortunately, the performance is not up to what I expected. As my object has a lot of information, serialization and deserialization takes too much times (the serialization of my object takes more than 1MB on my hard drive).

I have heard of

Memory-mapped files (MMF)

which seems to be one of the fastest method for IPC when the objects to share between process are simple. What is the fastest and easiest way to communicate between 2 processes in C#?

My object is only simple nested struct like this :

public struct Library
{
    public Book[] books;
    public string name;
}

public struct Book
{
    public decimal price;
    public string title;
    public string author;
}

=> Is it possible to avoid serialization/deserialization and share this kind of object with MMF ?

=> What should be the characteristics of the shared object to avoid these operations of sérialization/deserialization ?

One more constraint : My first process is a C# 64 bits process and my 2nd process is a 32 bits one.

Thank you

like image 296
Patrice Pezillier Avatar asked Nov 19 '14 20:11

Patrice Pezillier


1 Answers

You can't directly allocate objects in Memory Mapped File with C# (staying in safe code). So it means you need to have some sort of serialization to transfer of data between two applications.

Broad options:

  1. keep all raw data (more or less as byte array) in the MMF and have C# wrappers to read/write data on demand.
  2. Find faster serialization/build one by hand
  3. Use some form of change tracking and send only diffs between applications.

I'd personally go with option 3 as it will give the most reliable and type safe gains if applicable to particular problem.

like image 133
Alexei Levenkov Avatar answered Nov 02 '22 05:11

Alexei Levenkov