Repository navigation
Commit 008eb16
stream: avoid a promise per stream/iter from() read in pipelines
A pull() pipeline, pipeTo() with a signal and the consumers with a
signal reject their own pending pull, pipe or read when they are
aborted, and ignore the read of the source that is still pending then.
Yet they read a from() async source with its cancellable next(), which
gives every read a promise of its own so that a cancellation can reject
it at once: about 230 bytes per batch, for a rejection nobody waits for.
They now read such sources with kNextUncancellable, as pipeTo() without
a signal already did, when nothing but stream/iter code waits for the
reads: always for pipeTo() and the consumers, and for pull() pipelines
without stateful transforms. A stateful transform's generator waits for
the source in user code, so with one the reads stay cancellable and its
finally block still runs at once.
What makes this possible is that a cancellation now releases a pending
kNextUncancellable read instead of waiting for it: the normalization
ends, the source is closed at once (as for a cancelled next(), errors
ignored), and the operations queued behind the read, such as the
return() that cancelled it, run. The read settles when the source's
next() does, rejecting with the cancellation reason, and is ignored. A
pipeline stopped with return() or throw() while a pull is pending closes
its (stateless) transform layers without waiting for them, since they
wait for that read.
Allocation per batch, 16-byte chunks, async source:
- pipeTo() with a signal: 967 -> 738 bytes (as without a signal)
- pull() with one transform: 1518 -> 1289 bytes
- pipeTo() with a signal and one transform: 1829 -> 1601 bytes
test-stream-iter-abort-pending-read.js checks, for pull(), pipeTo() and
bytes(), with and without transforms and stopped by a signal, return()
or throw() while the source's next() is pending, that the call rejects
at once, the source is closed before that next() settles, and nothing
happens when it does. It also passes before this change.
Assisted-by: OpenCode
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: #66504
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>1 parent 1503971 commit 008eb16
4 files changed
Lines changed: 287 additions & 23 deletions
File tree
- lib/internal/streams/iter
- test/parallel
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| 58 | + | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
| |||
171 | 172 | | |
172 | 173 | | |
173 | 174 | | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
174 | 178 | | |
175 | 179 | | |
| 180 | + | |
176 | 181 | | |
177 | | - | |
| 182 | + | |
| 183 | + | |
178 | 184 | | |
179 | 185 | | |
180 | 186 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
152 | 152 | | |
153 | 153 | | |
154 | 154 | | |
155 | | - | |
156 | | - | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
157 | 162 | | |
158 | 163 | | |
159 | 164 | | |
| |||
591 | 596 | | |
592 | 597 | | |
593 | 598 | | |
594 | | - | |
595 | | - | |
596 | | - | |
597 | | - | |
598 | | - | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
599 | 605 | | |
600 | 606 | | |
601 | 607 | | |
| |||
836 | 842 | | |
837 | 843 | | |
838 | 844 | | |
839 | | - | |
840 | | - | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
841 | 848 | | |
| 849 | + | |
842 | 850 | | |
843 | 851 | | |
844 | 852 | | |
| |||
915 | 923 | | |
916 | 924 | | |
917 | 925 | | |
| 926 | + | |
| 927 | + | |
918 | 928 | | |
919 | 929 | | |
920 | 930 | | |
| |||
925 | 935 | | |
926 | 936 | | |
927 | 937 | | |
| 938 | + | |
| 939 | + | |
928 | 940 | | |
929 | 941 | | |
930 | 942 | | |
931 | 943 | | |
| 944 | + | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
932 | 973 | | |
933 | 974 | | |
934 | 975 | | |
| |||
954 | 995 | | |
955 | 996 | | |
956 | 997 | | |
957 | | - | |
958 | | - | |
959 | | - | |
960 | | - | |
| 998 | + | |
961 | 999 | | |
962 | 1000 | | |
963 | 1001 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1233 | 1233 | | |
1234 | 1234 | | |
1235 | 1235 | | |
1236 | | - | |
1237 | | - | |
| 1236 | + | |
| 1237 | + | |
| 1238 | + | |
| 1239 | + | |
| 1240 | + | |
| 1241 | + | |
| 1242 | + | |
| 1243 | + | |
| 1244 | + | |
| 1245 | + | |
| 1246 | + | |
| 1247 | + | |
| 1248 | + | |
1238 | 1249 | | |
1239 | 1250 | | |
| 1251 | + | |
1240 | 1252 | | |
1241 | 1253 | | |
1242 | 1254 | | |
1243 | 1255 | | |
1244 | 1256 | | |
1245 | 1257 | | |
| 1258 | + | |
| 1259 | + | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
| 1263 | + | |
1246 | 1264 | | |
1247 | 1265 | | |
1248 | 1266 | | |
1249 | | - | |
| 1267 | + | |
| 1268 | + | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
| 1273 | + | |
1250 | 1274 | | |
1251 | 1275 | | |
1252 | 1276 | | |
| |||
1290 | 1314 | | |
1291 | 1315 | | |
1292 | 1316 | | |
| 1317 | + | |
| 1318 | + | |
| 1319 | + | |
| 1320 | + | |
| 1321 | + | |
| 1322 | + | |
| 1323 | + | |
1293 | 1324 | | |
1294 | 1325 | | |
1295 | 1326 | | |
| |||
1377 | 1408 | | |
1378 | 1409 | | |
1379 | 1410 | | |
| 1411 | + | |
| 1412 | + | |
1380 | 1413 | | |
1381 | 1414 | | |
1382 | 1415 | | |
| |||
1437 | 1470 | | |
1438 | 1471 | | |
1439 | 1472 | | |
| 1473 | + | |
| 1474 | + | |
| 1475 | + | |
| 1476 | + | |
| 1477 | + | |
| 1478 | + | |
| 1479 | + | |
| 1480 | + | |
1440 | 1481 | | |
1441 | 1482 | | |
1442 | 1483 | | |
| |||
1542 | 1583 | | |
1543 | 1584 | | |
1544 | 1585 | | |
1545 | | - | |
| 1586 | + | |
| 1587 | + | |
1546 | 1588 | | |
1547 | 1589 | | |
1548 | 1590 | | |
| |||
1600 | 1642 | | |
1601 | 1643 | | |
1602 | 1644 | | |
| 1645 | + | |
| 1646 | + | |
| 1647 | + | |
| 1648 | + | |
| 1649 | + | |
1603 | 1650 | | |
1604 | 1651 | | |
1605 | 1652 | | |
| |||
1618 | 1665 | | |
1619 | 1666 | | |
1620 | 1667 | | |
| 1668 | + | |
| 1669 | + | |
| 1670 | + | |
| 1671 | + | |
| 1672 | + | |
1621 | 1673 | | |
1622 | 1674 | | |
1623 | 1675 | | |
| |||
2040 | 2092 | | |
2041 | 2093 | | |
2042 | 2094 | | |
2043 | | - | |
| 2095 | + | |
| 2096 | + | |
| 2097 | + | |
2044 | 2098 | | |
| 2099 | + | |
2045 | 2100 | | |
2046 | | - | |
| 2101 | + | |
| 2102 | + | |
2047 | 2103 | | |
2048 | 2104 | | |
2049 | 2105 | | |
| |||
2144 | 2200 | | |
2145 | 2201 | | |
2146 | 2202 | | |
2147 | | - | |
2148 | | - | |
2149 | | - | |
| 2203 | + | |
| 2204 | + | |
2150 | 2205 | | |
2151 | 2206 | | |
2152 | 2207 | | |
| |||
0 commit comments