Mutable struct vs. class?

后端 未结 3 1320
轮回少年
轮回少年 2021-02-11 00:33

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 me

相关标签:
3条回答
  • 2021-02-11 01:21

    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.

    0 讨论(0)
  • 2021-02-11 01:22

    An array of structs will offer better storage efficiency than an array of immutable references to distinct class instances having the same fields, because the latter will require all of the memory required by the former, in addition to memory to manage the class instances and memory required to hold the references. All that having been said, your struct as designed has a very inefficient layout. If you're really concerned about space, and every item in fact needs to independently store a byte, a Boolean, and a class reference, your best bet may be to either have two arrays of byte (a byte is actually smaller than a Boolean) and an array of class references, or else have an array of bytes, an array with 1/32 as many elements of something like BitVector32, and an array of class references.

    0 讨论(0)
  • 2021-02-11 01:24

    Do they really have to be mutable? You could always make it an immutable struct with methods to create a new value with one field different:

    struct Block
    {
        // I'd definitely get rid of the HasMetaData
        private readonly byte id;
        private readonly BlockMetaData metaData;
    
        public Block(byte id, BlockMetaData metaData)
        {
            this.id = id;
            this.metaData = metaData;
        }
    
        public byte Id { get { return id; } }
        public BlockMetaData MetaData { get { return metaData; } }
    
        public Block WithId(byte newId)
        {
            return new Block(newId, metaData);
        }
    
        public Block WithMetaData(BlockMetaData newMetaData)
        {
            return new Block(id, newMetaData);
        }
    }
    

    I'm still not sure whether I'd make it a struct, to be honest - but I'd try to make it immutable either way, I suspect.

    What are your performance requirements in terms of both memory and speed? How close does an immutable class come to those requirements?

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