Commit c01723a
committed
stream: use ring buffer for WHATWG stream queues
The [[queue]] backing every default readable/writable controller was a
plain array of { value, size } wrappers consumed with
ArrayPrototypeShift, so each buffered chunk allocated a wrapper object
and each dequeue moved (or forced the engine to re-linearize) the
remaining elements; the byte controller queue paid the same shift cost
for its chunk descriptor records.
Replace the array with a power-of-two ring buffer. Default controller
queues store each entry as (value, size) in two consecutive slots, so
the per-chunk wrapper allocation disappears; the byte controller keeps
its descriptor records (they are mutated in place at the head) in
single slots. Controllers start from (and are reset to) a shared
immutable empty queue, so constructing a stream allocates no queue
storage until a chunk is actually buffered. Enqueues measured by the
internal default size algorithm (never observable by user code, always
returns 1, cannot throw) skip the algorithm call and its try/catch
entirely.
The layout mirrors what Bun/WebKit use for the same spec structure:
[[queue]] as a ring-buffer deque (WTF::Deque in Bun's
src/jsc/bindings/webcore/streams/StreamQueue.h), the pure-JS ring
buffer in Bun's src/js/internal/fifo.ts, and the trivial-size-algorithm
bypass in
src/jsc/bindings/webcore/streams/JSReadableStreamDefaultController.cpp.
benchmark/compare.js against the unmodified baseline (30-run capture
plus an independent 15-run repeat, Welch t-test, all p < 1e-5):
webstreams/pipe-to.js +12-17% across all sixteen high-water-mark
configurations, readable-read-buffered +20% (bufferSize=1) to +49%
(bufferSize=1000), readable-async-iterator +21%. No stable significant
regression across the rest of the webstreams suite: the creation.js and
readable-read.js deltas seen in the full-suite capture disappear in
isolated 60-run rechecks.
Refs: https://github.com/oven-sh/bun/blob/main/src/js/internal/fifo.ts
Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>1 parent 6f77105 commit c01723a
4 files changed
Lines changed: 352 additions & 25 deletions
File tree
- lib/internal/webstreams
- test/parallel
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| 116 | + | |
116 | 117 | | |
117 | 118 | | |
118 | 119 | | |
| 120 | + | |
119 | 121 | | |
120 | 122 | | |
121 | 123 | | |
| |||
2495 | 2497 | | |
2496 | 2498 | | |
2497 | 2499 | | |
| 2500 | + | |
| 2501 | + | |
| 2502 | + | |
| 2503 | + | |
| 2504 | + | |
2498 | 2505 | | |
2499 | 2506 | | |
2500 | 2507 | | |
| |||
2652 | 2659 | | |
2653 | 2660 | | |
2654 | 2661 | | |
2655 | | - | |
| 2662 | + | |
2656 | 2663 | | |
2657 | 2664 | | |
2658 | 2665 | | |
| |||
3112 | 3119 | | |
3113 | 3120 | | |
3114 | 3121 | | |
3115 | | - | |
3116 | | - | |
3117 | | - | |
3118 | | - | |
3119 | | - | |
3120 | | - | |
3121 | | - | |
3122 | | - | |
| 3122 | + | |
| 3123 | + | |
| 3124 | + | |
| 3125 | + | |
| 3126 | + | |
| 3127 | + | |
| 3128 | + | |
3123 | 3129 | | |
3124 | 3130 | | |
3125 | 3131 | | |
| |||
3174 | 3180 | | |
3175 | 3181 | | |
3176 | 3182 | | |
3177 | | - | |
| 3183 | + | |
3178 | 3184 | | |
3179 | 3185 | | |
3180 | 3186 | | |
| |||
3192 | 3198 | | |
3193 | 3199 | | |
3194 | 3200 | | |
3195 | | - | |
| 3201 | + | |
3196 | 3202 | | |
3197 | 3203 | | |
3198 | 3204 | | |
| |||
3405 | 3411 | | |
3406 | 3412 | | |
3407 | 3413 | | |
3408 | | - | |
| 3414 | + | |
3409 | 3415 | | |
3410 | 3416 | | |
3411 | 3417 | | |
| |||
3498 | 3504 | | |
3499 | 3505 | | |
3500 | 3506 | | |
3501 | | - | |
| 3507 | + | |
3502 | 3508 | | |
3503 | 3509 | | |
3504 | 3510 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | | - | |
8 | | - | |
9 | 8 | | |
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
137 | 274 | | |
138 | 275 | | |
139 | 276 | | |
140 | 277 | | |
141 | 278 | | |
142 | 279 | | |
143 | 280 | | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
150 | 285 | | |
151 | 286 | | |
152 | 287 | | |
153 | 288 | | |
154 | 289 | | |
155 | | - | |
| 290 | + | |
156 | 291 | | |
157 | 292 | | |
158 | 293 | | |
159 | 294 | | |
160 | 295 | | |
161 | 296 | | |
162 | | - | |
| 297 | + | |
163 | 298 | | |
164 | 299 | | |
165 | 300 | | |
| |||
170 | 305 | | |
171 | 306 | | |
172 | 307 | | |
173 | | - | |
| 308 | + | |
174 | 309 | | |
175 | 310 | | |
176 | 311 | | |
| |||
266 | 401 | | |
267 | 402 | | |
268 | 403 | | |
| 404 | + | |
269 | 405 | | |
270 | 406 | | |
271 | 407 | | |
| 408 | + | |
272 | 409 | | |
273 | 410 | | |
274 | 411 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
| 70 | + | |
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
| |||
1177 | 1178 | | |
1178 | 1179 | | |
1179 | 1180 | | |
| 1181 | + | |
| 1182 | + | |
| 1183 | + | |
| 1184 | + | |
| 1185 | + | |
1180 | 1186 | | |
1181 | 1187 | | |
1182 | 1188 | | |
| |||
1293 | 1299 | | |
1294 | 1300 | | |
1295 | 1301 | | |
1296 | | - | |
| 1302 | + | |
1297 | 1303 | | |
1298 | 1304 | | |
1299 | 1305 | | |
| |||
0 commit comments