Skip to content

Commit db39217

Browse files
committed
Refactoring and small fixes.
1 parent a3b1fae commit db39217

9 files changed

Lines changed: 378 additions & 253 deletions

Arrays/ArrayPool.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ namespace Platform.Collections.Arrays
44
{
55
public static class ArrayPool
66
{
7+
public static readonly int DefaultSizesAmount = 512;
8+
public static readonly int DefaultMaxArraysPerSize = 32;
9+
710
[MethodImpl(MethodImplOptions.AggressiveInlining)]
811
public static T[] Allocate<T>(long size) => ArrayPool<T>.GetOrCreateThreadInstance().Allocate(size);
912

Arrays/ArrayPool[T].cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,19 @@ namespace Platform.Collections.Arrays
1212
/// </remarks>
1313
public class ArrayPool<T>
1414
{
15-
public static readonly int DefaultSizesAmount = 512;
16-
public static readonly int DefaultMaxArraysPerSize = 32;
1715
public static readonly T[] Empty = new T[0];
1816

1917
// May be use Default class for that later.
2018
[ThreadStatic]
2119
internal static ArrayPool<T> ThreadInstance;
2220

2321
private readonly int _maxArraysPerSize;
24-
private readonly Dictionary<int, Stack<T[]>> _pool = new Dictionary<int, Stack<T[]>>(DefaultSizesAmount);
22+
private readonly Dictionary<int, Stack<T[]>> _pool = new Dictionary<int, Stack<T[]>>(ArrayPool.DefaultSizesAmount);
2523

2624
public ArrayPool(int maxArraysPerSize) => _maxArraysPerSize = maxArraysPerSize;
2725

2826
public ArrayPool()
29-
: this(DefaultMaxArraysPerSize)
27+
: this(ArrayPool.DefaultMaxArraysPerSize)
3028
{
3129
}
3230

Arrays/CharArrayExtensions.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ public static int GenerateHashCode(this char[] array, int offset, int length)
99
{
1010
var hashSeed = 5381;
1111
var hashAccumulator = hashSeed;
12-
fixed (char* src = &array[offset])
12+
fixed (char* pointer = &array[offset])
1313
{
14-
for (char* s = src, last = s + length; s < last; s++)
14+
for (char* s = pointer, last = s + length; s < last; s++)
1515
{
1616
hashAccumulator = (hashAccumulator << 5) + hashAccumulator ^ *s;
1717
}
@@ -24,54 +24,54 @@ public static int GenerateHashCode(this char[] array, int offset, int length)
2424
/// </remarks>
2525
public static bool ContentEqualTo(this char[] left, int leftOffset, int length, char[] right, int rightOffset)
2626
{
27-
fixed (char* ap = &left[leftOffset])
27+
fixed (char* leftPointer = &left[leftOffset])
2828
{
29-
fixed (char* bp = &right[rightOffset])
29+
fixed (char* rightPointer = &right[rightOffset])
3030
{
31-
char* a = ap, b = bp;
32-
if (!CheckArraysMainPartForEquality(ref a, ref b, ref length))
31+
char* leftPointerCopy = leftPointer, rightPointerCopy = rightPointer;
32+
if (!CheckArraysMainPartForEquality(ref leftPointerCopy, ref rightPointerCopy, ref length))
3333
{
3434
return false;
3535
}
36-
CheckArraysRemainderForEquality(ref a, ref b, ref length);
36+
CheckArraysRemainderForEquality(ref leftPointerCopy, ref rightPointerCopy, ref length);
3737
return length <= 0;
3838
}
3939
}
4040
}
4141

42-
private static bool CheckArraysMainPartForEquality(ref char* a, ref char* b, ref int length)
42+
private static bool CheckArraysMainPartForEquality(ref char* left, ref char* right, ref int length)
4343
{
4444
while (length >= 10)
4545
{
46-
if ((*(int*)a != *(int*)b)
47-
|| (*(int*)(a + 2) != *(int*)(b + 2))
48-
|| (*(int*)(a + 4) != *(int*)(b + 4))
49-
|| (*(int*)(a + 6) != *(int*)(b + 6))
50-
|| (*(int*)(a + 8) != *(int*)(b + 8)))
46+
if ((*(int*)left != *(int*)right)
47+
|| (*(int*)(left + 2) != *(int*)(right + 2))
48+
|| (*(int*)(left + 4) != *(int*)(right + 4))
49+
|| (*(int*)(left + 6) != *(int*)(right + 6))
50+
|| (*(int*)(left + 8) != *(int*)(right + 8)))
5151
{
5252
return false;
5353
}
54-
a += 10;
55-
b += 10;
54+
left += 10;
55+
right += 10;
5656
length -= 10;
5757
}
5858
return true;
5959
}
6060

61-
private static void CheckArraysRemainderForEquality(ref char* a, ref char* b, ref int length)
61+
private static void CheckArraysRemainderForEquality(ref char* left, ref char* right, ref int length)
6262
{
6363
// This depends on the fact that the String objects are
6464
// always zero terminated and that the terminating zero is not included
6565
// in the length. For odd string sizes, the last compare will include
6666
// the zero terminator.
6767
while (length > 0)
6868
{
69-
if (*(int*)a != *(int*)b)
69+
if (*(int*)left != *(int*)right)
7070
{
7171
break;
7272
}
73-
a += 2;
74-
b += 2;
73+
left += 2;
74+
right += 2;
7575
length -= 2;
7676
}
7777
}

0 commit comments

Comments
 (0)