[2011-07-19 更新]
现在有一个新版本可用:http://www.mediafire.com/file/74fxj7u1n0ppcq9/MemStorageDemo-6639584-2011_07_19-12_47_00.zip
我仍在尝试调试一些令人讨厌的引用计数,但是从一个新的 xUnit 会话中,我能够运行一个创建 1000 万个对象的测试(它发生在我身上,我现在减少了字符串大小用于测试,但我让它运行了 1000 万个可变长度的字符串,长度从 3 到 15 个字节,我还没有机会尝试比这更大。在我的系统上,我的负载从大约 ~1.95G 到 ~2.35G有了 1000 万个对象,除了使用实际托管字符串的非常简单的支持类之外,我还没有对字符串做任何事情。
无论如何,所以,我认为它工作得相当好,尽管在后备存储上肯定还有优化工作要做,而且我还认为必要时可以在迭代器上做一些工作,这取决于你有多少数据一次处理。不幸的是,直到明天晚些时候或第二天才能再次查看它。
无论如何,这是基本的想法:
-
MemoryArray 类:使用Marhsal.AllocHGlobal() 分配的非托管内存来存储结构。我正在使用一个大的MemoryArray 进行测试,但就成员而言,只有几个字段,我认为只要你保持数组大小相当大,拆分它的内存消耗不会有太大差异。 MemoryArray 实现了 IEnumerable<MemoryArrayItem>,这是我在测试中用来填充数组的方法。
MemoryArray 用于保存来自任何被支持对象的常规大小的数据块。您可以使用我尚未实现的指针数学对枚举器执行一些操作。我目前每次都返回新对象,所以这是我相信的很大一部分。在我基于此原型的原型类中,我能够使用非常常规的指针数学来进行遍历,但是我认为我这样做的方式主要用于非常快速的遍历,而可能不适用于互操作性。
MemoryArray 仅有一个标准索引器,它使用 IntPtr 上的指针数学获取请求的元素,该指针表示在构造时分配的非托管数据的头部。我还实现了一个名义上的 2D 索引器,您可以在其中将表示维度的整数数组传递给数组,然后可以对其执行 A[x,y]。这只是一个简单的小例子,说明它是如何工作的。
我还没有实现的一件事是任何形式的小节,但我确实认为小节适合该项目,所以我可能会在有机会时实现它。
MemoryArrayEnumerator 类:我选择实现实际的枚举器而不是枚举器函数。枚举器类基本上只需要一个MemoryArray,然后为枚举器提供库存函数以返回实际的MemoryArrayItemobjects。
MemoryArrayItem 类:除了根据数组中的开始和位置保存适当的指针信息外,该类并没有做太多事情。是在这个对象之上(在旁边?)实现的特定类实际上执行指针的工作以获取数据。
然后还有一些支持类,MemoryStringArray 是可变大小的内存支持块,暂时只处理字符串,然后是自动处理类(AutoDisposer)和一个泛型类来处理连接和分离。 (AutoReference<>)。
现在,在这个基础之上是特定的类。这三种类型(数组/枚举器/项)中的每一种都是专门为您正在查看的对象实现的。在我放弃的这个项目的更大版本中,这是一个峰值,我对偏移量有更通用的处理,这样你就不会那么依赖于具体的类,但即使它们是很有用;我最初的实现都是没有真正基础的具体类。
目前,我将它们全部实现为您将引用传递给的单独类;因此,TestArray 类在其构造函数中传递了一个 MemoryArray 类。与枚举数和项目相同。我知道那里会节省一些处理能力,我认为如果我能找到一种将它们作为后代实现的好方法,而不仅仅是拥有底层类的副本,我认为也很有可能节省空间。不过,我想先获得它的基本感觉,这似乎是最直接的方法。问题是它是另一层间接性。
TestArray 和 TestArrayEnumerator 除了通过 MemoryArray 和 MemoryArrayEnumerator 的功能外,并没有做太多事情。这些类中的主要问题只是将指针分割并传递到使用它的项目中。
但是,TestArrayItem 是指针实际转换为真实数据的地方;这是文件。我剪掉了 cmets 的很大一部分,它们通过一些选项来更好地处理可变长度后备存储(仍然在上面给出的链接中的实际文件中),请原谅我给自己留下注释的大量 cmets我在做这件事时的想法:)
TestArrayItem.cs
// ----------------------------------------------
// rights lavished upon all with love
// see 'license/unlicense.txt'
// ♥ 2011, shelley butterfly - public domain
// ----------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MemStorageDemo
{
public unsafe class TestArrayItem
{
public static MemoryStringArray s_MemoryArrayStore = new MemoryStringArray();
public static int TestArrayItemSize_bytes =
sizeof(double) * 2
+ sizeof(int)
+ sizeof(int);
// hard-coding here; this is another place that things could be a little more generic if you wanted and if
// performance permitted; for instance, creating a dictionary of offsets based on index. also, perhaps
// a dictionary with key strings to allow indexing using field names of the object.
private enum EFieldOffset
{
DoubleTheFirstOffset = 0,
DoubleTheSecondOffset = 8,
IntTheFirstOffset = 16,
StringTheFirstHandleOffset = 20
}
private MemoryArrayItem myMemoryArrayItem;
private MemoryStringArray myStringStore;
// constructor that uses the static string array store
public TestArrayItem(MemoryArrayItem parMemoryArrayItem) :
this(parMemoryArrayItem, s_MemoryArrayStore)
{
}
// constructor for getting the item at its memory block without any initialization (e.g. existing item)
public TestArrayItem(MemoryArrayItem parMemoryArrayItem, MemoryStringArray parStringStore)
{
myMemoryArrayItem = parMemoryArrayItem;
myStringStore = parStringStore;
}
// constructor for geting the item at its memory block and initializing it (e.g. adding new items)
public TestArrayItem(MemoryArrayItem parMemoryArrayItem, double parDoubleTheFirst, double parDoubleTheSecond, int parIntTheFirst, string parStringTheFirst)
{
myMemoryArrayItem = parMemoryArrayItem;
DoubleTheFirst = parDoubleTheFirst;
DoubleTheSecond = parDoubleTheSecond;
IntTheFirst = parIntTheFirst;
StringTheFirst = parStringTheFirst;
}
// if you end up in a situation where the compiler isn't giving you equivalent performance to just doing
// the array math directly in the properties, you could always just do the math directly in the properties.
//
// it reads much cleaner the way i have it set up, and there's a lot less code duplication, so without
// actually determining empirically that i needed to do so, i would stick with the function calls.
private IntPtr GetPointerAtOffset(EFieldOffset parFieldOffset)
{ return myMemoryArrayItem.ObjectPointer + (int)parFieldOffset; }
private double* DoubleTheFirstPtr
{ get { return (double*)GetPointerAtOffset(EFieldOffset.DoubleTheFirstOffset); } }
public double DoubleTheFirst
{
get
{
return *DoubleTheFirstPtr;
}
set
{
*DoubleTheFirstPtr = value;
}
}
private double* DoubleTheSecondPtr
{ get { return (double*)GetPointerAtOffset(EFieldOffset.DoubleTheSecondOffset); } }
public double DoubleTheSecond
{
get
{
return *DoubleTheSecondPtr;
}
set
{
*DoubleTheSecondPtr = value;
}
}
// ahh wishing for a preprocessor about now
private int* IntTheFirstPtr
{ get { return (int*)GetPointerAtOffset(EFieldOffset.IntTheFirstOffset); } }
public int IntTheFirst
{
get
{
return *IntTheFirstPtr;
}
set
{
*IntTheFirstPtr = value;
}
}
// okay since we're using the StringArray backing store in the example, we just need to get the
// pointer stored in our blocks, and then copy the data from that address
private int* StringTheFirstHandlePtr
{ get { return (int*)GetPointerAtOffset(EFieldOffset.StringTheFirstHandleOffset); } }
public string StringTheFirst
{
get
{
return myStringStore.GetString(*StringTheFirstHandlePtr);
}
set
{
myStringStore.ModifyString(*StringTheFirstHandlePtr, value);
}
}
public void CreateStringTheFirst(string WithValue)
{
*StringTheFirstHandlePtr = myStringStore.AddString(WithValue);
}
public override string ToString()
{
return string.Format("{0:X8}: {{ {1:0.000}, {2:0.000}, {3}, {4} }} {5:X8}", (int)DoubleTheFirstPtr, DoubleTheFirst, DoubleTheSecond, IntTheFirst, StringTheFirst, (int)myMemoryArrayItem.ObjectPointer);
}
}
}
所以,这才是真正的魔力;只是基本上实现了根据有关字段的信息找出正确指针的函数。就目前而言,我认为它是代码生成的一个很好的候选者,好吧,假设我让附加/分离的东西正常工作。我隐藏了很多必须使用自动类型指针进行手动内存管理的事情,我认为从长远来看它值得调试......
不管怎样,就是这样,我希望今晚或明天能回来上网,我会尽力检查。不幸的是,我要把电缆调制解调器还给有线电视公司所以不会除非我们去麦当劳或其他什么地方,否则做生意:)希望这对您有所帮助;我将用它调试问题,以便至少有一个功能基础可供使用;我知道这不是我第一次考虑编写这样的库,我想其他人也有。
以前的内容
我使用类似这样的东西来使用我创建的 COM 库与预编译的 win32/64 FFTW dll 互操作。对于这个问题,我们确实需要比我所拥有的更通用的东西,所以我上周开始研究一些足以作为这些类型用途的体面通用库的东西,具有可扩展的内存管理、多维、切片、等等。
好吧,我昨天终于对自己承认(a)它还需要几天才能准备好,并且(b)我需要一个尖峰解决方案来弄清楚最后的一些问题。所以,我决定在较低的抽象层次上进行第一次切割,试图满足您问题中解决的需求。不幸的是,我们要搬家了,我必须做一些打包工作,但我认为这可能足以解决您的问题。
本周我将继续研究该示例,我将在此处更新新代码,我将尝试从中提取足够的信息以发表解释它的帖子,但如果您仍然感兴趣,这里是最后我可能会在本周晚些时候发布:
http://www.mediafire.com/file/a7yq53ls18q7bvf/EfficientStorage-6639584.zip
它只是一个 VS2010 解决方案,具有做我认为满足您需求的基本必需品。有很多 cmets 散布在各处,但请随时提出问题,我会在有互联网后立即回来查看……无论哪种方式,这都是一个有趣的项目。
完成示例后,我打算完成第一次迭代完整库并将其发布到某个地方;当我有链接时,我会在这里更新。
强制警告:它可以编译但未经测试;我敢肯定有问题,这是一个相当大的话题。