私がやろうとしていることは単純ですが、ただ遅いだけです。基本的に、データ (バイト配列) をループし、一部を INT に変換してから、RamCache と比較します。これもバイト配列です。それを INT に変換する理由は、それが 4 バイトであるためです。RamCache 配列の一部で 4 バイトが等しい場合、すでに 4 つの長さが等しいことがわかっています。そしてそこから、等しいバイト数を確認できます。
要するに、このコードがしなければならないこと:
データ配列をループして 4 バイトを取得し、RamCache 配列に含まれているかどうかを調べます。現在、データ配列と RamCache 配列に 65535 バイトが含まれている場合、以下のコードは遅くなります。
private unsafe SmartCacheInfo[] FindInCache(byte[] data, Action<SmartCacheInfo> callback)
{
List<SmartCacheInfo> ret = new List<SmartCacheInfo>();
fixed (byte* x = &(data[0]), XcachePtr = &(RamCache[0]))
{
Int32 Loops = data.Length >> 2;
int* cachePtr = (int*)XcachePtr;
int* dataPtr = (int*)x;
if (IndexWritten == 0)
return new SmartCacheInfo[0];
//this part is just horrible slow
for (int i = 0; i < data.Length; i++)
{
if (((data.Length - i) >> 2) == 0)
break;
int index = -1;
dataPtr = (int*)(x + i);
//get the index, alot faster then List.IndexOf
for (int j = 0; ; j++)
{
if (((IndexWritten - j) >> 2) == 0)
break;
if (dataPtr[0] == ((int*)(XcachePtr + j))[0])
{
index = j;
break;
}
}
if (index == -1)
{
//int not found, lets see how
SmartCacheInfo inf = new SmartCacheInfo(-1, i, 4, false);
inf.instruction = Instruction.NEWDATA;
i += inf.Length - 1; //-1... loop does +1
callback(inf);
}
else
{
SmartCacheInfo inf = new SmartCacheInfo(index, i, 0, true); //0 index for now just see what the length is of the MemCmp
inf.Length = MemCmp(data, i, RamCache, index);
ret.Add(inf);
i += inf.Length - 1; //-1... loop does +1
}
}
}
return ret.ToArray();
}
二重ループは、それを非常に遅くしているものです。データ配列には 65535 バイトが含まれているため、RamCache 配列も同様です。このコードは、私の SSP プロジェクトで使用しているキャッシュ システムの一部です。