Skip to content

Commit 38af5d0

Browse files
committed
Use unsafe methods to improve performance.
1 parent 705ea3c commit 38af5d0

12 files changed

+117
-104
lines changed

ReClass.NET/MemoryScanner/BytePattern.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Linq;
66
using System.Text;
77
using ReClassNET.Extensions;
8-
using ReClassNET.Util;
98

109
namespace ReClassNET.MemoryScanner
1110
{
@@ -223,6 +222,21 @@ public bool Equals(byte[] data, int index)
223222
return true;
224223
}
225224

225+
public unsafe bool Equals(byte* data)
226+
{
227+
Contract.Requires(data != null);
228+
229+
for (var j = 0; j < pattern.Count; ++j)
230+
{
231+
if (!pattern[j].Equals(*(data + j)))
232+
{
233+
return false;
234+
}
235+
}
236+
237+
return true;
238+
}
239+
226240
/// <summary>
227241
/// Converts this <see cref="BytePattern"/> to a byte array.
228242
/// </summary>

ReClass.NET/MemoryScanner/Comparer/ArrayOfBytesMemoryComparer.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Diagnostics;
1+
using System.Diagnostics;
32
using System.Diagnostics.Contracts;
43

54
namespace ReClassNET.MemoryScanner.Comparer
@@ -31,39 +30,45 @@ public ArrayOfBytesMemoryComparer(byte[] pattern)
3130
byteArray = pattern;
3231
}
3332

34-
public bool Compare(byte[] data, int index, out ScanResult result)
33+
public unsafe bool Compare(byte* data, out ScanResult result)
3534
{
3635
result = null;
3736

3837
if (byteArray != null)
3938
{
4039
for (var i = 0; i < byteArray.Length; ++i)
4140
{
42-
if (data[index + i] != byteArray[i])
41+
if (*(data + i) != byteArray[i])
4342
{
4443
return false;
4544
}
4645
}
4746
}
48-
else if (!bytePattern.Equals(data, index))
47+
else if (!bytePattern.Equals(data))
4948
{
5049
return false;
5150
}
5251

5352
var temp = new byte[ValueSize];
54-
Array.Copy(data, index, temp, 0, temp.Length);
53+
fixed (byte* cpy = &temp[0])
54+
{
55+
for (var i = 0; i < ValueSize; ++i)
56+
{
57+
*(cpy + i) = *(data + i);
58+
}
59+
}
5560
result = new ArrayOfBytesScanResult(temp);
5661

5762
return true;
5863
}
5964

60-
public bool Compare(byte[] data, int index, ScanResult previous, out ScanResult result)
65+
public unsafe bool Compare(byte* data, ScanResult previous, out ScanResult result)
6166
{
6267
#if DEBUG
6368
Debug.Assert(previous is ArrayOfBytesScanResult);
6469
#endif
6570

66-
return Compare(data, index, out result);
71+
return Compare(data, out result);
6772
}
6873
}
6974
}

ReClass.NET/MemoryScanner/Comparer/ByteMemoryComparer.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public ByteMemoryComparer(ScanCompareType compareType, byte value1, byte value2)
2626
Value2 = value2;
2727
}
2828

29-
public bool Compare(byte[] data, int index, out ScanResult result)
29+
public unsafe bool Compare(byte* data, out ScanResult result)
3030
{
3131
result = null;
3232

33-
var value = data[index];
33+
var value = *data;
3434

3535
bool IsMatch()
3636
{
@@ -69,20 +69,20 @@ bool IsMatch()
6969
return true;
7070
}
7171

72-
public bool Compare(byte[] data, int index, ScanResult previous, out ScanResult result)
72+
public unsafe bool Compare(byte* data, ScanResult previous, out ScanResult result)
7373
{
7474
#if DEBUG
7575
Debug.Assert(previous is ByteScanResult);
7676
#endif
7777

78-
return Compare(data, index, (ByteScanResult)previous, out result);
78+
return Compare(data, (ByteScanResult)previous, out result);
7979
}
8080

81-
public bool Compare(byte[] data, int index, ByteScanResult previous, out ScanResult result)
81+
public unsafe bool Compare(byte* data, ByteScanResult previous, out ScanResult result)
8282
{
8383
result = null;
8484

85-
var value = data[index];
85+
var value = *data;
8686

8787
bool IsMatch()
8888
{

ReClass.NET/MemoryScanner/Comparer/DoubleMemoryComparer.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ private bool CheckRoundedEquality(double value)
5555
}
5656
}
5757

58-
public bool Compare(byte[] data, int index, out ScanResult result)
58+
public unsafe bool Compare(byte* data, out ScanResult result)
5959
{
6060
result = null;
6161

62-
var value = BitConverter.ToDouble(data, index);
62+
var value = *(double*)data;
6363

6464
bool IsMatch()
6565
{
@@ -98,20 +98,20 @@ bool IsMatch()
9898
return true;
9999
}
100100

101-
public bool Compare(byte[] data, int index, ScanResult previous, out ScanResult result)
101+
public unsafe bool Compare(byte* data, ScanResult previous, out ScanResult result)
102102
{
103103
#if DEBUG
104104
Debug.Assert(previous is DoubleScanResult);
105105
#endif
106106

107-
return Compare(data, index, (DoubleScanResult)previous, out result);
107+
return Compare(data, (DoubleScanResult)previous, out result);
108108
}
109109

110-
public bool Compare(byte[] data, int index, DoubleScanResult previous, out ScanResult result)
110+
public unsafe bool Compare(byte* data, DoubleScanResult previous, out ScanResult result)
111111
{
112112
result = null;
113113

114-
var value = BitConverter.ToDouble(data, index);
114+
var value = *(double*)data;
115115

116116
bool IsMatch()
117117
{

ReClass.NET/MemoryScanner/Comparer/FloatMemoryComparer.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ private bool CheckRoundedEquality(float value)
5555
}
5656
}
5757

58-
public bool Compare(byte[] data, int index, out ScanResult result)
58+
public unsafe bool Compare(byte* data, out ScanResult result)
5959
{
6060
result = null;
6161

62-
var value = BitConverter.ToSingle(data, index);
62+
var value = *(float*)data;
6363

6464
bool IsMatch()
6565
{
@@ -98,20 +98,20 @@ bool IsMatch()
9898
return true;
9999
}
100100

101-
public bool Compare(byte[] data, int index, ScanResult previous, out ScanResult result)
101+
public unsafe bool Compare(byte* data, ScanResult previous, out ScanResult result)
102102
{
103103
#if DEBUG
104104
Debug.Assert(previous is FloatScanResult);
105105
#endif
106106

107-
return Compare(data, index, (FloatScanResult)previous, out result);
107+
return Compare(data, (FloatScanResult)previous, out result);
108108
}
109109

110-
public bool Compare(byte[] data, int index, FloatScanResult previous, out ScanResult result)
110+
public unsafe bool Compare(byte* data, FloatScanResult previous, out ScanResult result)
111111
{
112112
result = null;
113113

114-
var value = BitConverter.ToSingle(data, index);
114+
var value = *(float*)data;
115115

116116
bool IsMatch()
117117
{

ReClass.NET/MemoryScanner/Comparer/IScanComparer.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,19 @@ public interface IScanComparer
1313
/// Compares the data at the provided index to the current <see cref="CompareType"/>.
1414
/// </summary>
1515
/// <param name="data">The byte array to be compared.</param>
16-
/// <param name="index">The index into the byte array.</param>
1716
/// <param name="result">[out] The scan result if the <see cref="CompareType"/> matched.</param>
1817
/// <returns>True if matched.</returns>
19-
bool Compare(byte[] data, int index, out ScanResult result);
18+
unsafe bool Compare(byte* data, out ScanResult result);
2019

2120
/// <summary>
2221
/// Compares the data at the provided index to the current <see cref="CompareType"/>.
2322
/// The previous results may be used.
2423
/// </summary>
2524
/// <param name="data">The byte array to be compared.</param>
26-
/// <param name="index">The index into the byte array.</param>
2725
/// <param name="previous">Scan result to be compared.</param>
2826
/// <param name="result">[out] The scan result if the <see cref="CompareType"/> matched.</param>
2927
/// <returns>True if matched.</returns>
30-
bool Compare(byte[] data, int index, ScanResult previous, out ScanResult result);
28+
unsafe bool Compare(byte* data, ScanResult previous, out ScanResult result);
3129
}
3230

3331
[ContractClassFor(typeof(IScanComparer))]
@@ -44,18 +42,13 @@ public int ValueSize
4442
throw new NotImplementedException();
4543
}
4644
}
47-
public bool Compare(byte[] data, int index, out ScanResult result)
45+
public unsafe bool Compare(byte* data, out ScanResult result)
4846
{
49-
Contract.Requires(data != null);
50-
Contract.Requires(index >= 0);
51-
5247
throw new NotImplementedException();
5348
}
5449

55-
public bool Compare(byte[] data, int index, ScanResult previous, out ScanResult result)
50+
public unsafe bool Compare(byte* data, ScanResult previous, out ScanResult result)
5651
{
57-
Contract.Requires(data != null);
58-
Contract.Requires(index >= 0);
5952
Contract.Requires(previous != null);
6053

6154
throw new NotImplementedException();

ReClass.NET/MemoryScanner/Comparer/IntegerMemoryComparer.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Diagnostics;
1+
using System.Diagnostics;
32
using ReClassNET.Util;
43

54
namespace ReClassNET.MemoryScanner.Comparer
@@ -27,11 +26,11 @@ public IntegerMemoryComparer(ScanCompareType compareType, int value1, int value2
2726
Value2 = value2;
2827
}
2928

30-
public bool Compare(byte[] data, int index, out ScanResult result)
29+
public unsafe bool Compare(byte* data, out ScanResult result)
3130
{
3231
result = null;
3332

34-
var value = BitConverter.ToInt32(data, index);
33+
var value = *(int*)data;
3534

3635
bool IsMatch()
3736
{
@@ -70,20 +69,20 @@ bool IsMatch()
7069
return true;
7170
}
7271

73-
public bool Compare(byte[] data, int index, ScanResult previous, out ScanResult result)
72+
public unsafe bool Compare(byte* data, ScanResult previous, out ScanResult result)
7473
{
7574
#if DEBUG
7675
Debug.Assert(previous is IntegerScanResult);
7776
#endif
7877

79-
return Compare(data, index, (IntegerScanResult)previous, out result);
78+
return Compare(data, (IntegerScanResult)previous, out result);
8079
}
8180

82-
public bool Compare(byte[] data, int index, IntegerScanResult previous, out ScanResult result)
81+
public unsafe bool Compare(byte* data, IntegerScanResult previous, out ScanResult result)
8382
{
8483
result = null;
8584

86-
var value = BitConverter.ToInt32(data, index);
85+
var value = *(int*)data;
8786

8887
bool IsMatch()
8988
{

ReClass.NET/MemoryScanner/Comparer/LongMemoryComparer.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Diagnostics;
1+
using System.Diagnostics;
32
using ReClassNET.Util;
43

54
namespace ReClassNET.MemoryScanner.Comparer
@@ -27,11 +26,11 @@ public LongMemoryComparer(ScanCompareType compareType, long value1, long value2)
2726
Value2 = value2;
2827
}
2928

30-
public bool Compare(byte[] data, int index, out ScanResult result)
29+
public unsafe bool Compare(byte* data, out ScanResult result)
3130
{
3231
result = null;
3332

34-
var value = BitConverter.ToInt64(data, index);
33+
var value = *(long*)data;
3534

3635
bool IsMatch()
3736
{
@@ -70,20 +69,20 @@ bool IsMatch()
7069
return true;
7170
}
7271

73-
public bool Compare(byte[] data, int index, ScanResult previous, out ScanResult result)
72+
public unsafe bool Compare(byte* data, ScanResult previous, out ScanResult result)
7473
{
7574
#if DEBUG
7675
Debug.Assert(previous is LongScanResult);
7776
#endif
7877

79-
return Compare(data, index, (LongScanResult)previous, out result);
78+
return Compare(data, (LongScanResult)previous, out result);
8079
}
8180

82-
public bool Compare(byte[] data, int index, LongScanResult previous, out ScanResult result)
81+
public unsafe bool Compare(byte* data, LongScanResult previous, out ScanResult result)
8382
{
8483
result = null;
8584

86-
var value = BitConverter.ToInt64(data, index);
85+
var value = *(long*)data;
8786

8887
bool IsMatch()
8988
{

ReClass.NET/MemoryScanner/Comparer/ShortMemoryComparer.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Diagnostics;
1+
using System.Diagnostics;
32
using ReClassNET.Util;
43

54
namespace ReClassNET.MemoryScanner.Comparer
@@ -27,11 +26,11 @@ public ShortMemoryComparer(ScanCompareType compareType, short value1, short valu
2726
Value2 = value2;
2827
}
2928

30-
public bool Compare(byte[] data, int index, out ScanResult result)
29+
public unsafe bool Compare(byte* data, out ScanResult result)
3130
{
3231
result = null;
3332

34-
var value = BitConverter.ToInt16(data, index);
33+
var value = *(short*)data;
3534

3635
bool IsMatch()
3736
{
@@ -70,20 +69,20 @@ bool IsMatch()
7069
return true;
7170
}
7271

73-
public bool Compare(byte[] data, int index, ScanResult previous, out ScanResult result)
72+
public unsafe bool Compare(byte* data, ScanResult previous, out ScanResult result)
7473
{
7574
#if DEBUG
7675
Debug.Assert(previous is ShortScanResult);
7776
#endif
7877

79-
return Compare(data, index, (ShortScanResult)previous, out result);
78+
return Compare(data, (ShortScanResult)previous, out result);
8079
}
8180

82-
public bool Compare(byte[] data, int index, ShortScanResult previous, out ScanResult result)
81+
public unsafe bool Compare(byte* data, ShortScanResult previous, out ScanResult result)
8382
{
8483
result = null;
8584

86-
var value = BitConverter.ToInt16(data, index);
85+
var value = *(short*)data;
8786

8887
bool IsMatch()
8988
{

0 commit comments

Comments
 (0)