Skip to content

Use draining read in standard.c++ pumpTo impl#6119

Open
jasnell wants to merge 1 commit intomainfrom
jasnell/pumptoreader-draining
Open

Use draining read in standard.c++ pumpTo impl#6119
jasnell wants to merge 1 commit intomainfrom
jasnell/pumptoreader-draining

Conversation

@jasnell
Copy link
Collaborator

@jasnell jasnell commented Feb 20, 2026

Part of ongoing incremental perf improvements. This updates the standard->to->internal pumpTo loop (consuming a JS readable stream and writing the data to a kj destination) to use the new draining read optimization.

In a simple local benchmark, we see...

| Metric         | Main      | New       | Improvement       |
| -------------- | --------- | --------- | ----------------- |
| Avg Req/Sec    | 777       | 5,026     | 6.5x (+547%)      |
| Avg Latency    | 12.34 ms  | 1.35 ms   | 9.1x lower (-89%) |
| p50 Latency    | 10 ms     | 1 ms      | 10x lower         |
| p99 Latency    | 45 ms     | 8 ms      | 5.6x lower        |
| Max Latency    | 1,771 ms  | 22 ms     | 80x lower         |
| Avg Throughput | 1.49 MB/s | 7.19 MB/s | 4.8x              |
| Total Requests | 9k        | 55k       | 6.1x              |

the number vary broadly based on chunk size, write pattern, etc but in general we should see significant improvements across the board.

The specific scenario that is improved are things like return new Response(new ReadableStream(...)) in a worker fetch handler. That takes the JS-backed ReadableStream and drains it into the KJ stream at the internal level.

The specific optimization here is that we no longer perform a 1-to-1 read/write loop but instead drain as much data as we can synchronously off the JS-backed ReadableStream every time we are holding the isolate loop the coalescing writes to the kj layer. It means fewer JS->kj iterations, fewer times that we have to take the isolate lock, fewer promise allocations, fewer data copies, etc.

@jasnell jasnell requested review from a team as code owners February 20, 2026 00:20
@jasnell jasnell force-pushed the jasnell/pumptoreader-draining branch from 79c9aa8 to ce2a6f3 Compare February 20, 2026 00:21
@jasnell jasnell force-pushed the jasnell/pumptoreader-draining branch from ce2a6f3 to b717556 Compare February 20, 2026 21:10
@codspeed-hq

This comment was marked as outdated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant