Skip to content

Commit d33127c

Browse files
committed
optimized PatternScan ALittleBitNaiveFor
1 parent 09b872e commit d33127c

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

PatternScanBench/Implementations/PatternScanALittleBitNaiveFor.cs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,42 +36,40 @@ internal override long FindPattern(in byte[] cbMemory, in byte[] cbPattern, stri
3636
long ix;
3737
int iy;
3838

39-
List<byte> ls = new List<byte>();
40-
List<int> ini = new List<int>();
39+
List<byte> not0PatternBytesList = new List<byte>();
40+
List<int> not0PatternBytesIndexIndexList = new List<int>();
4141

4242
int dataLength = cbMemory.Length - cbPattern.Length;
4343

4444
for (iy = cbPattern.Length - 1; iy > -1; iy--)
4545
{
4646
if (szMask[iy] == 'x')
4747
{
48-
49-
ls.Add(cbPattern[iy]);
50-
ini.Add(iy);
48+
not0PatternBytesList.Add(cbPattern[iy]);
49+
not0PatternBytesIndexIndexList.Add(iy);
5150
}
5251
}
5352

54-
byte[] arr = ls.ToArray();
55-
byte[] arr1 = new byte[arr.Length];
53+
byte[] not0PatternBytesArray = not0PatternBytesList.ToArray();
54+
int not0PatternBytesL = not0PatternBytesArray.Length;
55+
int[] not0PatternBytesIndexArray = not0PatternBytesIndexIndexList.ToArray();
5656

5757
for (ix = 0; ix < dataLength; ix++)
5858
{
59-
if (arr[arr.Length - 1] == cbMemory[ix])
60-
{
61-
bool check = true;
59+
if (not0PatternBytesArray[not0PatternBytesL - 1] != cbMemory[ix]) continue;
60+
bool check = true;
6261

63-
for (iy = arr.Length - 1; iy > -1; iy--)
64-
{
65-
if (arr[iy] == cbMemory[ix + ini[iy]])
66-
continue;
67-
check = false;
68-
break;
69-
}
62+
for (iy = not0PatternBytesArray.Length - 1; iy > -1; iy--)
63+
{
64+
if (not0PatternBytesArray[iy] == cbMemory[ix + not0PatternBytesIndexArray[iy]])
65+
continue;
66+
check = false;
67+
break;
68+
}
7069

71-
if (check)
72-
{
73-
return ix;
74-
}
70+
if (check)
71+
{
72+
return ix;
7573
}
7674
}
7775

PatternScanBench/PatternScanBench.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<ErrorReport>prompt</ErrorReport>
2525
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
2626
<Prefer32Bit>true</Prefer32Bit>
27+
<LangVersion>7.2</LangVersion>
2728
</PropertyGroup>
2829
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
2930
<OutputPath>bin\x64\Release\</OutputPath>
@@ -34,6 +35,7 @@
3435
<ErrorReport>prompt</ErrorReport>
3536
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
3637
<Prefer32Bit>true</Prefer32Bit>
38+
<LangVersion>7.2</LangVersion>
3739
</PropertyGroup>
3840
<PropertyGroup />
3941
<PropertyGroup>

0 commit comments

Comments
 (0)