Search code examples
c#classmemorystructmutable

Mutable struct vs. class?


I'm unsure about whether to use a mutable struct or a mutable class. My program stores an array with a lot of objects. I've noticed that using a class doubles the amount of memory needed. However, I want the objects to be mutable, and I've been told that using mutable structs is evil. This is what my type looks like:

struct /* or class */ Block
{
    public byte ID;
    public bool HasMetaData; // not sure whether HasMetaData == false or
                             // MetaData == null is faster, might remove this
    public BlockMetaData MetaData; // BlockMetaData is always a class reference
}

Allocating a large amount of objects like this (notice that both codes below are run 81 times):

// struct
Block[,,] blocks = new Block[16, 256, 16];

uses about 35 MiB of memory, whilst doing it like this:

// class
Block[,,] blocks = new Block[16, 256, 16];
for (int z = 0; z < 16; z++)
for (int y = 0; y < 256; y++)
for (int x = 0; x < 16; x++)
    blocks[x, y, z] = new Block();

uses about 100 MiB of ram.

So to conclude, my question is as follows:

Should I use a struct or a class for my Block type? Instances should be mutable and store a few values plus one object reference.


Solution

  • First off, if you really want to save memory then don't be using a struct or a class.

    byte[,,] blockTypes = new byte[16, 256, 16]; 
    BlockMetaData[,,] blockMetadata = new BlockMetaData[16, 256, 16];
    

    You want to tightly pack similar things together in memory. You never want to put a byte next to a reference in a struct if you can possibly avoid it; such a struct will waste three to seven bytes automatically. References have to be word-aligned in .NET.

    Second, I'm assuming that you're building a voxel system here. There might be a better way to represent the voxels than a 3-d array, depending on their distribution. If you are going to be making a truly enormous number of these things then store them in an immutable octree. By using the persistence properties of the immutable octree you can make cubic structures with quadrillions of voxels in them so long as the universe you are representing is "clumpy". That is, there are large regions of similarity throughout the world. You trade somewhat larger O(lg n) time for accessing and changing elements, but you get to have way, way more elements to work with.

    Third, "ID" is a really bad way to represent the concept of "type". When I see "ID" I assume that the number uniquely identifies the element, not describes it. Consider changing the name to something less confusing.

    Fourth, how many of the elements have metadata? You can probably do far better than an array of references if the number of elements with metadata is small compared to the total number of elements. Consider a sparse array approach; sparse arrays are much more space efficient.