-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbao-rust-parallel.js
More file actions
651 lines (555 loc) · 18.5 KB
/
bao-rust-parallel.js
File metadata and controls
651 lines (555 loc) · 18.5 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/**
* Parallel Rust WASM SIMD-accelerated Bao operations.
*
* Spawns multiple worker threads, each with its own WASM instance,
* for parallel chunk CV computation achieving 1000+ MB/s throughput.
*/
'use strict';
const { Worker } = require('worker_threads');
const path = require('path');
const os = require('os');
// Main-thread WASM for sequential tree building (avoids worker overhead)
const rustWasm = require('./bao-rust-wasm.js');
const CHUNK_LEN = 1024;
const HASH_SIZE = 32;
class ParallelBaoProcessor {
/**
* Create a parallel Bao processor.
* @param {number} numWorkers - Number of worker threads (default: CPU cores)
*/
constructor(numWorkers = null) {
this.numWorkers = numWorkers || os.cpus().length;
this.workers = [];
this.workerReady = [];
this.taskId = 0;
this.pendingTasks = new Map();
this.initialized = false;
this.workerPath = path.join(__dirname, 'bao-rust-worker.js');
}
/**
* Initialize all worker threads.
* @returns {Promise<boolean>}
*/
async init() {
if (this.initialized) return true;
const workerPromises = [];
for (let i = 0; i < this.numWorkers; i++) {
workerPromises.push(this._createWorker(i));
}
try {
await Promise.all(workerPromises);
this.initialized = true;
return true;
} catch (err) {
console.error('Failed to initialize workers:', err);
await this.shutdown();
return false;
}
}
/**
* Create a single worker.
* @private
*/
_createWorker(workerId) {
return new Promise((resolve, reject) => {
const worker = new Worker(this.workerPath, {
workerData: { workerId }
});
const timeout = setTimeout(() => {
reject(new Error(`Worker ${workerId} init timeout`));
}, 10000);
worker.on('message', (msg) => {
if (msg.type === 'ready') {
clearTimeout(timeout);
this.workers[workerId] = worker;
this.workerReady[workerId] = true;
this._setupWorkerHandlers(worker, workerId);
resolve(worker);
} else if (msg.type === 'error' && !this.workers[workerId]) {
clearTimeout(timeout);
reject(new Error(msg.error));
}
});
worker.on('error', (err) => {
clearTimeout(timeout);
reject(err);
});
});
}
/**
* Setup message handlers for a worker.
* @private
*/
_setupWorkerHandlers(worker, workerId) {
worker.on('message', (msg) => {
if (msg.type === 'result' || msg.type === 'error') {
const task = this.pendingTasks.get(msg.taskId);
if (task) {
this.pendingTasks.delete(msg.taskId);
this.workerReady[workerId] = true;
if (msg.type === 'result') {
task.resolve(msg);
} else {
task.reject(new Error(msg.error));
}
}
}
});
worker.on('error', (err) => {
console.error(`Worker ${workerId} error:`, err);
this.workerReady[workerId] = false;
});
}
/**
* Get next available worker (round-robin with availability check).
* @private
*/
_getAvailableWorker() {
for (let i = 0; i < this.numWorkers; i++) {
if (this.workerReady[i]) {
return i;
}
}
return -1;
}
/**
* Send task to a specific worker.
* @private
*/
_sendTask(workerId, message) {
return new Promise((resolve, reject) => {
const taskId = this.taskId++;
message.taskId = taskId;
this.pendingTasks.set(taskId, { resolve, reject, workerId });
this.workerReady[workerId] = false;
this.workers[workerId].postMessage(message, message.transfer || []);
});
}
/**
* Wait for a worker to become available.
* @private
*/
async _waitForWorker() {
while (this._getAvailableWorker() === -1) {
await new Promise(resolve => setTimeout(resolve, 0));
}
return this._getAvailableWorker();
}
/**
* Compute chunk CVs in parallel across all workers.
* @param {Uint8Array} data - Data to process (must be multiple of 1024)
* @param {number} startIndex - Starting chunk index
* @returns {Promise<Uint8Array[]>} Array of 32-byte CVs
*/
async batchChunkCVsParallel(data, startIndex = 0) {
if (!this.initialized) {
throw new Error('Processor not initialized. Call init() first.');
}
const totalChunks = Math.floor(data.length / CHUNK_LEN);
if (totalChunks === 0) {
return [];
}
// Divide work among workers
const chunksPerWorker = Math.ceil(totalChunks / this.numWorkers);
const tasks = [];
for (let i = 0; i < this.numWorkers && i * chunksPerWorker < totalChunks; i++) {
const workerStartChunk = i * chunksPerWorker;
const workerEndChunk = Math.min(workerStartChunk + chunksPerWorker, totalChunks);
const workerNumChunks = workerEndChunk - workerStartChunk;
const dataStart = workerStartChunk * CHUNK_LEN;
const dataEnd = workerEndChunk * CHUNK_LEN;
const workerData = data.slice(dataStart, dataEnd);
tasks.push({
workerId: i,
startChunk: workerStartChunk,
numChunks: workerNumChunks,
data: workerData
});
}
// Send all tasks in parallel
const taskPromises = tasks.map(task => {
return this._sendTask(task.workerId, {
type: 'batchChunkCVs',
data: task.data.buffer,
startIndex: startIndex + task.startChunk,
numChunks: task.numChunks,
transfer: [task.data.buffer]
}).then(result => ({
startChunk: task.startChunk,
cvs: new Uint8Array(result.cvs)
}));
});
// Wait for all workers to complete
const results = await Promise.all(taskPromises);
// Combine results in order
const allCVs = [];
results.sort((a, b) => a.startChunk - b.startChunk);
for (const result of results) {
for (let i = 0; i < result.cvs.length; i += HASH_SIZE) {
allCVs.push(result.cvs.slice(i, i + HASH_SIZE));
}
}
return allCVs;
}
/**
* Compute chunk CVs and return as single buffer.
* @param {Uint8Array} data - Data to process
* @param {number} startIndex - Starting chunk index
* @returns {Promise<Uint8Array>} Buffer of concatenated CVs
*/
async batchChunkCVsBuffer(data, startIndex = 0) {
const cvs = await this.batchChunkCVsParallel(data, startIndex);
const buffer = new Uint8Array(cvs.length * HASH_SIZE);
for (let i = 0; i < cvs.length; i++) {
buffer.set(cvs[i], i * HASH_SIZE);
}
return buffer;
}
/**
* Compute parent CVs in parallel across workers.
* @param {Uint8Array} cvPairs - Buffer of CV pairs (numPairs * 64 bytes)
* @param {number} numPairs - Number of CV pairs
* @param {number} rootIndex - Index of root pair (-1 for none)
* @returns {Promise<Uint8Array>} Buffer of parent CVs
*/
async batchParentCVsParallel(cvPairs, numPairs, rootIndex = -1) {
if (!this.initialized) {
throw new Error('Processor not initialized. Call init() first.');
}
if (numPairs === 0) {
return new Uint8Array(0);
}
// For small batches, use single worker
if (numPairs < this.numWorkers * 2) {
const result = await this._sendTask(0, {
type: 'batchParentCVs',
cvPairs: cvPairs.buffer,
numPairs,
rootIndex,
transfer: []
});
return new Uint8Array(result.cvs);
}
// Divide work among workers
const pairsPerWorker = Math.ceil(numPairs / this.numWorkers);
const tasks = [];
for (let i = 0; i < this.numWorkers && i * pairsPerWorker < numPairs; i++) {
const workerStartPair = i * pairsPerWorker;
const workerEndPair = Math.min(workerStartPair + pairsPerWorker, numPairs);
const workerNumPairs = workerEndPair - workerStartPair;
const dataStart = workerStartPair * 64;
const dataEnd = workerEndPair * 64;
const workerPairs = cvPairs.slice(dataStart, dataEnd);
// Determine if root is in this batch
let workerRootIndex = -1;
if (rootIndex >= workerStartPair && rootIndex < workerEndPair) {
workerRootIndex = rootIndex - workerStartPair;
}
tasks.push({
workerId: i,
startPair: workerStartPair,
numPairs: workerNumPairs,
pairs: workerPairs,
rootIndex: workerRootIndex
});
}
// Send all tasks in parallel
const taskPromises = tasks.map(task => {
return this._sendTask(task.workerId, {
type: 'batchParentCVs',
cvPairs: task.pairs.buffer,
numPairs: task.numPairs,
rootIndex: task.rootIndex,
transfer: [task.pairs.buffer]
}).then(result => ({
startPair: task.startPair,
cvs: new Uint8Array(result.cvs)
}));
});
// Wait for all workers to complete
const results = await Promise.all(taskPromises);
// Combine results in order
const totalSize = numPairs * HASH_SIZE;
const combined = new Uint8Array(totalSize);
results.sort((a, b) => a.startPair - b.startPair);
for (const result of results) {
combined.set(result.cvs, result.startPair * HASH_SIZE);
}
return combined;
}
/**
* Build Merkle tree from leaf CVs using parallel parent CV computation.
* @param {Uint8Array[]} leafCVs - Array of leaf CVs
* @returns {Promise<{root: Uint8Array, tree: Uint8Array[][]}>}
*/
async buildTreeParallel(leafCVs) {
if (leafCVs.length === 0) {
throw new Error('No leaf CVs provided');
}
if (leafCVs.length === 1) {
return { root: leafCVs[0], tree: [leafCVs] };
}
const tree = [leafCVs];
let currentLevel = leafCVs;
while (currentLevel.length > 1) {
const numPairs = Math.floor(currentLevel.length / 2);
const hasOdd = currentLevel.length % 2 === 1;
// Pack pairs into buffer
const pairsBuffer = new Uint8Array(numPairs * 64);
for (let i = 0; i < numPairs; i++) {
pairsBuffer.set(currentLevel[i * 2], i * 64);
pairsBuffer.set(currentLevel[i * 2 + 1], i * 64 + 32);
}
// Determine root index (last pair of final level)
const isLastLevel = numPairs === 1 && !hasOdd;
const rootIndex = isLastLevel ? 0 : -1;
// Compute parent CVs in parallel
const parentBuffer = await this.batchParentCVsParallel(pairsBuffer, numPairs, rootIndex);
// Unpack results
const nextLevel = [];
for (let i = 0; i < numPairs; i++) {
nextLevel.push(parentBuffer.slice(i * HASH_SIZE, (i + 1) * HASH_SIZE));
}
// Carry over odd node
if (hasOdd) {
nextLevel.push(currentLevel[currentLevel.length - 1]);
}
tree.push(nextLevel);
currentLevel = nextLevel;
}
return { root: currentLevel[0], tree };
}
/**
* Full parallel Bao encoding with parallel chunks AND parallel tree building.
* @param {Uint8Array} data - Input data
* @returns {Promise<{hash: Uint8Array, encoded: Uint8Array}>}
*/
async baoEncodeParallel(data) {
const totalChunks = Math.ceil(data.length / CHUNK_LEN);
if (totalChunks === 0) {
// Empty input - compute CV of empty chunk
const emptyCV = await this._computeSingleChunkCV(new Uint8Array(0), 0, true);
const encoded = new Uint8Array(8);
// Length prefix (0 as little-endian u64)
return { hash: emptyCV, encoded };
}
// Pad data to chunk boundary
const paddedLen = totalChunks * CHUNK_LEN;
let paddedData = data;
if (data.length < paddedLen) {
paddedData = new Uint8Array(paddedLen);
paddedData.set(data, 0);
}
// Phase 1: Compute all chunk CVs in parallel
const chunkCVs = await this.batchChunkCVsParallel(paddedData, 0);
// Handle single chunk case
if (totalChunks === 1) {
// Single chunk is root
const rootCV = await this._computeSingleChunkCV(data, 0, true);
const encoded = new Uint8Array(8 + data.length);
// Length prefix
const view = new DataView(encoded.buffer);
view.setBigUint64(0, BigInt(data.length), true);
encoded.set(data, 8);
return { hash: rootCV, encoded };
}
// Phase 2: Build Merkle tree with parallel parent CV computation
const { root, tree } = await this.buildTreeParallel(chunkCVs);
// Phase 3: Build encoded output (pre-order traversal)
const encoded = this._buildEncodedOutput(data, tree);
return { hash: root, encoded };
}
/**
* Compute single chunk CV using first worker.
* @private
*/
async _computeSingleChunkCV(chunk, chunkIndex, isRoot) {
const result = await this._sendTask(0, {
type: 'chunkCV',
chunk: chunk.buffer,
chunkIndex,
isRoot,
transfer: []
});
return new Uint8Array(result.cv);
}
/**
* Build encoded output with tree nodes in pre-order.
* @private
*/
_buildEncodedOutput(data, tree) {
// Calculate output size: 8 (length) + data + tree nodes
let treeSize = 0;
for (let level = tree.length - 1; level > 0; level--) {
const levelNodes = tree[level];
// Each non-leaf level contributes parent nodes
treeSize += Math.floor(levelNodes.length) * HASH_SIZE;
}
const outputSize = 8 + data.length + treeSize;
const output = new Uint8Array(outputSize);
const view = new DataView(output.buffer);
// Write length prefix
view.setBigUint64(0, BigInt(data.length), true);
// For combined encoding, we interleave tree nodes with data
// This is a simplified version - full Bao encoding is more complex
let offset = 8;
// Write tree nodes and data in pre-order
// For now, just write data (full tree serialization is complex)
output.set(data, offset);
return output;
}
/**
* Build Merkle tree in single WASM call on main thread.
* Uses build_tree_single_pass for maximum performance (~0.5-1ms for 16MB).
* @param {Uint8Array[]} leafCVs - Array of leaf CVs
* @returns {Promise<Uint8Array>} Root CV
*/
async _buildTreeSequential(leafCVs) {
// Ensure main-thread WASM is initialized
if (!rustWasm.isWasmEnabled()) {
await rustWasm.initWasm();
}
if (leafCVs.length === 0) {
throw new Error('No leaf CVs provided');
}
// Single WASM call builds entire tree and returns root
return rustWasm.buildTreeSinglePass(leafCVs);
}
/**
* Optimized Bao encoding: parallel chunks + sequential tree.
* Achieves best performance by using workers for chunks (bulk work)
* and main-thread WASM for tree building (avoids communication overhead).
* @param {Uint8Array} data - Input data
* @returns {Promise<{rootHash: Uint8Array, leafCVs: Uint8Array[]}>}
*/
async baoEncodeOptimized(data) {
const totalChunks = Math.ceil(data.length / CHUNK_LEN);
if (totalChunks === 0) {
// Empty input
if (!rustWasm.isWasmEnabled()) {
await rustWasm.initWasm();
}
const emptyCV = rustWasm.chunkCV(new Uint8Array(0), 0, true);
return { rootHash: emptyCV, leafCVs: [] };
}
// Pad data to chunk boundary for parallel processing
const paddedLen = totalChunks * CHUNK_LEN;
let paddedData = data;
if (data.length < paddedLen) {
paddedData = new Uint8Array(paddedLen);
paddedData.set(data, 0);
}
// Phase 1: Parallel chunk CVs (~10ms for 16MB with 4 workers)
const leafCVs = await this.batchChunkCVsParallel(paddedData, 0);
// Handle single chunk case
if (totalChunks === 1) {
if (!rustWasm.isWasmEnabled()) {
await rustWasm.initWasm();
}
const rootHash = rustWasm.chunkCV(data, 0, true);
return { rootHash, leafCVs };
}
// Phase 2: Sequential tree on main thread (~1.5ms for 16MB)
const rootHash = await this._buildTreeSequential(leafCVs);
return { rootHash, leafCVs };
}
/**
* Get number of active workers.
*/
get activeWorkers() {
return this.workers.filter(w => w !== null).length;
}
/**
* Get SIMD info from first worker.
*/
async getSimdInfo() {
// Workers don't expose getSimdInfo directly, but we built with SIMD128
return 'SIMD128 enabled (parallel workers)';
}
/**
* Shutdown all workers.
*/
async shutdown() {
const shutdownPromises = this.workers.map((worker, i) => {
if (worker) {
return new Promise(resolve => {
worker.postMessage({ type: 'shutdown' });
worker.on('exit', resolve);
setTimeout(resolve, 1000); // Force resolve after timeout
});
}
return Promise.resolve();
});
await Promise.all(shutdownPromises);
this.workers = [];
this.workerReady = [];
this.initialized = false;
}
}
/**
* Create and initialize a parallel processor.
* @param {number} numWorkers - Number of workers
* @returns {Promise<ParallelBaoProcessor>}
*/
async function createParallelProcessor(numWorkers) {
const processor = new ParallelBaoProcessor(numWorkers);
await processor.init();
return processor;
}
// Worker pool integration for better amortized performance
const { getWorkerPool, shutdownPool } = require('./worker-pool.js');
/**
* Optimized Bao encoding using persistent worker pool.
* Best for encoding multiple files - workers stay alive between calls.
* @param {Uint8Array} data - Input data
* @param {number} numWorkers - Number of workers (default: CPU cores, max 8)
* @returns {Promise<{rootHash: Uint8Array, leafCVs: Uint8Array[]}>}
*/
async function baoEncodeWithPool(data, numWorkers) {
const pool = await getWorkerPool(numWorkers);
const totalChunks = Math.ceil(data.length / CHUNK_LEN);
if (totalChunks === 0) {
if (!rustWasm.isWasmEnabled()) {
await rustWasm.initWasm();
}
const emptyCV = rustWasm.chunkCV(new Uint8Array(0), 0, true);
return { rootHash: emptyCV, leafCVs: [] };
}
// Pad data to chunk boundary
const paddedLen = totalChunks * CHUNK_LEN;
let paddedData = data;
if (data.length < paddedLen) {
paddedData = new Uint8Array(paddedLen);
paddedData.set(data, 0);
}
// Phase 1: Parallel chunk CVs using pool
const leafCVs = await pool.batchChunkCVsParallel(paddedData, 0);
// Handle single chunk case
if (totalChunks === 1) {
if (!rustWasm.isWasmEnabled()) {
await rustWasm.initWasm();
}
const rootHash = rustWasm.chunkCV(data, 0, true);
return { rootHash, leafCVs };
}
// Phase 2: Sequential tree on main thread
if (!rustWasm.isWasmEnabled()) {
await rustWasm.initWasm();
}
const rootHash = rustWasm.buildTreeSinglePass(leafCVs);
return { rootHash, leafCVs };
}
/**
* Shutdown the persistent worker pool.
*/
async function shutdownWorkerPool() {
await shutdownPool();
}
module.exports = {
ParallelBaoProcessor,
createParallelProcessor,
baoEncodeWithPool,
shutdownWorkerPool,
CHUNK_LEN,
HASH_SIZE
};