Commit 1af5e80
committed
stream: performance optimizations for stream/iter pipeline and broadcast
Optimize the stream/iter implementation based on benchmark analysis
comparing classic streams, web streams, and stream/iter.
- Eliminate `withFlushSignalAsync`/`withFlushSignalSync` generator wrappers
from the stateless transform pipeline. Stateless transforms now handle
their own flush (`null`) signal internally after the for-await loop,
removing an entire async generator layer per pipeline. Stateful
transforms retain the wrapper since their cost is dominated by the
transform operation itself (compression, encryption, etc).
- Hoist writer capability checks in `pipeTo`/`pipeToSync`. Property lookups
for `writeSync`/`writevSync`/`endSync`/`failSync` are done once before the
loop instead of per-chunk via optional chaining. Split signal/no-signal
loops to avoid per-batch null checks. Added `writevSync` batch write
support to `pipeToSync`.
- Optimize `isUint8ArrayBatch` with single-element fast path and plain
for loop. Replaces `ArrayPrototypeEvery` (function call per element)
with direct indexed loop. Short-circuits on length 1 (most common
from transforms) and checks first/last before iterating middle.
- Make broadcast consumer `next()`/`return()`/`throw()` non-async. Returns
`PromiseResolve()` directly on the fast path (data in buffer) instead
of wrapping through async function machinery. Caches the done result.
- `RingBuffer`: replace modulo with bitwise AND. Capacity is always a
power of 2, so index computation uses & mask instead of % capacity.
- `Broadcast`: incremental min-cursor tracking. Replaces O(N) full scan
of all consumers on every `next()` call with a cached min cursor that
is only recomputed when dirty (consumer at the minimum advances or
detaches). Eliminates O(N^2) per-write-cycle scaling.
- `Broadcast`: separate waiters list for `notifyConsumers`. Only iterates
consumers with pending resolve callbacks instead of scanning all
consumers on every write.
- `concatBytes`: cache per-chunk byte lengths to avoid reading `byteLength`
twice per chunk (once for total, once for offset advance). Remove
dead `totalByteLength` function.
Benchmark results (MB/s, higher is better):
| Benchmark | classic | webstream | iter | iter-sync | iter vs classic |
| ---------------- | ------- | --------- | ------ | --------- | --------------- |
| Identity 1MB | 1,245 | 582 | **3,110** | 16,658 | 2.5x |
| Identity 64MB | 31,410 | 14,980 | **33,894** | 62,111 | 1.1x |
| Transform 1MB | 287 | 227 | **325** | 327 | 1.1x |
| Transform 64MB | 595 | 605 | **605** | 573 | 1.0x |
| Compression 1MB | **123** | 98 | 110 | -- | 0.9x |
| Compression 64MB | **329** | 303 | 308 | -- | 0.9x |
| pipeTo 1MB | 1,137 | 494 | **2,740** | 13,611 | 2.4x |
| pipeTo 64MB | 22,081 | 15,377 | **30,036** | 60,976 | 1.4x |
| Broadcast 1c 1MB | 1,365 | 521 | **1,991** | -- | 1.5x |
| Broadcast 2c 1MB | 1,285 | 439 | **1,962** | -- | 1.5x |
| Broadcast 4c 1MB | **1,217** | 322 | 750 | -- | 0.6x |
| File read 16MB | 1,469 | 537 | **1,639** | -- | 1.1x |
The creation benchmarks show the raw cost of constructing the various
objects without any other activity. The `classic` Node.js streams are
faster here simply because they do less work on actual creation.
| Creation (ops/sec) | classic | webstream | iter | iter vs classic |
| ------------------ | --------- | --------- | --------- | --------------- |
| readable | 8,662,361 | 505,889 | 1,144,385 | 0.1x |
| writable | 3,856,139 | 269,950 | 1,285,210 | 0.3x |
| pair | 3,120,224 | 141,988 | 349,176 | 0.1x |1 parent 371dbe9 commit 1af5e80
5 files changed
Lines changed: 196 additions & 152 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
| |||
111 | 112 | | |
112 | 113 | | |
113 | 114 | | |
| 115 | + | |
114 | 116 | | |
115 | 117 | | |
116 | 118 | | |
117 | 119 | | |
118 | 120 | | |
| 121 | + | |
| 122 | + | |
119 | 123 | | |
120 | 124 | | |
121 | 125 | | |
| |||
166 | 170 | | |
167 | 171 | | |
168 | 172 | | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
169 | 179 | | |
170 | 180 | | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
171 | 193 | | |
172 | 194 | | |
173 | 195 | | |
174 | 196 | | |
175 | 197 | | |
176 | | - | |
| 198 | + | |
177 | 199 | | |
178 | | - | |
179 | | - | |
180 | | - | |
| 200 | + | |
| 201 | + | |
181 | 202 | | |
182 | 203 | | |
183 | 204 | | |
184 | 205 | | |
185 | 206 | | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
186 | 211 | | |
187 | 212 | | |
188 | | - | |
| 213 | + | |
| 214 | + | |
189 | 215 | | |
190 | 216 | | |
191 | 217 | | |
192 | 218 | | |
193 | 219 | | |
194 | | - | |
| 220 | + | |
195 | 221 | | |
196 | 222 | | |
197 | 223 | | |
198 | | - | |
199 | | - | |
200 | | - | |
| 224 | + | |
| 225 | + | |
201 | 226 | | |
202 | 227 | | |
203 | 228 | | |
204 | 229 | | |
205 | 230 | | |
| 231 | + | |
206 | 232 | | |
207 | 233 | | |
208 | 234 | | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
216 | 238 | | |
217 | 239 | | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
225 | 243 | | |
226 | 244 | | |
227 | 245 | | |
| |||
342 | 360 | | |
343 | 361 | | |
344 | 362 | | |
345 | | - | |
| 363 | + | |
346 | 364 | | |
347 | 365 | | |
348 | 366 | | |
349 | 367 | | |
350 | 368 | | |
351 | 369 | | |
352 | | - | |
| 370 | + | |
353 | 371 | | |
| 372 | + | |
354 | 373 | | |
355 | 374 | | |
356 | 375 | | |
357 | | - | |
358 | | - | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
359 | 380 | | |
360 | 381 | | |
361 | | - | |
| 382 | + | |
362 | 383 | | |
363 | 384 | | |
364 | 385 | | |
| |||
368 | 389 | | |
369 | 390 | | |
370 | 391 | | |
371 | | - | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
372 | 399 | | |
373 | 400 | | |
374 | 401 | | |
375 | 402 | | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
376 | 406 | | |
377 | 407 | | |
378 | 408 | | |
379 | 409 | | |
380 | 410 | | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
381 | 414 | | |
382 | 415 | | |
383 | 416 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
255 | 255 | | |
256 | 256 | | |
257 | 257 | | |
258 | | - | |
259 | | - | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
260 | 269 | | |
261 | 270 | | |
262 | 271 | | |
| |||
0 commit comments