Repository navigation
Commit f302fbd
stream: reduce per-chunk overhead in stream/iter consumers
bytes(), text(), arrayBuffer(), array() and their sync variants kept a
snapshot object for every collected chunk, plus a batch entry and its
views array, so they can detect chunks that were resized or detached
before the result is assembled. For streams of many small chunks this
dominated memory use: collecting 1,000,000 one-byte chunks peaked at
about 490 MB of heap for 1 MB of data.
A non-empty view of a fixed-length, non-shared ArrayBuffer can only
change by its buffer being detached, which makes its byteLength 0, so
recording its byteLength is enough. Keep a full snapshot only for
empty views and views of resizable or shared buffers. The same input
now peaks at about 60 MB and is collected about five times faster.
Assisted-by: OpenCode
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: #66483
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>1 parent 7ba60dc commit f302fbd
3 files changed
Lines changed: 98 additions & 35 deletions
File tree
- lib/internal/streams/iter
- test/parallel
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
58 | 57 | | |
59 | | - | |
| 58 | + | |
| 59 | + | |
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
102 | | - | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | 98 | | |
110 | 99 | | |
111 | 100 | | |
| |||
115 | 104 | | |
116 | 105 | | |
117 | 106 | | |
118 | | - | |
| 107 | + | |
| 108 | + | |
119 | 109 | | |
120 | 110 | | |
121 | 111 | | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
129 | 116 | | |
130 | 117 | | |
131 | | - | |
132 | 118 | | |
133 | 119 | | |
134 | | - | |
| 120 | + | |
135 | 121 | | |
136 | 122 | | |
137 | 123 | | |
| |||
146 | 132 | | |
147 | 133 | | |
148 | 134 | | |
149 | | - | |
| 135 | + | |
| 136 | + | |
150 | 137 | | |
151 | 138 | | |
152 | 139 | | |
153 | 140 | | |
154 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
155 | 144 | | |
156 | | - | |
| 145 | + | |
157 | 146 | | |
158 | 147 | | |
159 | 148 | | |
| |||
162 | 151 | | |
163 | 152 | | |
164 | 153 | | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
172 | 158 | | |
173 | 159 | | |
174 | | - | |
175 | 160 | | |
176 | 161 | | |
177 | | - | |
| 162 | + | |
178 | 163 | | |
179 | 164 | | |
180 | 165 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
235 | 236 | | |
236 | 237 | | |
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 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
238 | 284 | | |
239 | 285 | | |
240 | 286 | | |
| |||
489 | 535 | | |
490 | 536 | | |
491 | 537 | | |
| 538 | + | |
492 | 539 | | |
493 | 540 | | |
494 | 541 | | |
| |||
503 | 550 | | |
504 | 551 | | |
505 | 552 | | |
| 553 | + | |
506 | 554 | | |
507 | 555 | | |
508 | 556 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
167 | 167 | | |
168 | 168 | | |
169 | 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 | + | |
170 | 199 | | |
171 | 200 | | |
172 | 201 | | |
173 | 202 | | |
174 | 203 | | |
175 | 204 | | |
176 | 205 | | |
| 206 | + | |
177 | 207 | | |
178 | 208 | | |
0 commit comments