From 9e5ba850a72abdba73333d16677c36632e701f5a Mon Sep 17 00:00:00 2001 From: Devin Ivy Date: Thu, 9 Apr 2026 17:52:03 -0400 Subject: [PATCH] refactor: rename pool() to workers() Co-Authored-By: Claude Opus 4.6 (1M context) --- examples/atomics/main.ts | 4 ++-- examples/non-blocking/main.ts | 4 ++-- examples/parallel-batch/main.ts | 4 ++-- examples/shared-state/main.ts | 4 ++-- src/index.ts | 2 +- src/worker-pool.ts | 12 ++++++------ test/error.test.ts | 4 ++-- test/pool.test.ts | 12 ++++++------ test/shared/cross-worker.test.ts | 20 ++++++++++---------- test/transfer.test.ts | 10 +++++----- 10 files changed, 38 insertions(+), 38 deletions(-) diff --git a/examples/atomics/main.ts b/examples/atomics/main.ts index f522d34..ac7b803 100644 --- a/examples/atomics/main.ts +++ b/examples/atomics/main.ts @@ -3,13 +3,13 @@ // // Run: node examples/atomics/main.ts -import { pool, int32atomic } from '../../src/index.ts'; +import { workers, int32atomic } from '../../src/index.ts'; import { increment } from './increment.ts'; const counter = int32atomic(); { - using run = pool(4); + using run = workers(4); // Fire off 100 increments across 4 workers await Promise.all(Array.from({ length: 100 }, () => run(increment(counter)))); diff --git a/examples/non-blocking/main.ts b/examples/non-blocking/main.ts index a04c744..fee8eac 100644 --- a/examples/non-blocking/main.ts +++ b/examples/non-blocking/main.ts @@ -4,7 +4,7 @@ // // Run: node examples/non-blocking/main.ts -import { pool } from '../../src/index.ts'; +import { workers } from '../../src/index.ts'; import { fibonacci } from './fibonacci.ts'; // Tick a counter on the main thread to prove it's not blocked @@ -18,7 +18,7 @@ console.log('Computing fibonacci(42) on a worker pool...'); console.log('Meanwhile, the main thread keeps ticking:\n'); { - using run = pool(2); + using run = workers(2); const start = performance.now(); const [a, b] = await Promise.all([run(fibonacci(42)), run(fibonacci(41))]); const elapsed = (performance.now() - start).toFixed(0); diff --git a/examples/parallel-batch/main.ts b/examples/parallel-batch/main.ts index 40db7dc..d891cd8 100644 --- a/examples/parallel-batch/main.ts +++ b/examples/parallel-batch/main.ts @@ -4,7 +4,7 @@ // // Run: node examples/parallel-batch/main.ts -import { pool } from '../../src/index.ts'; +import { workers } from '../../src/index.ts'; import { heavyWork } from './heavy-work.ts'; const items = Array.from({ length: 20 }, (_, i) => i + 1); @@ -23,7 +23,7 @@ console.log(` ${items.length} items in ${seqTime}ms\n`); let parTime: string; console.log('Parallel (worker pool, 4 workers)...'); { - using run = pool(4); + using run = workers(4); const parStart = performance.now(); const parResults = await Promise.all(items.map((item) => run(heavyWork(item)))); parTime = (performance.now() - parStart).toFixed(0); diff --git a/examples/shared-state/main.ts b/examples/shared-state/main.ts index 6625d00..89d3b63 100644 --- a/examples/shared-state/main.ts +++ b/examples/shared-state/main.ts @@ -4,7 +4,7 @@ // // Run: node examples/shared-state/main.ts -import { pool, shared, int32, mutex } from '../../src/index.ts'; +import { workers, shared, int32, mutex } from '../../src/index.ts'; import { updatePosition } from './update-position.ts'; const lock = mutex(); @@ -13,7 +13,7 @@ const pos = shared({ x: int32, y: int32 }); const steps = 1000; { - using run = pool(4); + using run = workers(4); // 4 workers each move the position (1, 2) per step, 1000 steps each await Promise.all([ diff --git a/src/index.ts b/src/index.ts index 8ed5df1..ae484b6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ export { mo } from './mo.ts'; export { Task } from './task.ts'; -export { pool } from './worker-pool.ts'; +export { workers } from './worker-pool.ts'; export { transfer } from './transfer.ts'; export type { Runner } from './runner.ts'; export { diff --git a/src/worker-pool.ts b/src/worker-pool.ts index af5b609..c042d57 100644 --- a/src/worker-pool.ts +++ b/src/worker-pool.ts @@ -5,13 +5,13 @@ import type { Runner } from './runner.ts'; const workerEntryUrl = new URL('./worker-entry.ts', import.meta.url); -export function pool(size: number): Runner { - const workers: Worker[] = []; +export function workers(size: number): Runner { + const pool: Worker[] = []; for (let i = 0; i < size; i++) { const worker = new Worker(workerEntryUrl); worker.unref(); setupWorker(worker); - workers.push(worker); + pool.push(worker); } let next = 0; @@ -20,17 +20,17 @@ export function pool(size: number): Runner { const run: Runner = Object.assign( (task: Task): Promise => { if (disposed) return Promise.reject(new Error('Worker pool is disposed')); - const worker = workers[next % workers.length]; + const worker = pool[next % pool.length]; next++; return execute(worker, task.id, task.args); }, { [Symbol.dispose]() { disposed = true; - for (const worker of workers) { + for (const worker of pool) { worker.terminate(); } - workers.length = 0; + pool.length = 0; }, }, ); diff --git a/test/error.test.ts b/test/error.test.ts index c279951..735f128 100644 --- a/test/error.test.ts +++ b/test/error.test.ts @@ -1,6 +1,6 @@ import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; -import { pool } from 'moroutine'; +import { workers } from 'moroutine'; import { fail } from './fixtures/math.ts'; describe('error handling', () => { @@ -11,7 +11,7 @@ describe('error handling', () => { }); it('rejects with error from pool worker', async () => { - const run = pool(1); + const run = workers(1); try { await assert.rejects(() => run(fail('pool boom')), { message: 'pool boom', diff --git a/test/pool.test.ts b/test/pool.test.ts index 4b056eb..579d014 100644 --- a/test/pool.test.ts +++ b/test/pool.test.ts @@ -1,11 +1,11 @@ import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; -import { pool } from 'moroutine'; +import { workers } from 'moroutine'; import { double, add } from './fixtures/math.ts'; -describe('pool', () => { +describe('workers', () => { it('executes a moroutine through the pool', async () => { - const run = pool(2); + const run = workers(2); try { const result = await run(double(2)); assert.equal(result, 4); @@ -15,7 +15,7 @@ describe('pool', () => { }); it('handles concurrent calls across pool workers', async () => { - const run = pool(2); + const run = workers(2); try { const results = await Promise.all([run(double(1)), run(double(2)), run(double(3)), run(double(4))]); assert.deepEqual(results, [2, 4, 6, 8]); @@ -25,7 +25,7 @@ describe('pool', () => { }); it('handles multiple argument moroutines', async () => { - const run = pool(1); + const run = workers(1); try { const result = await run(add(10, 20)); assert.equal(result, 30); @@ -35,7 +35,7 @@ describe('pool', () => { }); it('dispose terminates pool workers', async () => { - const run = pool(2); + const run = workers(2); await run(double(1)); run[Symbol.dispose](); // After dispose, calls should fail diff --git a/test/shared/cross-worker.test.ts b/test/shared/cross-worker.test.ts index c4f3ea1..0908c21 100644 --- a/test/shared/cross-worker.test.ts +++ b/test/shared/cross-worker.test.ts @@ -1,13 +1,13 @@ import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; -import { pool, int32atomic, mutex, rwlock, shared, int32, string, bytes } from 'moroutine'; +import { workers, int32atomic, mutex, rwlock, shared, int32, string, bytes } from 'moroutine'; import { atomicAdd, mutexIncrement, rwlockRead } from '../fixtures/sync.ts'; import { readValue, writeValue } from '../fixtures/shared-new.ts'; describe('shared primitives across workers', () => { it('Int32Atomic is shared across worker threads', async () => { const counter = int32atomic(); - const run = pool(2); + const run = workers(2); try { await Promise.all([run(atomicAdd(counter, 10)), run(atomicAdd(counter, 20))]); assert.equal(counter.load(), 30); @@ -19,7 +19,7 @@ describe('shared primitives across workers', () => { it('Mutex serializes access across workers', async () => { const m = mutex(); const counter = int32atomic(); - const run = pool(2); + const run = workers(2); try { await Promise.all([run(mutexIncrement(m, counter, 100)), run(mutexIncrement(m, counter, 100))]); assert.equal(counter.load(), 200); @@ -32,7 +32,7 @@ describe('shared primitives across workers', () => { const rw = rwlock(); const counter = int32atomic(); counter.store(42); - const run = pool(2); + const run = workers(2); try { const [a, b] = await Promise.all([run(rwlockRead(rw, counter)), run(rwlockRead(rw, counter))]); assert.equal(a, 42); @@ -46,7 +46,7 @@ describe('shared primitives across workers', () => { const point = shared({ x: int32, y: int32 }); point.store({ x: 1, y: 2 }); - const run = pool(1); + const run = workers(1); try { const result = await run(readValue(point)); assert.deepEqual(result, { x: 1, y: 2 }); @@ -58,7 +58,7 @@ describe('shared primitives across workers', () => { it('worker can write to struct via shared()', async () => { const point = shared({ x: int32, y: int32 }); - const run = pool(1); + const run = workers(1); try { await run(writeValue(point, { x: 10, y: 20 })); assert.deepEqual(point.load(), { x: 10, y: 20 }); @@ -71,7 +71,7 @@ describe('shared primitives across workers', () => { const t = shared([int32, int32]); t.store([1, 2]); - const run = pool(1); + const run = workers(1); try { const result = await run(readValue(t)); assert.deepEqual(result, [1, 2]); @@ -84,7 +84,7 @@ describe('shared primitives across workers', () => { const s = shared(string(32)); s.store('hello'); - const run = pool(1); + const run = workers(1); try { const result = await run(readValue(s)); assert.equal(result, 'hello'); @@ -97,7 +97,7 @@ describe('shared primitives across workers', () => { const b = shared(bytes(4)); b.store(new Uint8Array([1, 2, 3, 4])); - const run = pool(1); + const run = workers(1); try { const result = (await run(readValue(b))) as Uint8Array; assert.deepEqual([...result], [1, 2, 3, 4]); @@ -110,7 +110,7 @@ describe('shared primitives across workers', () => { const entity = shared({ name: string(16), hp: int32 }); entity.store({ name: 'goblin', hp: 50 }); - const run = pool(1); + const run = workers(1); try { const result = await run(readValue(entity)); assert.deepEqual(result, { name: 'goblin', hp: 50 }); diff --git a/test/transfer.test.ts b/test/transfer.test.ts index 168bd0f..bf2d7e2 100644 --- a/test/transfer.test.ts +++ b/test/transfer.test.ts @@ -1,6 +1,6 @@ import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; -import { pool, transfer } from 'moroutine'; +import { workers, transfer } from 'moroutine'; import { sumBuffer, sumUint8, makeBuffer } from './fixtures/transfer.ts'; describe('transfer', () => { @@ -12,7 +12,7 @@ describe('transfer', () => { view[2] = 3; view[3] = 4; - const run = pool(1); + const run = workers(1); try { const sum = await run(sumBuffer(transfer(buf))); assert.equal(sum, 10); @@ -27,7 +27,7 @@ describe('transfer', () => { const arr = new Uint8Array([5, 10, 15, 20]); const originalBuffer = arr.buffer; - const run = pool(1); + const run = workers(1); try { const sum = await run(sumUint8(transfer(arr))); assert.equal(sum, 50); @@ -39,7 +39,7 @@ describe('transfer', () => { }); it('auto-transfers return values from worker (zero-copy)', async () => { - const run = pool(1); + const run = workers(1); try { const buf = await run(makeBuffer(4)); const view = new Uint8Array(buf); @@ -58,7 +58,7 @@ describe('transfer', () => { view[2] = 3; view[3] = 4; - const run = pool(1); + const run = workers(1); try { const sum = await run(sumBuffer(buf)); assert.equal(sum, 10); -- 2.51.2