-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPointerChunk.cs
145 lines (122 loc) · 3.67 KB
/
PointerChunk.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
class BestPractices
{
public static void Main()
{
var a = BenchmarkRunner.Run<ChunkBenchmark>();
}
}
public static class ArrayExtensions
{
public static IEnumerable<int[]> Chunk(this int[] array, int size)
{
while (array.Any())
{
yield return array.Take(size).ToArray();
array = array.Skip(size).ToArray();
}
}
}
[MemoryDiagnoser]
public class ChunkBenchmark
{
public int[] array;
public Random random;
[GlobalSetup]
public void InitializeArray()
{
array = Enumerable.Range(1, 10_000_000).Where(x => x % 2 == 0).ToArray();
random = new Random();
}
[Benchmark]
public void PointerChunkTest()
{
var pointerChunk = new PointerChunk(array, 1_000);
for (int i = 0; i < 3; i++)
{
var a = pointerChunk[random.Next(1,100)];
}
}
[Benchmark]
public void LinqChunkTest()
{
var chunk = array.Chunk(1_000);
for (int i = 0; i < 3; i++)
{
var a = chunk.ElementAt(random.Next(1,100));
}
}
}
[SkipLocalsInit]
// int64, int32, int16, byte, IntPtr
public sealed class PointerChunk
{
private IntPtr[] _chunkList;
private int[] _partialList;
private int _chunkSize;
private int _lastLoopSize;
private int _chunkListSize { get; }
private int _sizeofInt = sizeof(int);
public PointerChunk(int[] largeArray,int chunkSize)
{
_chunkSize = chunkSize;
_chunkList = Chunk(largeArray, chunkSize); // Chunk method handles exceptions.
_chunkListSize = _chunkList.Length; // so that _chunkList won't be null.
}
public int Length => _chunkListSize;
public int[] this[int i]
{
get
{
if (i == _chunkListSize - 1)
_chunkSize = _lastLoopSize;
if (i > _chunkListSize - 1)
throw new IndexOutOfRangeException();
else
{
_partialList = new int[_chunkSize];
var tempPtr = _chunkList[i];
for (int j = 0; j < _chunkSize; j++)
{
var value = Marshal.ReadInt32(tempPtr, _sizeofInt * j);
_partialList[j] = value;
}
return _partialList;
}
}
}
public IntPtr[] Chunk(int[] largeArray,int chunkSize)
{
if (largeArray is null)
throw new NullReferenceException("Array can't be null");
if (largeArray.Length == 0)
throw new ArgumentException("You can't chunk an empty array");
IntPtr[] list; // thread safe IntPtr list.
int[] tempArr = largeArray;
int arrLength = tempArr.Length;
int remainder = arrLength % chunkSize;
int noReminderSize = arrLength / chunkSize;
int remainderSize = arrLength / chunkSize + 1;
int loopSize = remainder == 0 ? noReminderSize : remainderSize;
_lastLoopSize = remainder == 0 ? chunkSize : remainder;
list = new IntPtr[loopSize];
unsafe
{
fixed (int* ptr = tempArr)
{
IntPtr iPtr = new IntPtr(ptr);
for (int i = 0; i < loopSize; i++)
{
IntPtr tempPtr = iPtr + i * _sizeofInt * chunkSize;
list[i] = tempPtr;
}
}
}
return list;
}
}