可变结构与类? [英] Mutable struct vs. class?

查看:209
本文介绍了可变结构与类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定是否使用一个可变的结构或可变类。
我的程序存储了很多对象的数组。
我发现使用类需要加倍的内存量。不过,我想的对象是可变的,我一直在说,使用可变结构是邪恶的。
这是我喜欢的类型如下:

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
}



分配大量像这样的对象(请注意,以下两个代码运行81次):

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];



使用的内存大约35 MIB,而这样做是这样的:

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();



使用RAM约100 MIB。

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.

推荐答案

首先,如果你真的想节省内存则不要使用一个结构或类。

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];

您想要的紧紧包类似的东西一起在内存的。你永远要放在旁边,如果你都不可能避免一个结构的引用一个字节;这样的结构会自动浪费三至七个字节。 引用必须是字对齐的.NET。

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.

二,我假设你在这里建设一个体素系统。有可能是表示比一个三维阵列的体素,根据它们的分布的更好的方法。如果你将要做出的这些东西一个真正的数量庞大然后将它们存储在一成不变的八叉树。通过使用的持久的不可改变的八叉树的特性可以使立方结构在他们的体素的 quadrillions 的,只要您所代表的宇宙是块状。也就是说,有相似的大区域遍布世界各地。你交易稍大O(LG n)的时间用于访问和变化的元素,但你有办法,办法更多元素的工作。

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.

三,ID就是代表型的概念,一个非常糟糕的方式。当我看到ID我认为该编号唯一的标识的元素,不是的描述的它。考虑更改名称,东西少混乱。

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.

这篇关于可变结构与类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆