defuss-multicore 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Isomorphic multicore execution + loop-unrolled linear algebra for JavaScript/TypeScript.
4
4
 
5
- - **Web Workers** in the browser, **worker_threads** in Node.js — same API
5
+ - **Web Workers** in the browser, **worker_threads** in Node.js - same API
6
6
  - Auto-splits arrays/TypedArrays across CPU cores
7
7
  - JIT loop-unrolled vector/matrix ops (4x/8x/16x unroll factors, auto-selected)
8
8
  - Parallel `map`, `filter`, `reduce` for any array
@@ -47,7 +47,7 @@ for await (const r of parallel(data)) { ... } // stream as workers finish
47
47
 
48
48
  - Array/TypedArray args are **auto-split** across cores
49
49
  - Scalar args are **broadcast** to every worker
50
- - Returns `ParallelResult<R>` — both `PromiseLike<R[]>` and `AsyncIterable<R>`
50
+ - Returns `ParallelResult<R>` - both `PromiseLike<R[]>` and `AsyncIterable<R>`
51
51
 
52
52
  #### `MulticoreOptions<R>`
53
53
 
@@ -55,15 +55,15 @@ for await (const r of parallel(data)) { ... } // stream as workers finish
55
55
  |-------------|---------------------------|------------------------|-------------|
56
56
  | `cores` | `number` | `navigator.hardwareConcurrency` | Number of worker threads |
57
57
  | `threshold` | `number` | `1024` | Min array length before parallelizing (falls back to main thread below this) |
58
- | `reduce` | `(a: R, b: R) => R` | — | Reduce partial results into a single value |
58
+ | `reduce` | `(a: R, b: R) => R` | - | Reduce partial results into a single value |
59
59
  | `eager` | `boolean` | `false` | Pre-spawn workers immediately instead of on first call |
60
60
 
61
61
  #### `CallOptions` (per-call overrides)
62
62
 
63
63
  | Option | Type | Default | Description |
64
64
  |------------|----------------|----------|-------------|
65
- | `cores` | `number` | — | Override core count for this call |
66
- | `signal` | `AbortSignal` | — | Cancel in-flight workers |
65
+ | `cores` | `number` | - | Override core count for this call |
66
+ | `signal` | `AbortSignal` | - | Cancel in-flight workers |
67
67
  | `transfer` | `boolean` | `true` | Auto-detect and transfer ArrayBuffer ownership (zero-copy) |
68
68
 
69
69
  ### `map(array, fn, options?)`
@@ -98,7 +98,7 @@ const sum = await reduce(hugeArray, (a, b) => a + b, 0);
98
98
 
99
99
  ### `dotProduct(as, bs)`
100
100
 
101
- Batch dot product of vector pairs. JIT loop-unrolled (up to 16× unroll).
101
+ Batch dot product of vector pairs. JIT loop-unrolled (up to 16x unroll).
102
102
 
103
103
  ```ts
104
104
  import { dotProduct } from "defuss-multicore";
@@ -117,7 +117,7 @@ Matrix multiplication with transpose optimization + loop unrolling.
117
117
  ```ts
118
118
  import { matmul } from "defuss-multicore";
119
119
 
120
- // A: M×K, B: K×N → C: M×N
120
+ // A: MxK, B: KxN => C: MxN
121
121
  const C: Float64Array[] = matmul(A, B);
122
122
  ```
123
123
 
@@ -133,7 +133,7 @@ const diff = matsub(A, B);
133
133
  const quot = matdiv(A, B);
134
134
  ```
135
135
 
136
- All matrix ops use `Matrix<Float64Array>` — an array of row vectors (`Float64Array[]`).
136
+ All matrix ops use `Matrix<Float64Array>` - an array of row vectors (`Float64Array[]`).
137
137
 
138
138
  ### `getPoolSize()`
139
139
 
@@ -161,32 +161,32 @@ All benchmarks measured on Node.js (worker_threads) on Apple Silicon (10 cores).
161
161
 
162
162
  ### Loop-Unrolled Ops vs Naive Baseline
163
163
 
164
- Single-threaded comparison — same thread, unrolled kernels vs naive loops:
164
+ Single-threaded comparison - same thread, unrolled kernels vs naive loops:
165
165
 
166
166
  | Operation | Size | Speedup |
167
167
  |-----------|------|---------|
168
- | **matmul** | 200 × 300 × 200 | **2.36×** |
169
- | **matadd** | 1000 × 1000 | **2.11×** |
170
- | **matmul** | 500 × 500 | **2.06×** |
171
- | **dotProduct** | 100K × 768-dim | **1.46×** |
172
- | **matsub** | 1000 × 1000 | 0.49× |
173
- | **matdiv** | 1000 × 1000 | 0.37× |
168
+ | **matmul** | 200 x 300 x 200 | **2.36x** |
169
+ | **matadd** | 1000 x 1000 | **2.11x** |
170
+ | **matmul** | 500 x 500 | **2.06x** |
171
+ | **dotProduct** | 100K x 768-dim | **1.46x** |
172
+ | **matsub** | 1000 x 1000 | 0.49x |
173
+ | **matdiv** | 1000 x 1000 | 0.37x |
174
174
 
175
- Loop unrolling shines on **compute-heavy inner loops** like matrix multiplication, where the unrolled kernel avoids branch overhead and allows the CPU to pipeline instructions. Element-wise ops (add/sub/div) benefit less because the operation per element is trivial — the memory access pattern dominates.
175
+ Loop unrolling shines on **compute-heavy inner loops** like matrix multiplication, where the unrolled kernel avoids branch overhead and allows the CPU to pipeline instructions. Element-wise ops (add/sub/div) benefit less because the operation per element is trivial - the memory access pattern dominates.
176
176
 
177
177
  ### Multicore Workers vs Single-Thread
178
178
 
179
- Worker parallelism — dispatching across all CPU cores vs running on the main thread:
179
+ Worker parallelism - dispatching across all CPU cores vs running on the main thread:
180
180
 
181
181
  | Workload | Size | Speedup |
182
182
  |----------|------|---------|
183
- | **Key stretching** (PBKDF2-like) | 100K × 1000 rounds | **4.89×** |
184
- | **CRC32** (network packets) | 10K × 4KB | **1.31×** |
185
- | **CRC32** (small messages) | 500K × 64B | 0.80× |
186
- | **Transform** (sin+cos) | 5M items | 0.60× |
187
- | **FNV-1a** hash | 1M × 128B | 0.44× |
188
- | **Filter** elements | 2M items | 0.39× |
189
- | **Sum** (accumulate) | 10M items | 0.06× |
183
+ | **Key stretching** (PBKDF2-like) | 100K x 1000 rounds | **4.89x** |
184
+ | **CRC32** (network packets) | 10K x 4KB | **1.31x** |
185
+ | **CRC32** (small messages) | 500K x 64B | 0.80x |
186
+ | **Transform** (sin+cos) | 5M items | 0.60x |
187
+ | **FNV-1a** hash | 1M x 128B | 0.44x |
188
+ | **Filter** elements | 2M items | 0.39x |
189
+ | **Sum** (accumulate) | 10M items | 0.06x |
190
190
 
191
191
  ## When to Use `multicore()`
192
192
 
@@ -194,30 +194,30 @@ The benchmarks tell a clear story: **worker parallelism pays off when each chunk
194
194
 
195
195
  ### DO: Parallelize These
196
196
 
197
- - **Key derivation / password hashing** — thousands of rounds per item (4.9× speedup)
198
- - **Batch checksumming** (CRC32, SHA, etc.) of large messages — enough work per chunk to amortize dispatch
199
- - **Heavy per-element computation** — image processing, physics simulation, compression
197
+ - **Key derivation / password hashing** - thousands of rounds per item (4.9x speedup)
198
+ - **Batch checksumming** (CRC32, SHA, etc.) of large messages - enough work per chunk to amortize dispatch
199
+ - **Heavy per-element computation** - image processing, physics simulation, compression
200
200
  - **Any workload where each chunk runs >5ms** on a single core
201
201
 
202
202
  ### DON'T: Parallelize These
203
203
 
204
- - **Simple reductions** (sum, min, max) — main-thread loop is faster than worker dispatch
205
- - **Trivial transforms** (multiply, add constant) — memory-bandwidth bound, not CPU-bound
206
- - **Small arrays** (<1024 elements) — the `threshold` option exists for this reason
207
- - **Single function calls** — multicore is for **batches**, not individual invocations
208
- - **I/O-bound work** — fetch, file reads, DB queries are already async; workers add overhead
204
+ - **Simple reductions** (sum, min, max) - main-thread loop is faster than worker dispatch
205
+ - **Trivial transforms** (multiply, add constant) - memory-bandwidth bound, not CPU-bound
206
+ - **Small arrays** (<1024 elements) - the `threshold` option exists for this reason
207
+ - **Single function calls** - multicore is for **batches**, not individual invocations
208
+ - **I/O-bound work** - fetch, file reads, DB queries are already async; workers add overhead
209
209
 
210
210
  ### DO: Use Loop-Unrolled Ops
211
211
 
212
- - **`matmul`** for matrix multiplication, neural network layers (2×+ speedup)
213
- - **`matadd`** for accumulating matrices (2.1×)
214
- - **`dotProduct`** for embedding similarity, cosine distance, attention scores (1.5×+ speedup)
212
+ - **`matmul`** for matrix multiplication, neural network layers (2x+ speedup)
213
+ - **`matadd`** for accumulating matrices (2.1x)
214
+ - **`dotProduct`** for embedding similarity, cosine distance, attention scores (1.5x+ speedup)
215
215
 
216
216
  ### Key Principles
217
217
 
218
- 1. **Measure first.** The `threshold` option exists so small inputs fall back to the main thread automatically. But "small" depends on your workload — a 10K-element array of simple additions is too small; a 10K-element array of 1000-round hash stretches is perfect.
218
+ 1. **Measure first.** The `threshold` option exists so small inputs fall back to the main thread automatically. But "small" depends on your workload - a 10K-element array of simple additions is too small; a 10K-element array of 1000-round hash stretches is perfect.
219
219
 
220
- 2. **Worker functions must be pure.** They run in isolated contexts — no closures, no imports, no DOM access. Everything the function needs must be passed as arguments or computed inline.
220
+ 2. **Worker functions must be pure.** They run in isolated contexts - no closures, no imports, no DOM access. Everything the function needs must be passed as arguments or computed inline.
221
221
 
222
222
  3. **Transferables are auto-detected.** ArrayBuffers are transferred (zero-copy) by default. Disable with `{ transfer: false }` if you need to reuse the buffer after the call.
223
223
 
package/dist/index.cjs CHANGED
@@ -82,7 +82,7 @@ if (typeof require !== 'undefined') {
82
82
  parentPort.on('message', __handleMessage);
83
83
  }
84
84
  } catch (_) {
85
- // Not in worker_threads context \u2014 ignore
85
+ // Not in worker_threads context - ignore
86
86
  }
87
87
  }
88
88
  `.trim();
package/dist/index.d.cts CHANGED
@@ -4,7 +4,7 @@ type TypedArray = Float32Array | Float64Array | Int8Array | Int16Array | Int32Ar
4
4
  type NumericArray = number[] | TypedArray;
5
5
  /** A matrix is an array of row vectors (each row is a NumericArray) */
6
6
  type Matrix<T extends NumericArray = Float32Array> = T[];
7
- /** Batch of vectors (same shape as Matrix — alias for clarity) */
7
+ /** Batch of vectors (same shape as Matrix - alias for clarity) */
8
8
  type Vectors<T extends NumericArray = Float32Array> = T[];
9
9
  interface PoolConfig {
10
10
  /** Maximum number of workers in the pool (default: core count) */
@@ -39,8 +39,8 @@ interface CallOptions {
39
39
  transfer?: boolean;
40
40
  }
41
41
  /**
42
- * Dual interface: use as `PromiseLike` (await → R[]) or as `AsyncIterable`
43
- * (for-await-of → yields each R as workers complete).
42
+ * Dual interface: use as `PromiseLike` (await => R[]) or as `AsyncIterable`
43
+ * (for-await-of => yields each R as workers complete).
44
44
  */
45
45
  interface ParallelResult<R> extends AsyncIterable<R>, PromiseLike<R[]> {
46
46
  }
@@ -49,7 +49,7 @@ interface ParallelResult<R> extends AsyncIterable<R>, PromiseLike<R[]> {
49
49
  */
50
50
  interface ReducedParallelResult<R> extends AsyncIterable<R>, PromiseLike<R> {
51
51
  }
52
- /** The callable returned by `multicore(fn)` — same arg types, parallel return */
52
+ /** The callable returned by `multicore(fn)` - same arg types, parallel return */
53
53
  type ParallelFn<T extends (...args: unknown[]) => unknown> = (...args: [...Parameters<T>, CallOptions?]) => ParallelResult<ReturnType<T>>;
54
54
  type ReducedParallelFn<T extends (...args: unknown[]) => unknown> = (...args: [...Parameters<T>, CallOptions?]) => ReducedParallelResult<ReturnType<T>>;
55
55
  interface OpOptions extends CallOptions {
@@ -82,7 +82,7 @@ declare const getCoreCount: () => number;
82
82
  declare const multicore: <T extends (...args: any[]) => any>(fn: T, options?: MulticoreOptions<ReturnType<T>>) => ((...args: [...Parameters<T>, CallOptions?]) => ParallelResult<ReturnType<T>>);
83
83
 
84
84
  /**
85
- * Parallel `Array.prototype.map` — distributes work across CPU cores.
85
+ * Parallel `Array.prototype.map` - distributes work across CPU cores.
86
86
  *
87
87
  * @example
88
88
  * ```ts
@@ -91,7 +91,7 @@ declare const multicore: <T extends (...args: any[]) => any>(fn: T, options?: Mu
91
91
  */
92
92
  declare const map: <T, U>(array: T[], fn: (item: T) => U, options?: CallOptions) => ParallelResult<U[]>;
93
93
  /**
94
- * Parallel `Array.prototype.filter` — distributes predicate across CPU cores.
94
+ * Parallel `Array.prototype.filter` - distributes predicate across CPU cores.
95
95
  *
96
96
  * @example
97
97
  * ```ts
@@ -100,7 +100,7 @@ declare const map: <T, U>(array: T[], fn: (item: T) => U, options?: CallOptions)
100
100
  */
101
101
  declare const filter: <T>(array: T[], fn: (item: T) => boolean, options?: CallOptions) => ParallelResult<T[]>;
102
102
  /**
103
- * Parallel `Array.prototype.reduce` — each worker reduces its chunk,
103
+ * Parallel `Array.prototype.reduce` - each worker reduces its chunk,
104
104
  * then main thread reduces the partial results.
105
105
  *
106
106
  * @example
@@ -125,12 +125,12 @@ declare const reduce: <T>(array: T[], fn: (a: T, b: T) => T, initial: T, options
125
125
  declare const dotProduct: <T extends NumericArray>(vectorsA: Vectors<T>, vectorsB: Vectors<T>, options?: OpOptions) => Float32Array;
126
126
 
127
127
  /**
128
- * Matrix multiplication. C = A × B
128
+ * Matrix multiplication. C = A x B
129
129
  *
130
130
  * Row-parallel: each "worker" computes a slice of C's rows.
131
131
  * Inner dot product uses the unrolled kernel (4/8/16) for the k-dimension.
132
132
  *
133
- * Complexity: O(M × N × K) with M = A.rows, K = A.cols = B.rows, N = B.cols.
133
+ * Complexity: O(M x N x K) with M = A.rows, K = A.cols = B.rows, N = B.cols.
134
134
  */
135
135
  declare const matmul: <T extends NumericArray>(A: Matrix<T>, B: Matrix<T>, options?: OpOptions) => Matrix<T>;
136
136
 
package/dist/index.d.mts CHANGED
@@ -4,7 +4,7 @@ type TypedArray = Float32Array | Float64Array | Int8Array | Int16Array | Int32Ar
4
4
  type NumericArray = number[] | TypedArray;
5
5
  /** A matrix is an array of row vectors (each row is a NumericArray) */
6
6
  type Matrix<T extends NumericArray = Float32Array> = T[];
7
- /** Batch of vectors (same shape as Matrix — alias for clarity) */
7
+ /** Batch of vectors (same shape as Matrix - alias for clarity) */
8
8
  type Vectors<T extends NumericArray = Float32Array> = T[];
9
9
  interface PoolConfig {
10
10
  /** Maximum number of workers in the pool (default: core count) */
@@ -39,8 +39,8 @@ interface CallOptions {
39
39
  transfer?: boolean;
40
40
  }
41
41
  /**
42
- * Dual interface: use as `PromiseLike` (await → R[]) or as `AsyncIterable`
43
- * (for-await-of → yields each R as workers complete).
42
+ * Dual interface: use as `PromiseLike` (await => R[]) or as `AsyncIterable`
43
+ * (for-await-of => yields each R as workers complete).
44
44
  */
45
45
  interface ParallelResult<R> extends AsyncIterable<R>, PromiseLike<R[]> {
46
46
  }
@@ -49,7 +49,7 @@ interface ParallelResult<R> extends AsyncIterable<R>, PromiseLike<R[]> {
49
49
  */
50
50
  interface ReducedParallelResult<R> extends AsyncIterable<R>, PromiseLike<R> {
51
51
  }
52
- /** The callable returned by `multicore(fn)` — same arg types, parallel return */
52
+ /** The callable returned by `multicore(fn)` - same arg types, parallel return */
53
53
  type ParallelFn<T extends (...args: unknown[]) => unknown> = (...args: [...Parameters<T>, CallOptions?]) => ParallelResult<ReturnType<T>>;
54
54
  type ReducedParallelFn<T extends (...args: unknown[]) => unknown> = (...args: [...Parameters<T>, CallOptions?]) => ReducedParallelResult<ReturnType<T>>;
55
55
  interface OpOptions extends CallOptions {
@@ -82,7 +82,7 @@ declare const getCoreCount: () => number;
82
82
  declare const multicore: <T extends (...args: any[]) => any>(fn: T, options?: MulticoreOptions<ReturnType<T>>) => ((...args: [...Parameters<T>, CallOptions?]) => ParallelResult<ReturnType<T>>);
83
83
 
84
84
  /**
85
- * Parallel `Array.prototype.map` — distributes work across CPU cores.
85
+ * Parallel `Array.prototype.map` - distributes work across CPU cores.
86
86
  *
87
87
  * @example
88
88
  * ```ts
@@ -91,7 +91,7 @@ declare const multicore: <T extends (...args: any[]) => any>(fn: T, options?: Mu
91
91
  */
92
92
  declare const map: <T, U>(array: T[], fn: (item: T) => U, options?: CallOptions) => ParallelResult<U[]>;
93
93
  /**
94
- * Parallel `Array.prototype.filter` — distributes predicate across CPU cores.
94
+ * Parallel `Array.prototype.filter` - distributes predicate across CPU cores.
95
95
  *
96
96
  * @example
97
97
  * ```ts
@@ -100,7 +100,7 @@ declare const map: <T, U>(array: T[], fn: (item: T) => U, options?: CallOptions)
100
100
  */
101
101
  declare const filter: <T>(array: T[], fn: (item: T) => boolean, options?: CallOptions) => ParallelResult<T[]>;
102
102
  /**
103
- * Parallel `Array.prototype.reduce` — each worker reduces its chunk,
103
+ * Parallel `Array.prototype.reduce` - each worker reduces its chunk,
104
104
  * then main thread reduces the partial results.
105
105
  *
106
106
  * @example
@@ -125,12 +125,12 @@ declare const reduce: <T>(array: T[], fn: (a: T, b: T) => T, initial: T, options
125
125
  declare const dotProduct: <T extends NumericArray>(vectorsA: Vectors<T>, vectorsB: Vectors<T>, options?: OpOptions) => Float32Array;
126
126
 
127
127
  /**
128
- * Matrix multiplication. C = A × B
128
+ * Matrix multiplication. C = A x B
129
129
  *
130
130
  * Row-parallel: each "worker" computes a slice of C's rows.
131
131
  * Inner dot product uses the unrolled kernel (4/8/16) for the k-dimension.
132
132
  *
133
- * Complexity: O(M × N × K) with M = A.rows, K = A.cols = B.rows, N = B.cols.
133
+ * Complexity: O(M x N x K) with M = A.rows, K = A.cols = B.rows, N = B.cols.
134
134
  */
135
135
  declare const matmul: <T extends NumericArray>(A: Matrix<T>, B: Matrix<T>, options?: OpOptions) => Matrix<T>;
136
136
 
package/dist/index.mjs CHANGED
@@ -80,7 +80,7 @@ if (typeof require !== 'undefined') {
80
80
  parentPort.on('message', __handleMessage);
81
81
  }
82
82
  } catch (_) {
83
- // Not in worker_threads context \u2014 ignore
83
+ // Not in worker_threads context - ignore
84
84
  }
85
85
  }
86
86
  `.trim();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "defuss-multicore",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"