forked from gunnarmorling/1brc
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
292 lines (248 loc) · 6.7 KB
/
index.js
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import * as os from 'node:os';
import * as fsp from 'node:fs/promises';
import * as fs from 'node:fs';
import * as workerThreads from 'worker_threads';
const MAX_LINE_LENGTH = 100 + 1 + 4 + 1;
const CHAR_SEMICOLON = ';'.charCodeAt(0);
const CHAR_NEWLINE = '\n'.charCodeAt(0);
const TOKEN_STATION_NAME = 0;
const TOKEN_TEMPERATURE = 1;
/** @type {(...args: any[]) => void} */
const debug = process.env.DEBUG ? console.error : () => {};
/**
* @typedef {Map<string, {min: number, max: number, sum: number, count: number}>} CalcResultsCont
*/
if (workerThreads.isMainThread) {
const fileName = process.argv[2];
const file = await fsp.open(fileName);
const size = (await file.stat()).size;
const threadsCount = os.cpus().length;
const chunkSize = Math.floor(size / threadsCount);
/** @type {number[]} */
const chunkOffsets = [];
let offset = 0;
const bufFindNl = Buffer.alloc(MAX_LINE_LENGTH);
while (true) {
offset += chunkSize;
if (offset >= size) {
chunkOffsets.push(size);
break;
}
await file.read(bufFindNl, 0, MAX_LINE_LENGTH, offset);
const nlPos = bufFindNl.indexOf(10);
bufFindNl.fill(0);
if (nlPos === -1) {
chunkOffsets.push(size);
break;
} else {
offset += nlPos + 1;
chunkOffsets.push(offset);
}
}
await file.close();
/**
* @type {CalcResultsCont}
*/
const compiledResults = new Map();
let stoppedWorkers = 0;
for (let i = 0; i < chunkOffsets.length; i++) {
const worker = new workerThreads.Worker('./src/main/nodejs/index.js', {
workerData: {
fileName,
start: i === 0 ? 0 : chunkOffsets[i - 1],
end: chunkOffsets[i],
},
});
worker.on(
'message',
(
/** @type {CalcResultsCont} */
message
) => {
for (let [key, value] of message.entries()) {
const existing = compiledResults.get(key);
if (existing) {
existing.min = Math.min(existing.min, value.min);
existing.max = Math.max(existing.max, value.max);
existing.sum += value.sum;
existing.count += value.count;
} else {
compiledResults.set(key, value);
}
}
}
);
worker.on('error', (err) => {
console.error(err);
});
worker.on('exit', (code) => {
if (code !== 0) {
new Error(`Worker stopped with exit code ${code}`);
} else {
debug('Worker stopped');
}
stoppedWorkers++;
if (stoppedWorkers === chunkOffsets.length) {
printCompiledResults(compiledResults);
}
});
}
} else {
const { fileName, start, end } = workerThreads.workerData;
if (start > end - 1) {
workerThreads.parentPort.postMessage(new Map());
} else {
const readStream = fs.createReadStream(fileName, {
start: start,
end: end - 1,
});
parseStream(readStream);
}
}
/**
* @param {CalcResultsCont} compiledResults
*/
function printCompiledResults(compiledResults) {
const sortedStations = Array.from(compiledResults.keys()).sort();
process.stdout.write('{');
for (let i = 0; i < sortedStations.length; i++) {
if (i > 0) {
process.stdout.write(', ');
}
const data = compiledResults.get(sortedStations[i]);
process.stdout.write(sortedStations[i]);
process.stdout.write('=');
process.stdout.write(
round(data.min / 10) +
'/' +
round(data.sum / 10 / data.count) +
'/' +
round(data.max / 10)
);
}
process.stdout.write('}\n');
}
/**
* @example
* round(1.2345) // "1.2"
* round(1.55) // "1.6"
* round(1) // "1.0"
*
* @param {number} num
* @returns {string}
*/
function round(num) {
const fixed = Math.round(10 * num) / 10;
return fixed.toFixed(1);
}
/**
* @param {import('node:fs').ReadStream} readStream
*/
function parseStream(readStream) {
let readingToken = TOKEN_STATION_NAME;
let stationName = Buffer.allocUnsafe(100);
let stationNameLen = 0;
let temperature = Buffer.allocUnsafe(5);
let temperatureLen = 0;
/**
* @type {CalcResultsCont}
*/
const map = new Map();
/**
* @param {Buffer} chunk
* @returns {void}
*/
function parseChunk(chunk) {
for (let i = 0; i < chunk.length; i++) {
if (chunk[i] === CHAR_SEMICOLON) {
readingToken = TOKEN_TEMPERATURE;
} else if (chunk[i] === CHAR_NEWLINE) {
const stationNameStr = stationName.toString('utf8', 0, stationNameLen);
let temperatureFloat = 0 | 0;
try {
temperatureFloat = parseFloatBufferIntoInt(
temperature,
temperatureLen
);
} catch (err) {
console.log({ temperature, temperatureLen }, err.message);
throw err;
}
const existing = map.get(stationNameStr);
if (existing) {
existing.min =
existing.min < temperatureFloat ? existing.min : temperatureFloat;
existing.max =
existing.max > temperatureFloat ? existing.max : temperatureFloat;
existing.sum += temperatureFloat;
existing.count++;
} else {
map.set(stationNameStr, {
min: temperatureFloat,
max: temperatureFloat,
sum: temperatureFloat,
count: 1,
});
}
readingToken = TOKEN_STATION_NAME;
stationNameLen = 0;
temperatureLen = 0;
} else if (readingToken === TOKEN_STATION_NAME) {
stationName[stationNameLen] = chunk[i];
stationNameLen++;
} else {
temperature[temperatureLen] = chunk[i];
temperatureLen++;
}
}
}
readStream.on('data', (/** @type {Buffer} */ chunk) => {
parseChunk(chunk);
});
readStream.on('end', () => {
debug('Sending result to the main thread');
workerThreads.parentPort.postMessage(map);
});
}
const CHAR_MINUS = '-'.charCodeAt(0);
/**
* @param {Buffer} b
* @param {number} length 1-5
*
* @returns {number}
*/
function parseFloatBufferIntoInt(b, length) {
if (b[0] === CHAR_MINUS) {
// b can be -1.1 or -11.1
switch (length) {
case 4:
return -(parseOneDigit(b[1]) * 10 + parseOneDigit(b[3]));
case 5:
return -(
parseOneDigit(b[1]) * 100 +
parseOneDigit(b[2]) * 10 +
parseOneDigit(b[4])
);
}
} else {
// b can be 1.1 or 11.1
switch (length) {
case 3: // b is 1.1
return parseOneDigit(b[0]) * 10 + parseOneDigit(b[2]);
case 4:
return (
parseOneDigit(b[0]) * 100 +
parseOneDigit(b[1]) * 10 +
parseOneDigit(b[3])
);
}
}
}
/**
* @param {number} char byte number of a digit char
*
* @returns {number}
*/
function parseOneDigit(char) {
return char - 0x30;
}