Skip to content

Commit c6a60c5

Browse files
committed
feat: heavy perf updates
1 parent 3989be1 commit c6a60c5

37 files changed

+4003
-2985
lines changed

DOCS.md

Lines changed: 20 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> Complete guide to architecture, internal decisions, and performance optimizations.
44
5-
**Version:** 3.4.0 (TypeScript)
5+
**Version:** 4.0.0 (TypeScript)
66

77
---
88

@@ -64,7 +64,6 @@ const result = await bee(x => x * 2)(21) // 42
6464
| **Function caching** | LRU cache with vm.Script compilation |
6565
| **Worker affinity** | Routes same function to same worker for V8 JIT benefits |
6666
| **TypeScript** | Full type definitions included |
67-
| **Generators** | Stream results with generator functions |
6867
| **Cancellation** | AbortSignal support for task cancellation |
6968
| **Retry** | Automatic retry with exponential backoff |
7069
| **Request Coalescing** | Deduplicates identical simultaneous calls |
@@ -83,18 +82,18 @@ const result = await bee(x => x * 2)(21) // 42
8382
┌─────────────────────────────────────────────────────────────────────────┐
8483
│ index.ts (Public API) │
8584
│ • bee() - Simple curried API │
86-
│ • beeThreads.run/withTimeout/stream
85+
│ • beeThreads.run/withTimeout/turbo/worker
8786
│ • configure/shutdown/warmup/getPoolStats │
8887
└─────────────────────────────────────────────────────────────────────────┘
8988
90-
┌──────────────────────────────┐
91-
92-
┌──────────────┐ ┌──────────────┐ ┌──────────────┐
93-
│ executor.ts │ │stream-exec.ts│ │ pool.ts │
94-
│ Fluent API │ │Generator API │ │ Worker mgmt │
95-
└──────────────┘ └──────────────┘ └──────────────┘
96-
97-
└──────────────────────────────┘
89+
┌──────────────────────────────┐
90+
91+
┌──────────────┐ ┌──────────────┐
92+
│ executor.ts │ │ pool.ts │
93+
│ Fluent API │ │ Worker mgmt │
94+
└──────────────┘ └──────────────┘
95+
96+
└──────────────────────────────┘
9897
9998
┌─────────────────────────────────────────────────────────────────────────┐
10099
│ execution.ts (Task Engine) │
@@ -105,16 +104,12 @@ const result = await bee(x => x * 2)(21) // 42
105104
│ • Metrics tracking │
106105
└─────────────────────────────────────────────────────────────────────────┘
107106
108-
┌─────────────────────┴──────────────────────┐
109-
▼ ▼
110-
┌─────────────────────────────┐ ┌─────────────────────────────┐
111-
│ worker.ts │ │ generator-worker.ts │
112-
│ • vm.Script compilation │ │ • Streaming yields │
113-
│ • LRU function cache │ │ • Return value capture │
114-
│ • Curried fn support │ │ • Same optimizations │
115-
│ • Console forwarding │ │ │
116-
│ • Error property preserve │ │ │
117-
└─────────────────────────────┘ └─────────────────────────────┘
107+
108+
┌─────────────────────────────────────────────────────────────────────────┐
109+
│ worker.ts │
110+
│ • vm.Script compilation • LRU function cache • Curried fn support│
111+
│ • Console forwarding • Error property preserve │
112+
└─────────────────────────────────────────────────────────────────────────┘
118113
```
119114

120115
---
@@ -181,9 +176,6 @@ const MessageType = {
181176
SUCCESS: 'success',
182177
ERROR: 'error',
183178
LOG: 'log',
184-
YIELD: 'yield',
185-
RETURN: 'return',
186-
END: 'end',
187179
} as const
188180

189181
// Logger interface (compatible with Pino, Winston, console)
@@ -208,15 +200,15 @@ Single source of truth for ALL mutable state. Makes debugging easier and testing
208200
**What it does:**
209201

210202
- Stores pool configuration (`poolSize`, `minThreads`, `timeout`, etc.)
211-
- Manages worker pools (`pools.normal`, `pools.generator`)
203+
- Manages worker pool (`pools.normal`)
212204
- Maintains O(1) counters for busy/idle workers
213205
- Tracks execution metrics
214206

215207
**State managed:**
216208

217209
```typescript
218210
config // User settings (poolSize, timeout, retry, etc.)
219-
pools // Active workers { normal: Worker[], generator: Worker[] }
211+
pools // Active workers { normal: Worker[] }
220212
poolCounters // O(1) counters { busy: N, idle: N }
221213
queues // Pending tasks by priority { high: [], normal: [], low: [] }
222214
metrics // Execution statistics
@@ -352,66 +344,6 @@ setContext(context: Record<string, unknown>): Executor<T> {
352344

353345
---
354346

355-
### `src/stream-executor.ts` - Generator Streaming
356-
357-
**Why it exists:**
358-
Enables streaming results from generator functions.
359-
360-
**What it does:**
361-
362-
- Creates `ReadableStream` from generator functions
363-
- Streams yielded values as they're produced
364-
- Captures return value for access after completion
365-
- Handles cleanup on cancel
366-
367-
**Chainable methods:**
368-
369-
| Method | Description |
370-
| ----------------------- | -------------------------------------------- |
371-
| `.usingParams(...args)` | Pass arguments to the generator |
372-
| `.setContext(obj)` | Inject external variables (closures) |
373-
| `.transfer([...]))` | Zero-copy for large binary data |
374-
| `.reconstructBuffers()` | Convert Uint8Array back to Buffer in results |
375-
| `.execute()` | Start streaming |
376-
377-
**Example:**
378-
379-
```js
380-
const stream = beeThreads
381-
.stream(function* (n) {
382-
for (let i = 1; i <= n; i++) {
383-
yield i * i
384-
}
385-
return 'done'
386-
})
387-
.usingParams(5)
388-
.execute()
389-
390-
for await (const value of stream) {
391-
console.log(value) // 1, 4, 9, 16, 25
392-
}
393-
394-
console.log(stream.returnValue) // 'done'
395-
```
396-
397-
**With Buffer reconstruction:**
398-
399-
```js
400-
const stream = beeThreads
401-
.stream(function* () {
402-
yield require('fs').readFileSync('chunk1.bin')
403-
yield require('fs').readFileSync('chunk2.bin')
404-
})
405-
.reconstructBuffers()
406-
.execute()
407-
408-
for await (const chunk of stream) {
409-
console.log(Buffer.isBuffer(chunk)) // true ✅
410-
}
411-
```
412-
413-
---
414-
415347
### `src/cache.ts` - LRU Function Cache
416348

417349
**Why it exists:**
@@ -489,33 +421,6 @@ function serializeError(e: unknown): SerializedError {
489421

490422
---
491423

492-
### `src/generator-worker.ts` - Generator Worker Script
493-
494-
**Why it exists:**
495-
The code that runs inside worker threads for generator functions.
496-
497-
**What it does:**
498-
499-
- Same as `worker.ts` but handles generators
500-
- Streams yielded values back to main thread
501-
- Captures return value for final message
502-
- Handles async generators (yields that return Promises)
503-
504-
**Message flow:**
505-
506-
```
507-
Main Thread Worker Thread
508-
| |
509-
|-------- { fn, args } ------->|
510-
| | (execute generator)
511-
|<------ { type: YIELD } ------|
512-
|<------ { type: YIELD } ------|
513-
|<------ { type: RETURN } -----|
514-
|<------ { type: END } --------|
515-
```
516-
517-
---
518-
519424
### `src/errors.ts` - Custom Error Classes
520425

521426
**Why it exists:**
@@ -1279,18 +1184,6 @@ beeThreads.configure({
12791184
9. pool.ts: Return worker to pool or process queued task
12801185
```
12811186

1282-
### Generator Streaming
1283-
1284-
```
1285-
1. User calls beeThreads.stream(gen).execute()
1286-
2. stream-executor.ts: Create ReadableStream, request worker
1287-
3. generator-worker.ts: Execute generator
1288-
4. For each yield: Send { type: YIELD, value }
1289-
5. On return: Send { type: RETURN, value }, { type: END }
1290-
6. stream-executor.ts: Enqueue values to ReadableStream
1291-
7. User consumes with for-await-of
1292-
```
1293-
12941187
---
12951188

12961189
## Error Handling
@@ -1462,10 +1355,9 @@ function detectBundlerMode(): boolean {
14621355
return false
14631356
}
14641357

1465-
export function getWorkerScript(type: PoolType): string {
1358+
export function getWorkerScript(): string {
14661359
if (USE_INLINE_WORKERS) {
1467-
const code = type === 'generator' ? INLINE_GENERATOR_WORKER_CODE : INLINE_WORKER_CODE
1468-
return createWorkerDataUrl(code)
1360+
return createWorkerDataUrl(INLINE_WORKER_CODE)
14691361
}
14701362
return path.join(__dirname, 'worker.js')
14711363
}

README.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,6 @@ const buffer = await beeThreads
120120
Buffer.isBuffer(buffer) // true
121121
```
122122

123-
### `beeThreads.stream()` - Generators
124-
125-
```ts
126-
const stream = beeThreads
127-
.stream(function* (n: number) {
128-
for (let i = 1; i <= n; i++) yield i * i
129-
})
130-
.usingParams(5)
131-
.execute()
132-
133-
for await (const value of stream) console.log(value) // 1, 4, 9, 16, 25
134-
```
135-
136123
---
137124

138125
## `beeThreads.turbo()` - Parallel Arrays

0 commit comments

Comments
 (0)