h2lean 0.11.0 → 0.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/client-session.js +31 -9
- package/lib/client-stream.js +81 -16
- package/lib/fast-tick.js +139 -62
- package/package.json +1 -1
package/lib/client-session.js
CHANGED
|
@@ -2083,7 +2083,9 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
2083
2083
|
this._frame(RST_STREAM, 0, promisedSid, body, true);
|
|
2084
2084
|
return;
|
|
2085
2085
|
}
|
|
2086
|
-
const
|
|
2086
|
+
const pushHeaders = this.listenerCount('stream') === 0
|
|
2087
|
+
? null
|
|
2088
|
+
: this._toObject(fields, fcount, neverNames(fields, fcount, flags));
|
|
2087
2089
|
// Create the pushed stream. Its writable half is pre-ended: the client never
|
|
2088
2090
|
// sends a request body for a push (node: writableEnded=true, writableFinished=true
|
|
2089
2091
|
// at the 'stream' event, measured v26.12.1).
|
|
@@ -2108,8 +2110,9 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
2108
2110
|
// flags 4 = NGHTTP2_FLAG_END_HEADERS, the only flag PUSH_PROMISE carries that
|
|
2109
2111
|
// the JS layer surfaces (measured v26.12.1). Synchronous so the application's
|
|
2110
2112
|
// 'push' subscription is in place before the response HEADERS arrive in the
|
|
2111
|
-
// same read batch.
|
|
2112
|
-
|
|
2113
|
+
// same read batch. The promise headers object exists only for that argument,
|
|
2114
|
+
// so with no listener the build above skips -- unobservable either way.
|
|
2115
|
+
this.emit('stream', stream, pushHeaders, 4);
|
|
2113
2116
|
}
|
|
2114
2117
|
|
|
2115
2118
|
// The shape cache: the server's design, ported. A block whose bytes have been
|
|
@@ -2186,6 +2189,11 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
2186
2189
|
fields = flat.items;
|
|
2187
2190
|
fcount = flat.count;
|
|
2188
2191
|
blockFlags = this._decoder.neverCount > 0 ? this._decoder.neverFlags : null;
|
|
2192
|
+
// The field list the stream holds must outlive this decode: the
|
|
2193
|
+
// scratch's backing array is cleared in place by the next one. The copy
|
|
2194
|
+
// that caches the shape serves the stream too; without a cache entry
|
|
2195
|
+
// the copy is the stream's own.
|
|
2196
|
+
let copy = null;
|
|
2189
2197
|
if (shapes !== null) {
|
|
2190
2198
|
if (++this._shapeMisses > 64 && this._shapeHits * 4 < this._shapeMisses) {
|
|
2191
2199
|
this._shapes = null;
|
|
@@ -2193,15 +2201,17 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
2193
2201
|
if (shapes.size >= MAX_SHAPES) {
|
|
2194
2202
|
shapes.clear();
|
|
2195
2203
|
}
|
|
2204
|
+
copy = fields.slice(0, fcount);
|
|
2196
2205
|
shapes.set(slot, {
|
|
2197
2206
|
block: block !== null ? Buffer.from(block) : pieceConcat(pieces, from, to),
|
|
2198
|
-
flat:
|
|
2207
|
+
flat: copy,
|
|
2199
2208
|
// a snapshot copy, not the decoder's live array: the next decode
|
|
2200
2209
|
// clears the array in place, and this entry must survive it
|
|
2201
2210
|
flags: blockFlags !== null ? blockFlags.slice() : null
|
|
2202
2211
|
});
|
|
2203
2212
|
}
|
|
2204
2213
|
}
|
|
2214
|
+
fields = copy !== null ? copy : fields.slice(0, fcount);
|
|
2205
2215
|
}
|
|
2206
2216
|
} catch (err) {
|
|
2207
2217
|
if (err instanceof HPackError) {
|
|
@@ -2236,7 +2246,20 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
2236
2246
|
if (this._refuseHeaders(sid, stream, fields, fcount)) {
|
|
2237
2247
|
return;
|
|
2238
2248
|
}
|
|
2239
|
-
const
|
|
2249
|
+
const sensitive = neverNames(fields, fcount, blockFlags);
|
|
2250
|
+
// The response object is not built here: the stream builds it at the first
|
|
2251
|
+
// read (lib/client-stream.js _onHeaders), so a client that only consumes
|
|
2252
|
+
// the body pays no per-response object at all -- the paired marginal is in
|
|
2253
|
+
// OPTIMIZATION.md item 48. This block still needs :status, read off the
|
|
2254
|
+
// same field list _refuseHeaders has just vouched for: exactly one on a
|
|
2255
|
+
// response, none on a trailer.
|
|
2256
|
+
let status;
|
|
2257
|
+
for (let i = 0; i < fcount; i += 2) {
|
|
2258
|
+
if (fields[i] === ':status') {
|
|
2259
|
+
status = fields[i + 1] | 0;
|
|
2260
|
+
break;
|
|
2261
|
+
}
|
|
2262
|
+
}
|
|
2240
2263
|
// RFC 8336 3.2 + core.js emitStreamEvent: a 421 (Misdirected Request) answer
|
|
2241
2264
|
// deletes the origin the request carried from originSet, and it does so here --
|
|
2242
2265
|
// before 'response' is scheduled, so a handler that reads `session.originSet`
|
|
@@ -2244,12 +2267,11 @@ class ClientHttp2Session extends EventEmitter {
|
|
|
2244
2267
|
// its lazy-seed side effect exactly as node's `session[kState].originSet =
|
|
2245
2268
|
// initOriginSet(session)` assignment does: on the first 421 of a session the set
|
|
2246
2269
|
// is created from the socket before the delete, and over h2c both stacks leave
|
|
2247
|
-
// the set unobservable (the getter is encrypted-gated).
|
|
2248
|
-
|
|
2249
|
-
if (headers[':status'] === 421) {
|
|
2270
|
+
// the set unobservable (the getter is encrypted-gated).
|
|
2271
|
+
if (status === 421) {
|
|
2250
2272
|
this._initOriginSet().delete(stream._origin);
|
|
2251
2273
|
}
|
|
2252
|
-
stream._onHeaders(
|
|
2274
|
+
stream._onHeaders(status, fields, fcount, sensitive, end, flags);
|
|
2253
2275
|
if (end) {
|
|
2254
2276
|
this._dropStream(sid);
|
|
2255
2277
|
}
|
package/lib/client-stream.js
CHANGED
|
@@ -38,10 +38,15 @@ class AbortError extends Error {
|
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
// `sentHeaders`/`sentTrailers
|
|
42
|
-
//
|
|
43
|
-
//
|
|
44
|
-
//
|
|
41
|
+
// `sentHeaders`/`sentTrailers` and the state flags node exposes through symbols
|
|
42
|
+
// are kept as plain fields here; they are not part of the documented client
|
|
43
|
+
// surface an application is expected to touch, and pinning them to node's symbol
|
|
44
|
+
// identity is an M2 compatibility decision, not a round-trip one. The inbound
|
|
45
|
+
// response/trailer objects node does not keep on the stream at all (they pass
|
|
46
|
+
// through the events only, core.js emitStreamEvent) are prototype getters over
|
|
47
|
+
// the unbuilt field list: the object comes into being at the first read, which
|
|
48
|
+
// is what keeps a body-only client free of per-response allocations
|
|
49
|
+
// (OPTIMIZATION.md item 48).
|
|
45
50
|
class ClientHttp2Stream extends Duplex {
|
|
46
51
|
constructor(session, id, _opts) {
|
|
47
52
|
super({ allowHalfOpen: true, decodeStrings: false });
|
|
@@ -110,7 +115,17 @@ class ClientHttp2Stream extends Duplex {
|
|
|
110
115
|
// measured v26.12.1).
|
|
111
116
|
this._pushed = false;
|
|
112
117
|
this.additionalHeaders = [];
|
|
113
|
-
|
|
118
|
+
// The response and trailer objects are built at the first read, not when
|
|
119
|
+
// the block arrives -- the getters below. The slots start undefined so the
|
|
120
|
+
// object shape is stable from construction.
|
|
121
|
+
this._respFields = undefined;
|
|
122
|
+
this._respFcount = 0;
|
|
123
|
+
this._respSensitive = undefined;
|
|
124
|
+
this._responseObject = undefined;
|
|
125
|
+
this._trailerFields = undefined;
|
|
126
|
+
this._trailerFcount = 0;
|
|
127
|
+
this._trailerSensitive = undefined;
|
|
128
|
+
this._trailersObject = undefined;
|
|
114
129
|
// Set by session.request() when the application passed waitForTrailers: the last
|
|
115
130
|
// DATA on the wire does not carry END_STREAM; instead 'wantTrailers' fires and
|
|
116
131
|
// the app is expected to call sendTrailers() (or close()) to finish the message.
|
|
@@ -147,6 +162,43 @@ class ClientHttp2Stream extends Duplex {
|
|
|
147
162
|
return this._sentView;
|
|
148
163
|
}
|
|
149
164
|
|
|
165
|
+
// node's client keeps no response object on the stream at all -- core.js
|
|
166
|
+
// emitStreamEvent passes the objects through the events only -- so these two
|
|
167
|
+
// are prototype getters over the raw field list: the object comes into being
|
|
168
|
+
// at the first read (an event argument when a listener exists, or the getter),
|
|
169
|
+
// and one build serves every later read. A body-only client pays no
|
|
170
|
+
// per-response object (OPTIMIZATION.md item 48). Object.keys(stream) lists
|
|
171
|
+
// neither, which is closer to node than the plain fields this replaces were.
|
|
172
|
+
get responseHeaders() {
|
|
173
|
+
if (this._responseObject !== undefined) {
|
|
174
|
+
return this._responseObject;
|
|
175
|
+
}
|
|
176
|
+
const fields = this._respFields;
|
|
177
|
+
if (fields === undefined) {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
const o = this.session._toObject(fields, this._respFcount, this._respSensitive);
|
|
181
|
+
this._respFields = undefined;
|
|
182
|
+
this._respSensitive = undefined;
|
|
183
|
+
this._responseObject = o;
|
|
184
|
+
return o;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
get trailers() {
|
|
188
|
+
if (this._trailersObject !== undefined) {
|
|
189
|
+
return this._trailersObject;
|
|
190
|
+
}
|
|
191
|
+
const fields = this._trailerFields;
|
|
192
|
+
if (fields === undefined) {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
const o = this.session._toObject(fields, this._trailerFcount, this._trailerSensitive);
|
|
196
|
+
this._trailerFields = undefined;
|
|
197
|
+
this._trailerSensitive = undefined;
|
|
198
|
+
this._trailersObject = o;
|
|
199
|
+
return o;
|
|
200
|
+
}
|
|
201
|
+
|
|
150
202
|
// ---- writable side ----------------------------------------------------------
|
|
151
203
|
// The application's bytes go to the session, which frames them as DATA inside
|
|
152
204
|
// the two windows. If there is no credit, `_pump` leaves `_pending` set and the
|
|
@@ -184,15 +236,19 @@ class ClientHttp2Stream extends Duplex {
|
|
|
184
236
|
_read() {}
|
|
185
237
|
|
|
186
238
|
// The session decoded a response (or informational / trailer) block for us.
|
|
187
|
-
//
|
|
188
|
-
// `set-cookie` as an array
|
|
189
|
-
//
|
|
190
|
-
|
|
191
|
-
|
|
239
|
+
// The field list arrives unbuilt: the null-prototype object with `:status`
|
|
240
|
+
// coerced to a number and `set-cookie` as an array is what the session's
|
|
241
|
+
// _toObject builds, and it is built here at the first read -- an event
|
|
242
|
+
// argument when a listener exists, or the responseHeaders/trailers getter.
|
|
243
|
+
// A body-only client (no 'response' subscription, no getter read) pays no
|
|
244
|
+
// per-response object at all; the paired marginal is OPTIMIZATION.md item 48.
|
|
245
|
+
_onHeaders(status, fields, fcount, sensitive, end, flags) {
|
|
192
246
|
if (status !== undefined && status >= 100 && status < 200 && !this.responseEmitted) {
|
|
193
247
|
// 1xx informational (including the 100 Continue that node surfaces as
|
|
194
|
-
// 'continue'):
|
|
195
|
-
//
|
|
248
|
+
// 'continue'): built eagerly -- the blocks are rare and the one object
|
|
249
|
+
// feeds the two events and the additionalHeaders field. The 100-Continue
|
|
250
|
+
// translation node does is a 'headers' whose :status is 100.
|
|
251
|
+
const headers = this.session._toObject(fields, fcount, sensitive);
|
|
196
252
|
this.additionalHeaders.push(headers);
|
|
197
253
|
// node's inbound emit carries the frame flags as the second argument
|
|
198
254
|
// (`emit(stream, event, obj, flags, headers)`, internal/http2/core.js), which is
|
|
@@ -203,17 +259,26 @@ class ClientHttp2Stream extends Duplex {
|
|
|
203
259
|
}
|
|
204
260
|
} else if (!this.responseEmitted) {
|
|
205
261
|
this.responseEmitted = true;
|
|
206
|
-
this.responseHeaders = headers;
|
|
207
262
|
this.sentStatus = status;
|
|
263
|
+
this._respFields = fields;
|
|
264
|
+
this._respFcount = fcount;
|
|
265
|
+
this._respSensitive = sensitive;
|
|
208
266
|
// node passes the raw HEADERS frame flags as the second argument and a
|
|
209
267
|
// rawHeaders array as the third (core.js: emit(stream, event, obj, flags,
|
|
210
268
|
// headers)); the rawHeaders array is deliberately not built -- one flat
|
|
211
269
|
// array per response is a cost no h2lean consumer has asked for.
|
|
212
|
-
this.
|
|
270
|
+
const event = this._pushed ? 'push' : 'response';
|
|
271
|
+
if (this.listenerCount(event) > 0) {
|
|
272
|
+
this.emit(event, this.responseHeaders, flags);
|
|
273
|
+
}
|
|
213
274
|
} else {
|
|
214
275
|
// A second final-status block after the response is the trailer block.
|
|
215
|
-
this.
|
|
216
|
-
this.
|
|
276
|
+
this._trailerFields = fields;
|
|
277
|
+
this._trailerFcount = fcount;
|
|
278
|
+
this._trailerSensitive = sensitive;
|
|
279
|
+
if (this.listenerCount('trailers') > 0) {
|
|
280
|
+
this.emit('trailers', this.trailers);
|
|
281
|
+
}
|
|
217
282
|
}
|
|
218
283
|
if (end) {
|
|
219
284
|
this._endRead();
|
package/lib/fast-tick.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
// A batched replacement for process.nextTick, built like lib/fast-timer.js is
|
|
4
|
-
// built: one module, one pump shared by every session in the
|
|
5
|
-
// callback order as the contract. The
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
4
|
+
// built: one module, one class, one pump shared by every session in the
|
|
5
|
+
// process, and the callback order as the contract. The module exports a
|
|
6
|
+
// process-wide instance plus the class itself, so the pump can be lifted into
|
|
7
|
+
// a standalone module by moving the file; sessions never construct their own,
|
|
8
|
+
// because per-instance pumps would put a tick entry back on every server --
|
|
9
|
+
// the cost this exists to remove. The application opts in per server or
|
|
10
|
+
// client with `batchNextTick: true` (default false = node's own
|
|
11
|
+
// process.nextTick); H2LEAN_BATCH_TICKS=1 turns it on wherever the option is
|
|
12
|
+
// left undefined, so the whole suite can run under the pump and pin its event
|
|
13
|
+
// orders.
|
|
9
14
|
//
|
|
10
15
|
// What it changes: instead of one tick-queue entry per callback, callbacks
|
|
11
16
|
// land in a flat array and ONE real nextTick -- the pump -- drains them, so a
|
|
@@ -14,7 +19,19 @@
|
|
|
14
19
|
// isolated while the server-measured marginal of one merged tick is
|
|
15
20
|
// 0.03-0.05 us (OPTIMIZATION.md item 46, the pump's paired numbers in item
|
|
16
21
|
// 47), which is why this removes ALL of a session's entries rather than
|
|
17
|
-
// merging two.
|
|
22
|
+
// merging two. The queues are cursor-filled preallocated stores that reset
|
|
23
|
+
// instead of right-trimming (`length = 0` makes the next fill re-grow the
|
|
24
|
+
// store from scratch on V8, item 38) -- bench/micro/tick-queue-shape.js puts
|
|
25
|
+
// the trimmed shape at 27 ns/schedule against 12 for the cursor store at the
|
|
26
|
+
// 2-entries-per-turn size a quiet connection produces, and shows the class
|
|
27
|
+
// method layer costing nothing over the flat cursor. The scheduler itself is
|
|
28
|
+
// a constructor closure rather than a bound method -- the reason is at the
|
|
29
|
+
// closure. The literal FlatList API
|
|
30
|
+
// (one push() per item) measured slower than the pair write, and its
|
|
31
|
+
// setImmediate shrink would rebuild a store that can already hold the next
|
|
32
|
+
// generation's entries -- so the technique is re-implemented here with a
|
|
33
|
+
// release-when-idle rule (an oversized store is replaced only after its
|
|
34
|
+
// generation has drained), not by requiring lib/flat-list.js.
|
|
18
35
|
//
|
|
19
36
|
// What it keeps: FIFO order exactly as node's single queue gives it --
|
|
20
37
|
// including callbacks scheduled while the pump drains, which land in the next
|
|
@@ -32,9 +49,18 @@
|
|
|
32
49
|
// when it was scheduled, so an application reading AsyncLocalStorage inside
|
|
33
50
|
// a deferred 'finish' handler needs the default mode.
|
|
34
51
|
//
|
|
52
|
+
// The callback shape is (fn, arg), the shape every h2lean site schedules
|
|
53
|
+
// through: one argument, no per-callback closure or argument array, which is
|
|
54
|
+
// the allocation the entry price is made of. A site that needs more
|
|
55
|
+
// arguments -- the pushStream callback, the error paths -- wraps them in a
|
|
56
|
+
// closure at that site, the way the rare paths always have. A variadic
|
|
57
|
+
// schedule(callback, ...args) would allocate an args array per call and give
|
|
58
|
+
// back what the pump removes.
|
|
59
|
+
//
|
|
35
60
|
// On an exception inside the pump, the throwing pair is dropped and the
|
|
36
|
-
// callbacks after it are carried into the next generation
|
|
37
|
-
//
|
|
61
|
+
// callbacks after it are carried into the next generation -- ahead of
|
|
62
|
+
// whatever the aborted drain scheduled there, the order node's tail-of-the-
|
|
63
|
+
// queue semantics give them. Measured on a live node: an exception escaping a
|
|
38
64
|
// tick aborts that turn's drain -- the ticks behind it stay in the queue and
|
|
39
65
|
// run at the next tick phase only if the event loop gets there (a pending
|
|
40
66
|
// tick queue alone does not start a turn, and without an uncaughtException
|
|
@@ -44,10 +70,6 @@
|
|
|
44
70
|
// this._tick = fastTick.resolve(opts); // schedule | process.nextTick
|
|
45
71
|
// this._tick(fn, arg); // (fn, arg) shape, like nextTick
|
|
46
72
|
|
|
47
|
-
let ready = []; // callbacks awaiting the next pump run: [fn, arg, fn, arg, ...]
|
|
48
|
-
let idle = []; // the recycled array of the generation just drained
|
|
49
|
-
let scheduled = false; // a pump entry exists in the real tick queue
|
|
50
|
-
|
|
51
73
|
// Read at resolve() time, not at module load, so a process can turn the mode
|
|
52
74
|
// on after the library is required (the suite gate runs the whole tree under
|
|
53
75
|
// the pump this way). One env getter per session construction, never per tick.
|
|
@@ -55,61 +77,116 @@ function envForced() {
|
|
|
55
77
|
return process.env.H2LEAN_BATCH_TICKS === '1';
|
|
56
78
|
}
|
|
57
79
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
//
|
|
62
|
-
//
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
//
|
|
76
|
-
//
|
|
77
|
-
//
|
|
78
|
-
//
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const rest = list.slice(i + 2);
|
|
84
|
-
for (let k = rest.length - 1; k >= 0; k--) {
|
|
85
|
-
ready.unshift(rest[k]);
|
|
80
|
+
class FastTick {
|
|
81
|
+
// 64 slots = 32 pairs per generation before the store grows; at saturation
|
|
82
|
+
// the generations reach a few dozen entries, at low load they hold one or
|
|
83
|
+
// two -- the microbench's losing size, which is exactly what the cursor
|
|
84
|
+
// store fixes.
|
|
85
|
+
constructor(capacity) {
|
|
86
|
+
this.capacity = capacity;
|
|
87
|
+
this.ready = new Array(capacity); // [fn, arg, fn, arg, ...] up to readyCount
|
|
88
|
+
this.readyCount = 0;
|
|
89
|
+
this.idle = new Array(capacity); // the drained store, empty between turns
|
|
90
|
+
this.scheduled = false; // a pump entry exists in the real tick queue
|
|
91
|
+
// The reference the sessions hold: one closure per process, never per
|
|
92
|
+
// schedule. Sessions must not mix routes -- a pump generation reordering
|
|
93
|
+
// against its own native entries is the incoherence resolve() exists to
|
|
94
|
+
// prevent -- so the resolved reference is copied through streams and the
|
|
95
|
+
// compat objects unchanged. A closure rather than a bound method: one
|
|
96
|
+
// call frame instead of two, and a raw call-shape probe prices the bound
|
|
97
|
+
// wrapper at ~7 ns against ~5 for the closure. Paired rounds of this tree
|
|
98
|
+
// against the shipped flat pump, both under the pump, put the whole
|
|
99
|
+
// conversion within noise on all three server arms -- the shape claim is
|
|
100
|
+
// the probe's, not the server's.
|
|
101
|
+
this.schedule = (fn, arg) => {
|
|
102
|
+
const c = this.readyCount;
|
|
103
|
+
if (c + 2 > this.ready.length) {
|
|
104
|
+
this._grow(c);
|
|
86
105
|
}
|
|
106
|
+
const store = this.ready;
|
|
107
|
+
store[c] = fn;
|
|
108
|
+
store[c + 1] = arg;
|
|
109
|
+
this.readyCount = c + 2;
|
|
110
|
+
if (!this.scheduled) {
|
|
111
|
+
this.scheduled = true;
|
|
112
|
+
process.nextTick(this._pumpBound);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
this._pumpBound = this.pump.bind(this);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
_grow(count) {
|
|
119
|
+
// Rare: a generation larger than the preallocation. The store is replaced
|
|
120
|
+
// under the caller with the live entries carried over; the oversized size
|
|
121
|
+
// is given back when the store finishes draining (see pump's finally).
|
|
122
|
+
const old = this.ready;
|
|
123
|
+
const bigger = new Array(old.length * 2);
|
|
124
|
+
for (let i = 0; i < count; i++) {
|
|
125
|
+
bigger[i] = old[i];
|
|
87
126
|
}
|
|
88
|
-
|
|
89
|
-
scheduled = true;
|
|
90
|
-
process.nextTick(pump);
|
|
91
|
-
}
|
|
92
|
-
throw err;
|
|
93
|
-
} finally {
|
|
94
|
-
list.length = 0;
|
|
127
|
+
this.ready = bigger;
|
|
95
128
|
}
|
|
96
|
-
}
|
|
97
129
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
130
|
+
pump() {
|
|
131
|
+
this.scheduled = false;
|
|
132
|
+
const items = this.ready;
|
|
133
|
+
const count = this.readyCount;
|
|
134
|
+
// Re-entrant schedules during the drain push into `this.ready` -- the
|
|
135
|
+
// next generation -- and queue the next pump, which is node's
|
|
136
|
+
// tail-of-the-queue semantics: a callback scheduled while tick A runs
|
|
137
|
+
// executes after the ticks that were queued before A did.
|
|
138
|
+
this.ready = this.idle;
|
|
139
|
+
this.readyCount = 0;
|
|
140
|
+
this.idle = items;
|
|
141
|
+
let i = 0;
|
|
142
|
+
try {
|
|
143
|
+
while (i < count) {
|
|
144
|
+
items[i](items[i + 1]);
|
|
145
|
+
i += 2;
|
|
146
|
+
}
|
|
147
|
+
} catch (err) {
|
|
148
|
+
// The ticks behind the throwing one stay queued on node too -- the
|
|
149
|
+
// drain aborts, the leftovers run at the next tick phase, if the loop
|
|
150
|
+
// gets there. Carry ours to the FRONT of the next generation, ahead of
|
|
151
|
+
// whatever the aborted drain scheduled there, and make sure a pump is
|
|
152
|
+
// queued. The failed pair is gone the way node's throwing tick is gone
|
|
153
|
+
// -- carrying it too re-runs the thrower at the head of every later
|
|
154
|
+
// generation and starves the survivors.
|
|
155
|
+
const rest = count - (i + 2);
|
|
156
|
+
if (rest > 0) {
|
|
157
|
+
const ahead = this.readyCount;
|
|
158
|
+
if (ahead > 0) {
|
|
159
|
+
this.ready.copyWithin(rest, 0, ahead);
|
|
160
|
+
}
|
|
161
|
+
for (let k = 0; k < rest; k++) {
|
|
162
|
+
this.ready[k] = items[i + 2 + k];
|
|
163
|
+
}
|
|
164
|
+
this.readyCount = ahead + rest;
|
|
165
|
+
}
|
|
166
|
+
if (!this.scheduled && this.readyCount !== 0) {
|
|
167
|
+
this.scheduled = true;
|
|
168
|
+
process.nextTick(this._pumpBound);
|
|
169
|
+
}
|
|
170
|
+
throw err;
|
|
171
|
+
} finally {
|
|
172
|
+
// The drained store is idle: empty in the cursor sense (readyCount of
|
|
173
|
+
// the other store is 0), and if one generation grew it past the
|
|
174
|
+
// preallocation, the size is released now -- the same
|
|
175
|
+
// one-oversized-generation-does-not-pin-it rule FlatList applies to
|
|
176
|
+
// header blocks (lib/flat-list.js), at the point where the store is
|
|
177
|
+
// provably empty.
|
|
178
|
+
if (items.length > this.capacity) {
|
|
179
|
+
this.idle = new Array(this.capacity);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
103
182
|
}
|
|
104
|
-
}
|
|
105
183
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// itself is one instance for the whole process: sessions in batch mode share
|
|
110
|
-
// it, sessions in the default mode never meet it.
|
|
111
|
-
function resolve(opts) {
|
|
112
|
-
return ((opts && opts.batchNextTick) ?? envForced()) ? schedule : process.nextTick;
|
|
184
|
+
resolve(opts) {
|
|
185
|
+
return ((opts && opts.batchNextTick) ?? envForced()) ? this.schedule : process.nextTick;
|
|
186
|
+
}
|
|
113
187
|
}
|
|
114
188
|
|
|
115
|
-
|
|
189
|
+
// One pump for the whole process; the class is exported beside it so the
|
|
190
|
+
// module can be lifted out as-is.
|
|
191
|
+
module.exports = new FastTick(64);
|
|
192
|
+
module.exports.FastTick = FastTick;
|
package/package.json
CHANGED