2012-09-05 2 views
1

내가하려는 것은 간단하지만 그것은 단지 slooow입니다. 기본적으로 데이터 (바이트 배열)를 반복하고, 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 프로젝트에서 사용하고있는 캐시 시스템의 일부분보다 약간 큽니다.

답변

1

RamCache 배열 또는 배열 사본을 정렬하고 Array.BinarySearch을 사용하십시오. 정렬 할 수없는 경우 RamCache의 HashSet을 만듭니다.