Skip to content

Commit f0f1d0e

Browse files
committed
2 parents eac3abb + f075193 commit f0f1d0e

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,61 +19,55 @@ internal class PatternScanALittleBitNaiveFor
1919
internal static long FindPattern(in byte[] cbMemory, in byte[] cbPattern, string szMask)
2020
{
2121
int cbMemoryL = cbMemory.Length;
22-
int cbPatternL = cbPattern.Length;
23-
int[] cbPatternIndexes = new int[cbPatternL+1];
24-
int[] tcbPatternIndexes = new int[cbPatternL];
25-
2622
ref byte PcbMemory = ref cbMemory[0];
23+
24+
int cbPatternL = cbPattern.Length;
2725
ref byte PcbPattern = ref cbPattern[0];
26+
int[] cbPatternIndexes = new int[cbPatternL + 1];
2827
ref int PcbPatternIndexes = ref cbPatternIndexes[0];
29-
ref int tPcbPatternIndexes = ref tcbPatternIndexes[0];
28+
ref int iPcbPatternIndexes = ref cbPatternIndexes[0];
3029

31-
30+
char[] bMask = szMask.ToCharArray();
31+
byte[] bbMask = new byte[cbPatternL];
32+
ref char PbMask = ref bMask[0];
33+
ref byte PbbMask = ref bbMask[0];
3234

33-
int tcbPatternL= 0;
3435
int l = 0;
3536
for (int i = 0; i < cbPatternL; i++)
3637
{
38+
PbbMask = (byte)PbMask;
3739
l++;
38-
if(szMask[i] == 'x')
40+
if (PbbMask == 120)
3941
{
40-
tcbPatternL++;
41-
PcbPatternIndexes = l;
42-
PcbPatternIndexes = ref Unsafe.Add(ref PcbPatternIndexes, 1);
43-
l = 0;
42+
iPcbPatternIndexes = l;
43+
iPcbPatternIndexes = ref Unsafe.Add(ref iPcbPatternIndexes, 1);
44+
l = 0;
4445
}
46+
PbMask = ref Unsafe.Add(ref PbMask, 1);
47+
PbbMask = ref Unsafe.Add(ref PbbMask, 1);
4548
}
4649

47-
PcbPatternIndexes = ref cbPatternIndexes[0];
48-
4950
for (int i = 0; i < cbMemoryL; i++, PcbMemory = ref Unsafe.Add(ref PcbMemory, 1))
5051
{
51-
//if(i == 0x198A9A)
52-
//{
53-
// int k = 10;
54-
//}
55-
if(PcbMemory == PcbPattern)
52+
if (PcbMemory == PcbPattern)
5653
{
5754
ref byte xPcbMemory = ref PcbMemory;
5855
ref byte xPcbPattern = ref PcbPattern;
5956
ref int xPcbPatternIndexes = ref PcbPatternIndexes;
60-
bool check = true;
6157

62-
for (int j = 0; j < tcbPatternL; j++, xPcbPatternIndexes = ref Unsafe.Add(ref xPcbPatternIndexes, 1), xPcbMemory = ref Unsafe.Add(ref xPcbMemory, xPcbPatternIndexes), xPcbPattern = ref Unsafe.Add(ref xPcbPattern, xPcbPatternIndexes))
58+
while (true)
6359
{
64-
if(xPcbMemory != xPcbPattern)
65-
{
66-
check = false;
60+
xPcbPatternIndexes = ref Unsafe.Add(ref xPcbPatternIndexes, 1);
61+
xPcbMemory = ref Unsafe.Add(ref xPcbMemory, xPcbPatternIndexes);
62+
xPcbPattern = ref Unsafe.Add(ref xPcbPattern, xPcbPatternIndexes);
63+
if (xPcbMemory != xPcbPattern)
6764
break;
68-
}
69-
if(j == tcbPatternL -1)
70-
{
71-
if (check) return i;
72-
}
65+
else if ((int)Unsafe.ByteOffset(ref PcbMemory, ref xPcbMemory) == cbPatternL - 1)
66+
return i;
7367
}
7468
}
7569
}
7670
return -1;
7771
}
7872
}
79-
}
73+
}

PatternScanBench/Program.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,11 @@ internal static void Main(string[] args)
305305

306306
if (IntPtr.Size != 8)
307307
throw new PlatformNotSupportedException("Supports x64 only");
308-
308+
//////////////////////////////////////////////////////////////////////////
309+
//Benchmark b = new Benchmark();
310+
//b.ALittleBitNaiveFor();
311+
//return;
312+
///////////////////////////////////////////////////////////////////
309313
Spinner spinner = new();
310314
spinner.Start();
311315

0 commit comments

Comments
 (0)