Commit 6d140aa
committed
stream: reduce allocations on WHATWG streams hot paths
Pure-JavaScript optimizations to lib/internal/webstreams/*:
- Reuse the pull and write promise-reaction closures per controller
instead of allocating two fresh closures per chunk. They are created
lazily on the first pull/write so construction-only workloads never
pay for them.
- Add the buffered fast path to the ReadableStream async iterator,
mirroring the one ReadableStreamDefaultReader.read() already has:
when data is queued in a default controller, dequeue directly and
skip the read request, the deferred promise, and the promise
chaining on the following call.
- Run the post-start step through queueMicrotask() when the start
algorithm result is not an object (fulfillment is guaranteed and no
.then lookup is observable), instead of wrapping it in
new Promise((r) => r(result)) plus a two-closure reaction. Object
and thenable results keep the promise path since their adoption
timing and .then lookups are observable.
- Specialize the promise-callback wrappers for user algorithms by
arity (0/1/2), replacing the rest-parameter + ReflectApply form that
allocated an arguments array per invocation. The exact number of
arguments each user callback observes is preserved.
- Share immutable nil records for the writable stream closeRequest,
inFlightWriteRequest, inFlightCloseRequest and pendingAbortRequest
resets; these records are only ever replaced wholesale. Push the
PromiseWithResolvers() record directly as the write request rather
than rebuilding an identical object.
- Remove dead per-instance allocations: the never-read close record in
the writable stream state, the placeholder close/ready records that
reader/writer setup unconditionally replaces, the per-stream
() => 1 size algorithm closures, and the kControllerErrorFunction
placeholder plus bound function (now a prototype method; byte
streams keep their historical no-op behavior there).
Benchmark results vs main, same-day builds, benchmark/compare.js
--runs 10 (statistically significant rows):
creation WritableStream (n=500k) +97.1% ***
creation TransformStream (n=500k) +50.3% ***
creation ReadableStream (n=500k) +13.9% ***
creation ReadableStreamBYOBReader (n=500k) +13.0% ***
creation ReadableStreamDefaultReader (n=500k) +9.8% **
readable-async-iterator +38.1% ***
pipe-to (16 hwm configs) +2.6..+4.5% (all positive)
js_transfer WS / TS / RS +7.7% / +6.3% / +3.0%
readable-read normal/byob, read-buffered parity (n.s.)
WPT streams/compression/encoding results are identical to main
(1403/338/3822 subtests passed, same 8 expected failures by name),
and all webstreams-related parallel tests pass.
Assisted-by: Claude Fable 5 <noreply@anthropic.com>1 parent 19c46ab commit 6d140aa
4 files changed
Lines changed: 228 additions & 165 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
| 107 | + | |
108 | 108 | | |
| 109 | + | |
109 | 110 | | |
110 | 111 | | |
111 | 112 | | |
| |||
252 | 253 | | |
253 | 254 | | |
254 | 255 | | |
255 | | - | |
256 | 256 | | |
257 | 257 | | |
258 | 258 | | |
| |||
294 | 294 | | |
295 | 295 | | |
296 | 296 | | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
297 | 306 | | |
298 | 307 | | |
299 | 308 | | |
| |||
535 | 544 | | |
536 | 545 | | |
537 | 546 | | |
538 | | - | |
539 | | - | |
540 | | - | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
541 | 579 | | |
542 | 580 | | |
543 | 581 | | |
| |||
611 | 649 | | |
612 | 650 | | |
613 | 651 | | |
614 | | - | |
| 652 | + | |
615 | 653 | | |
616 | 654 | | |
617 | 655 | | |
| |||
835 | 873 | | |
836 | 874 | | |
837 | 875 | | |
838 | | - | |
839 | | - | |
840 | | - | |
841 | | - | |
842 | | - | |
| 876 | + | |
| 877 | + | |
843 | 878 | | |
844 | 879 | | |
845 | 880 | | |
| |||
949 | 984 | | |
950 | 985 | | |
951 | 986 | | |
952 | | - | |
953 | | - | |
954 | | - | |
955 | | - | |
956 | | - | |
| 987 | + | |
| 988 | + | |
957 | 989 | | |
958 | 990 | | |
959 | 991 | | |
| |||
1287 | 1319 | | |
1288 | 1320 | | |
1289 | 1321 | | |
1290 | | - | |
| 1322 | + | |
1291 | 1323 | | |
1292 | 1324 | | |
1293 | 1325 | | |
| |||
2475 | 2507 | | |
2476 | 2508 | | |
2477 | 2509 | | |
2478 | | - | |
2479 | | - | |
2480 | | - | |
| 2510 | + | |
| 2511 | + | |
| 2512 | + | |
| 2513 | + | |
| 2514 | + | |
2481 | 2515 | | |
2482 | 2516 | | |
2483 | 2517 | | |
2484 | 2518 | | |
2485 | 2519 | | |
2486 | | - | |
2487 | | - | |
| 2520 | + | |
| 2521 | + | |
| 2522 | + | |
| 2523 | + | |
| 2524 | + | |
| 2525 | + | |
| 2526 | + | |
| 2527 | + | |
2488 | 2528 | | |
2489 | 2529 | | |
2490 | 2530 | | |
| |||
2547 | 2587 | | |
2548 | 2588 | | |
2549 | 2589 | | |
| 2590 | + | |
| 2591 | + | |
2550 | 2592 | | |
2551 | 2593 | | |
2552 | 2594 | | |
2553 | 2595 | | |
2554 | 2596 | | |
2555 | 2597 | | |
2556 | 2598 | | |
2557 | | - | |
2558 | 2599 | | |
2559 | 2600 | | |
2560 | 2601 | | |
| 2602 | + | |
| 2603 | + | |
| 2604 | + | |
| 2605 | + | |
| 2606 | + | |
| 2607 | + | |
| 2608 | + | |
| 2609 | + | |
| 2610 | + | |
| 2611 | + | |
| 2612 | + | |
| 2613 | + | |
| 2614 | + | |
| 2615 | + | |
| 2616 | + | |
2561 | 2617 | | |
2562 | 2618 | | |
2563 | 2619 | | |
| |||
2582 | 2638 | | |
2583 | 2639 | | |
2584 | 2640 | | |
2585 | | - | |
| 2641 | + | |
2586 | 2642 | | |
2587 | 2643 | | |
2588 | | - | |
| 2644 | + | |
2589 | 2645 | | |
2590 | 2646 | | |
2591 | 2647 | | |
| |||
3236 | 3292 | | |
3237 | 3293 | | |
3238 | 3294 | | |
3239 | | - | |
3240 | | - | |
3241 | | - | |
| 3295 | + | |
| 3296 | + | |
| 3297 | + | |
| 3298 | + | |
3242 | 3299 | | |
3243 | 3300 | | |
3244 | 3301 | | |
3245 | 3302 | | |
3246 | 3303 | | |
3247 | | - | |
3248 | | - | |
| 3304 | + | |
| 3305 | + | |
| 3306 | + | |
| 3307 | + | |
| 3308 | + | |
| 3309 | + | |
| 3310 | + | |
| 3311 | + | |
3249 | 3312 | | |
3250 | 3313 | | |
3251 | 3314 | | |
| |||
3367 | 3430 | | |
3368 | 3431 | | |
3369 | 3432 | | |
| 3433 | + | |
| 3434 | + | |
3370 | 3435 | | |
3371 | 3436 | | |
3372 | 3437 | | |
| |||
3381 | 3446 | | |
3382 | 3447 | | |
3383 | 3448 | | |
| 3449 | + | |
| 3450 | + | |
| 3451 | + | |
| 3452 | + | |
| 3453 | + | |
| 3454 | + | |
| 3455 | + | |
| 3456 | + | |
| 3457 | + | |
| 3458 | + | |
| 3459 | + | |
| 3460 | + | |
3384 | 3461 | | |
3385 | 3462 | | |
3386 | 3463 | | |
| |||
3405 | 3482 | | |
3406 | 3483 | | |
3407 | 3484 | | |
3408 | | - | |
| 3485 | + | |
3409 | 3486 | | |
3410 | 3487 | | |
3411 | | - | |
| 3488 | + | |
3412 | 3489 | | |
3413 | 3490 | | |
3414 | 3491 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
| 47 | + | |
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
| |||
462 | 463 | | |
463 | 464 | | |
464 | 465 | | |
465 | | - | |
| 466 | + | |
466 | 467 | | |
467 | 468 | | |
468 | | - | |
| 469 | + | |
469 | 470 | | |
470 | 471 | | |
471 | | - | |
| 472 | + | |
472 | 473 | | |
473 | 474 | | |
474 | 475 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | | - | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
72 | 76 | | |
73 | | - | |
| 77 | + | |
74 | 78 | | |
75 | 79 | | |
76 | 80 | | |
| |||
169 | 173 | | |
170 | 174 | | |
171 | 175 | | |
172 | | - | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
173 | 193 | | |
174 | | - | |
| 194 | + | |
175 | 195 | | |
176 | 196 | | |
177 | 197 | | |
| |||
213 | 233 | | |
214 | 234 | | |
215 | 235 | | |
216 | | - | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
217 | 239 | | |
| 240 | + | |
218 | 241 | | |
219 | 242 | | |
220 | 243 | | |
| |||
0 commit comments