.Net data structure storing heterogeneous structures in memory

I am looking for a .Net data structure that keeps heterogeneous structures continuous in memory to be cpu-cache-friendly.

This type of data structure is explained in this blog post: T-machine.org in Iteration 4 .

In .Net, an array of value types (structs) keeps data contiguous in memory, but this only works for a non-shared array. I tried to create ValueType[], but structs boxed. Thus, links are contiguous in memory, but not real data.

After many attempts, I do not think this is possible initially in .Net. The only possible solution that I see is the manual control of the serialization and deserialization of structures in an array of bytes, but I do not think it will be done.

Did you find your own solution? or the best solution that is mine?

Edit 1: I am trying to implement a component entity system as described on the T-Machine.org blog .

+4
source share
1 answer

No. In C #, there is no way to iterate 4. You can not decide where in memory will be placed .NET structor class. There is nothing like posting new C ++ .

But note that even Iteration 4 seems to have more problems than solutions:

At this stage, our iterations are not bad, but they see some recurring problems:

  • Redistributing arrays when adding / removing components (Ive not considered this above - if you are not familiar with the problem, google "C dynamic array")
  • ( Iteration 1, , , )
  • ( )

struct , ...

public enum StructType
{
    Velocity = 0,
    Position = 1,
    Foo = 2,
    Bar = 3,
}

public struct Velocity
{
    public int Vx;
    public int Vy;
}

public struct Position
{
    public int X;
    public int Y;
    public int Z;
}

public struct Foo
{
    public double Weight;
    public double Height;
    public int Age;
}

public struct Bar
{
    public int ColorR;
    public int ColorG;
    public int ColorB;
    public int Transparency;
}

[StructLayout(LayoutKind.Explicit)]
public struct SuperStruct
{
    [FieldOffset(0)]
    public StructType StructType;

    [FieldOffset(4)]
    public Velocity Velocity;

    [FieldOffset(4)]
    public Position Position;

    [FieldOffset(4)]
    public Foo Foo;

    [FieldOffset(4)]
    public Bar Bar;
}

"" # C. , FixedLayout FieldOffset, . , , , , SuperStruct . 32 , Foo 20 , 8 .

, SuperStruct. , , Iterion 4, StructType , .

+2

All Articles