Mutable struct vs. class?

后端 未结 3 1318
轮回少年
轮回少年 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: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.

提交回复
热议问题