Search code examples
c#memory-managementpool

Type reinterpreting and memory pooling in C#


Is it possible to create a buffer of bytes and then use it to allocate different types of data. Also can it be done in a managed way (maybe with span/memory)?

I come from a C++ background where I would create buffers of bytes on the heap and then reuse them to allocate most of my data types. So if what I'm asking is not possible in a safe way, can I allocate memory on the native heap and then reinterpret my data as different types? Are there any type punning or type aliasing rules I should be aware of in C#?


Solution

  • You can do something like "reinterpret cast" as long as it is a blittable type.

    public struct MyPoint
    {
        public int X;
        public int Y;
    }
    ...
    var bytes = new byte[8];
    var points = MemoryMarshal.Cast<byte, MyPoint>(bytes.AsSpan());
    points[0].X = 42; // points to same memory as 'bytes'
    

    But if you start mixing in references this will fail at runtime. So this is only practical in fairly specific optimization scenarios where you have large buffers of some very simple, blittable, data type.

    In general you need to learn how .Net manages memory and try to work with the garbage collector, not reuse C++ practices that do not belong in a managed environment.

    A notable difference between C++ is that small object allocations are very cheap in .Net. It just involves incrementing a pointer in a memory segment and initializing the object header. To some extent you pay for it at garbage collection time, but you only pay for objects that are actually alive, not any garbage. There are plenty of articles digging deeper into memory and performance. The Memory and GC page seem like a decent start.