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

后端 未结 1 906
囚心锁ツ
囚心锁ツ 2021-01-03 03:45

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

相关标签:
1条回答
  • 2021-01-03 04:11

    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.

    0 讨论(0)
提交回复
热议问题