Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to avoiding copies of data when using memory mapped files in C#?

My understanding of how memory mapped files work in C# is that every request for data results in a copy. For instance, if you had a large data structure persisted as a file, using a memory mapped file would result in memory for the actual file mapped into RAM, and a copy residing in a GC heap once it was read from the file.

I'm assuming this is because pointers and the GC don't get along well together generally speaking.

So, is there any way around this?

  • Perhaps via some mixed mode C++ that can expose a managed API over the memory mapped data?
  • What about direct pointer manipulation with unsafe C#?

The general problem I'm trying to solve is sharing a large data structure between multiple processes. The data structure is used to answer a small set of "questions" that can be exposed as a simple API (i.e. basically, a highly specialized index of a bunch of other data).

On a side note, doesn't this make the .NET API useless for the "sharing large amounts of data" scenario?

like image 894
David Avatar asked Oct 08 '22 20:10

David


1 Answers

You can use unsafe code to directly access the mapped memory. I suggest you look into "blittable structs" which are struct types which can be copied around in memory without modification. Here is an example:

struct MyDataRecord { public int X, Y; }

...

for (var i = 0 .. 10) {
 ((MyDataRecord*)pointerToUnmanagedMemory)[i] = new MyDataRecord() { X = i, Y = i * i };
}

This is very performant and kind of convenient.

like image 79
usr Avatar answered Oct 10 '22 09:10

usr