-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use worker thread for piece hashing (#198)
Worker threads actually share memory in Node.js so there's no copy getting the CAR bytes into the worker. This allows WASM piece hashing in a worker that doesn't stall the main thread. #### Before Notice how the spinner is stalled for multiple seconds as piece hashing is done: https://github.com/storacha-network/w3cli/assets/152863/5d88f8c5-e7ae-4da3-96ce-a686fb3f2258 #### After No stall! https://github.com/storacha-network/w3cli/assets/152863/747a412e-1c27-456b-913f-a4ec6e29498a Co-authored-by: Alan Shaw <[email protected]>
- Loading branch information
Showing
3 changed files
with
45 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { parentPort, workerData } from 'node:worker_threads' | ||
import * as PieceHasher from 'fr32-sha2-256-trunc254-padded-binary-tree-multihash' | ||
|
||
const hasher = PieceHasher.create() | ||
hasher.write(workerData) | ||
|
||
const bytes = new Uint8Array(hasher.multihashByteLength()) | ||
hasher.digestInto(bytes, 0, true) | ||
hasher.free() | ||
|
||
parentPort?.postMessage(bytes) |