-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChunky.cs
211 lines (170 loc) · 6.27 KB
/
Chunky.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using static billions.Consts;
namespace billions
{
public static class Chunky
{
public static Stats Stats = new Stats();
public static int Threads = 20;
public static string FilePath;
public static long TotalLines;
public unsafe static void Start()
{
TotalLines = 0;
using var fs = new FileStream(FilePath,
new FileStreamOptions()
{
Access = FileAccess.Read,
Mode = FileMode.Open,
Options = FileOptions.SequentialScan,
Share = FileShare.ReadWrite,
BufferSize = 1,
});
fs.Position = 0;
long batchSize = fs.Length / Threads;
fs.Position = batchSize;
while (fs.ReadByte() != NewLine)
{
}
AddDebugInfo(0, fs.Position);
List<Thread> threadArray = new List<Thread>(Threads);
long end = fs.Position;
threadArray.Add(StartThread(0, end));
for (int t = 1; t < Threads - 1; t++)
{
long start = fs.Position;
fs.Position = start + batchSize;
int seek = fs.ReadByte();
while (seek != -1 && seek != NewLine)
{
// read till eof or newline
seek = fs.ReadByte();
}
AddDebugInfo(start, fs.Position);
threadArray.Add(StartThread(start, fs.Position));
}
AddDebugInfo(fs.Position, fs.Length);
threadArray.Add(StartThread(fs.Position, fs.Length));
for (int t = 0; t < Threads; t++)
{
threadArray[t].Join();
}
Console.WriteLine($"Lines: {TotalLines}");
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Thread StartThread(long start, long end)
{
var thread = new Thread(() =>
{
long lines = Chunk(start, end);
Interlocked.Add(ref TotalLines, lines);
DebugMsg("thread done {0} - {1} : {2}", start, end, lines);
})
{
Priority = ThreadPriority.AboveNormal,
//IsBackground = false,
};
thread.Start();
return thread;
}
public static int ChunkyFilling(long fileStart, long fileEnd)
{
const long BUFFER_SIZE = 200_000;
var stationList = Stats.NewThread();
using var fs = File.OpenHandle(
FilePath,
FileMode.Open,
FileAccess.Read,
FileShare.Read,
FileOptions.RandomAccess);
//todo
//var buffer = new byte[BUFFER_SIZE];
int lines = 0;
long pending = fileEnd - fileStart;
var lastBuffer = ArrayPool<byte>.Shared.Rent(Math.Min((int)BUFFER_SIZE, (int)(fileEnd - fileStart)));
FileSegment lastSegment = new FileSegment(lastBuffer);
var read = RandomAccess.Read(fs, lastBuffer, fileStart);
pending -= read;
var sequence = new ReadOnlySequence<byte>(lastBuffer);
var reader = new SequenceReader<byte>(sequence);
while (true)
{
while (reader.TryReadTo(out ReadOnlySpan<byte> line, NewLine, advancePastDelimiter: true))
{
lines++;
NewStation(in line, in stationList);
}
if (pending <= 0)
{
break;
}
// todo : optimise
// if (pending < smallstacksize)
byte[] buffer = ArrayPool<byte>.Shared.Rent(Math.Min((int)BUFFER_SIZE, (int)pending));
var sread = RandomAccess.Read(fs, buffer, fileEnd - pending);
FileSegment newSegment;
if (sread > pending)
{
sread = (int)pending;
newSegment = new FileSegment(((Memory<byte>)buffer).Slice(0, (int)pending), lastSegment);
}
else
{
newSegment = new FileSegment(buffer, lastSegment);
}
pending -= sread;
// todo : free mem?
sequence = new ReadOnlySequence<byte>(
lastSegment,
reader.CurrentSpanIndex,
newSegment,
sread);
reader = new SequenceReader<byte>(sequence);
ArrayPool<byte>.Shared.Return(lastBuffer);
lastBuffer = buffer;
lastSegment = newSegment;
}
//Debug.Assert(pending == 0);
//Debug.Assert(reader.Length == lines);
return lines;
}
public static int Chunk(long fileStart, long fileEnd)
{
try
{
return ChunkyFilling(fileStart, fileEnd);
}
catch (Exception ex)
{
throw new ApplicationException($"Error in Chunk: {fileStart} - {fileEnd}", ex);
}
}
[Conditional("DEBUG")]
private static void AddDebugInfo(long start, long end)
{
Console.WriteLine($"Chunk: {start}, End: {end}");
}
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void NewStation(in ReadOnlySpan<byte> line, in ThreadStats stats)
{
var seperatorIndex = line.IndexOf(Semicolon);
var nameSpan = line.Slice(0, seperatorIndex);
var valueSpan = line.Slice(seperatorIndex + 1);
stats.Add(nameSpan,valueSpan);
}
}
}
public sealed class FileSegment : ReadOnlySequenceSegment<byte>
{
public FileSegment(ReadOnlyMemory<byte> memory, FileSegment? previous = null)
{
if (previous != null)
{
previous.Next = this;
RunningIndex = previous.RunningIndex + previous.Memory.Length;
}
Memory = memory;
}
}