@amritk/nish-aarch64-linux 0.44.0 → 0.46.0

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.
Files changed (119) hide show
  1. package/INSTALL.md +3 -1
  2. package/bin/nish +0 -0
  3. package/docs/AI.md +18 -16
  4. package/package.json +1 -1
  5. package/runtime/core/numbers.mjs +3 -1
  6. package/runtime/core/strings.mjs +90 -38
  7. package/runtime/core.d.ts +8 -0
  8. package/runtime/core.mjs +16 -0
  9. package/runtime/nish.d.ts +88 -5
  10. package/runtime/nish.h +10 -0
  11. package/runtime/nish.mjs +5 -0
  12. package/runtime/runtime-host.c +15 -0
  13. package/runtime/runtime-net.c +223 -4
  14. package/runtime/runtime-os.c +15 -1
  15. package/runtime/runtime-uring.c +291 -31
  16. package/runtime/sqlite/nish-sqlite.c +301 -128
  17. package/std/README.md +15 -15
  18. package/std/array.ts +10 -2
  19. package/std/bytes.ts +239 -26
  20. package/std/cli-args.ts +1072 -606
  21. package/std/cli-flags.ts +180 -98
  22. package/std/cli.ts +21 -13
  23. package/std/collections.ts +245 -127
  24. package/std/compress/checksum.ts +32 -37
  25. package/std/compress/deflate.ts +1102 -542
  26. package/std/compress/gzip.ts +290 -145
  27. package/std/compress/zlib.ts +57 -21
  28. package/std/crypto/aes.ts +153 -75
  29. package/std/crypto/base64url.ts +84 -61
  30. package/std/crypto/bcrypt-batch.ts +164 -90
  31. package/std/crypto/bcrypt.ts +185 -107
  32. package/std/crypto/chacha20poly1305.ts +84 -68
  33. package/std/crypto/ct.ts +9 -7
  34. package/std/crypto/ed25519.ts +64 -27
  35. package/std/crypto/hkdf.ts +105 -63
  36. package/std/crypto/hmac.ts +95 -89
  37. package/std/crypto/jwt.ts +248 -126
  38. package/std/crypto/p256.ts +758 -444
  39. package/std/crypto/p384.ts +20 -13
  40. package/std/crypto/pki-roots.ts +95 -38
  41. package/std/crypto/pki.ts +164 -60
  42. package/std/crypto/rsa.ts +266 -223
  43. package/std/crypto/scram.ts +3 -4
  44. package/std/crypto/sha1.ts +107 -65
  45. package/std/crypto/sha256.ts +502 -422
  46. package/std/crypto/sha512.ts +204 -135
  47. package/std/crypto/x509-random.ts +9 -3
  48. package/std/crypto/x509.ts +331 -171
  49. package/std/json-equal.ts +35 -19
  50. package/std/json-format.ts +215 -108
  51. package/std/json-number.ts +126 -72
  52. package/std/json-pointer.ts +42 -13
  53. package/std/json-tokenizer.ts +498 -307
  54. package/std/json-typed.ts +115 -36
  55. package/std/json-value.ts +341 -152
  56. package/std/json.ts +300 -158
  57. package/std/net/buffer-pool.ts +17 -7
  58. package/std/net/dns-config.ts +427 -236
  59. package/std/net/dns-message.ts +114 -47
  60. package/std/net/dns-resolver.ts +736 -237
  61. package/std/net/dns.ts +205 -52
  62. package/std/net/fetch.ts +320 -184
  63. package/std/net/grpc.ts +948 -0
  64. package/std/net/handler.ts +1315 -222
  65. package/std/net/hpack.ts +395 -180
  66. package/std/net/http-compress.ts +502 -65
  67. package/std/net/http-fields.ts +85 -49
  68. package/std/net/http-server.ts +1764 -602
  69. package/std/net/http1-client-codec.ts +372 -161
  70. package/std/net/http1-codec.ts +665 -331
  71. package/std/net/http1-server.ts +660 -278
  72. package/std/net/http2-cleartext.ts +165 -95
  73. package/std/net/http2-frame.ts +232 -89
  74. package/std/net/http2-tls.ts +42 -12
  75. package/std/net/http2.ts +327 -101
  76. package/std/net/http3-frame.ts +68 -33
  77. package/std/net/http3-server.ts +647 -192
  78. package/std/net/http3.ts +667 -189
  79. package/std/net/oidc.ts +63 -24
  80. package/std/net/outbound.ts +207 -142
  81. package/std/net/postgres-async.ts +1165 -443
  82. package/std/net/postgres-carrier.ts +575 -372
  83. package/std/net/postgres.ts +1459 -559
  84. package/std/net/prefork.ts +254 -98
  85. package/std/net/protobuf.ts +1017 -0
  86. package/std/net/qpack.ts +143 -92
  87. package/std/net/quic-conn-ack.ts +50 -18
  88. package/std/net/quic-conn-cid.ts +66 -26
  89. package/std/net/quic-conn-params.ts +13 -7
  90. package/std/net/quic-datagram.ts +90 -27
  91. package/std/net/quic-frame.ts +225 -124
  92. package/std/net/quic-listener.ts +197 -89
  93. package/std/net/quic-packet.ts +289 -163
  94. package/std/net/quic-recovery.ts +279 -128
  95. package/std/net/quic-stream.ts +466 -167
  96. package/std/net/quic.ts +568 -257
  97. package/std/net/router.ts +450 -99
  98. package/std/net/serve.ts +651 -232
  99. package/std/net/static.ts +1458 -0
  100. package/std/net/tls/client.ts +140 -45
  101. package/std/net/tls/codec.ts +510 -260
  102. package/std/net/tls/record-client.ts +268 -76
  103. package/std/net/tls/record-server.ts +236 -79
  104. package/std/net/tls/record.ts +173 -47
  105. package/std/net/tls/schedule.ts +58 -39
  106. package/std/net/tls/ticket.ts +295 -103
  107. package/std/net/tls/verify.ts +1 -1
  108. package/std/net/tls-tcp.ts +218 -75
  109. package/std/net/tls.ts +260 -152
  110. package/std/net/upstream.ts +283 -86
  111. package/std/net/url.ts +277 -102
  112. package/std/net/websocket.ts +370 -186
  113. package/std/net/webtransport.ts +755 -436
  114. package/std/regex.ts +1659 -829
  115. package/std/sqlite.ts +171 -0
  116. package/std/sync/ring.ts +320 -156
  117. package/std/sync/shm.ts +53 -6
  118. package/std/text.ts +205 -124
  119. package/std/threads.ts +22 -2
@@ -221,11 +221,74 @@ export class PgCarrierLimits {
221
221
  watch: i32 = 0
222
222
  }
223
223
 
224
- // Fills free stack `a` highest first, so that the lowest is handed out first.
225
- const fillFree = (a: i32[]): void => {
226
- const n: i32 = toI32(a.length)
227
- for (let k: i32 = 0; k < n; k++) {
228
- a[k] = n - 1 - k
224
+ // One pool slot's socket and what the carrier knows of it, one object per
225
+ // slot so that a slot's number is checked against the table once and its
226
+ // fields read without a check each.
227
+ class PgCarrierSlot {
228
+ // When its connect times out, 0 for no deadline.
229
+ deadline: i64 = 0
230
+ // Its TLS client, once the server agreed to TLS.
231
+ tls: TlsRecordClient | null = null
232
+ // The slot's number in the pool.
233
+ readonly index: i32
234
+ // The socket, -1 for none.
235
+ fd: i32 = -1
236
+ // Whether its connect finished.
237
+ connected: boolean = false
238
+ // The events the socket is watched for, so that one unchanged is not
239
+ // asked for again.
240
+ interest: i32 = 0
241
+ // Whether a connection was ever made in it.
242
+ made: boolean = false
243
+ // The socket error that failed it.
244
+ error: i32 = 0
245
+ // Its TLS phase (`PG_CARRIER_TLS_*`), and how much of the SSLRequest has
246
+ // gone out.
247
+ tlsPhase: i32 = 0
248
+ tlsAsked: i32 = 0
249
+
250
+ constructor(index: i32) {
251
+ this.index = index
252
+ }
253
+ }
254
+
255
+ // One query number's state: its tag, request, pool ticket (-1 for a batched
256
+ // one), and whether the server failed it.
257
+ class PgCarrierQuery {
258
+ readonly index: i32
259
+ tag: i32 = -1
260
+ request: HttpRequest | null = null
261
+ ticket: i32 = -1
262
+ failed: boolean = false
263
+
264
+ constructor(index: i32) {
265
+ this.index = index
266
+ }
267
+ }
268
+
269
+ // One batched execution's state: its batcher, run (-1 before `take`), and
270
+ // whether its run was ended. `value` is what the ticket map holds for it,
271
+ // the query count plus its number, so that it is told apart from a query.
272
+ class PgCarrierExecution {
273
+ readonly value: i32
274
+ batcher: i32 = -1
275
+ run: i32 = -1
276
+ ended: boolean = false
277
+
278
+ constructor(value: i32) {
279
+ this.value = value
280
+ }
281
+ }
282
+
283
+ // An attached batcher, whether an execution of it is queued on the pool,
284
+ // and whether it stalled this turn with every run in flight.
285
+ class PgCarrierBatch {
286
+ readonly batcher: PgBatcher
287
+ queued: boolean = false
288
+ stalled: boolean = false
289
+
290
+ constructor(batcher: PgBatcher) {
291
+ this.batcher = batcher
229
292
  }
230
293
  }
231
294
 
@@ -258,50 +321,30 @@ export class PgCarrier {
258
321
  private readonly nonce: u8[]
259
322
  private readonly ready: i32[]
260
323
  private readonly timeoutNs: i64
261
- // Each pool slot's socket (-1 for none), whether its connect finished,
262
- // whether a connection was ever made in it, when its connect times out
263
- // (0 for no deadline), and the socket error that failed it.
264
- private readonly fds: i32[]
265
- private readonly connected: boolean[]
266
- // The events each slot's socket is watched for, so that one unchanged
267
- // is not asked for again.
268
- private readonly interest: i32[]
269
- private readonly made: boolean[]
270
- private readonly deadlines: i64[]
271
- private readonly errors: i32[]
324
+ // Each pool slot's socket and state, by slot number.
325
+ private readonly sockets: PgCarrierSlot[]
272
326
  // TLS (`PgConfig.tls`): the name checked and the roots it is checked
273
- // against, each slot's phase (`PG_CARRIER_TLS_*`), its client, how much of
274
- // the SSLRequest has gone out; the SSLRequest itself, the one byte its
275
- // answer is read into, and the buffer ciphertext is read through.
327
+ // against; the SSLRequest, the one byte its answer is read into, and the
328
+ // buffer ciphertext is read through.
276
329
  private readonly tlsName: string
277
330
  private readonly trust: TlsClientTrust | null
278
- private readonly tlsPhase: i32[]
279
- private readonly tlsConns: (TlsRecordClient | null)[]
280
- private readonly tlsAsked: i32[]
281
331
  private readonly sslRequest: u8[]
282
332
  private readonly tlsAnswer: u8[]
283
333
  private readonly tlsInput: u8[]
284
- // Each query's tag, request, pool ticket (-1 for a batched one), whether
285
- // the server failed it, and the stack of free ones.
286
- private readonly qTags: i32[]
287
- private readonly qRequests: (HttpRequest | null)[]
288
- private readonly qTickets: i32[]
289
- private readonly qFailed: boolean[]
290
- private readonly qFree: i32[]
291
- // Each batched execution's batcher, run (-1 before `take`), whether its run
292
- // was ended, and the stack of free ones.
293
- private readonly eBatchers: i32[]
294
- private readonly eRuns: i32[]
295
- private readonly eEnded: boolean[]
296
- private readonly eFree: i32[]
334
+ // Each query by number, and the stack of free ones, lowest on top.
335
+ private readonly held: PgCarrierQuery[]
336
+ private readonly qFree: PgCarrierQuery[]
337
+ // Each batched execution by number, and the stack of free ones.
338
+ private readonly executions: PgCarrierExecution[]
339
+ private readonly eFree: PgCarrierExecution[]
297
340
  // Pool tickets to what they carry: a query, or `queries` plus an
298
- // execution. Open addressing, -1 for an empty slot.
341
+ // execution. Open addressing, -1 for an empty slot, probed in `u32` so
342
+ // that the step wraps under the mask.
299
343
  private readonly mapKeys: i32[]
300
344
  private readonly mapValues: i32[]
301
- private readonly batchers: PgBatcher[]
302
- private readonly batchQueued: boolean[]
303
- private readonly batchStalled: boolean[]
345
+ private readonly batchers: PgCarrierBatch[]
304
346
  private conn: PgConnection | null = null
347
+ private readonly mapMask: u32
305
348
  /** The query the last event is about, as `run` answered it; -1 for none. */
306
349
  query: i32 = -1
307
350
  /** The tag `query` was run with. */
@@ -313,9 +356,6 @@ export class PgCarrier {
313
356
  private readonly tlsMode: i32
314
357
  private readonly watchId: i32
315
358
  private readonly loop: i32
316
- private qFreeCount: i32 = 0
317
- private eFreeCount: i32 = 0
318
- private readonly mapMask: i32
319
359
  // What a call owes: the query being built, one dropped unsent, the query
320
360
  // whose final event was answered last, a run's events, and requests shed
321
361
  // from a batcher whose execution was lost.
@@ -389,52 +429,70 @@ export class PgCarrier {
389
429
  this.path = target.path
390
430
  this.nonce = new Array<u8>(PG_CARRIER_NONCE_BYTES)
391
431
  this.timeoutNs = toI64(limits.connectTimeoutMs) * toI64(1000000)
432
+ // Every table is filled by `push` to the size the caps give, so that no
433
+ // length is computed.
392
434
  const size: i32 = pool.size
393
- this.ready = new Array<i32>(2 * size)
394
- this.fds = new Array<i32>(size)
395
- this.connected = new Array<boolean>(size)
396
- this.interest = new Array<i32>(size)
397
- this.made = new Array<boolean>(size)
398
- this.deadlines = new Array<i64>(size)
399
- this.errors = new Array<i32>(size)
400
- this.tlsPhase = new Array<i32>(size)
401
- this.tlsConns = new Array<TlsRecordClient | null>(size)
402
- this.tlsAsked = new Array<i32>(size)
435
+ const ready: i32[] = []
436
+ const sockets: PgCarrierSlot[] = []
437
+ for (let s: i32 = 0; s < size; s++) {
438
+ ready.push(0)
439
+ ready.push(0)
440
+ sockets.push(new PgCarrierSlot(s))
441
+ }
442
+ this.ready = ready
443
+ this.sockets = sockets
403
444
  this.sslRequest = pgSslRequest()
404
445
  this.tlsAnswer = new Array<u8>(1)
405
- this.tlsInput = new Array<u8>(
406
- this.tlsMode !== PG_TLS_DISABLE ? PG_CARRIER_TLS_READ_BYTES : PG_CARRIER_ZERO
407
- )
408
- for (let s: i32 = 0; s < size && s < toI32(this.fds.length); s++) {
409
- this.fds[s] = -1
410
- }
411
- this.qTags = new Array<i32>(queries)
412
- this.qRequests = new Array<HttpRequest | null>(queries)
413
- this.qTickets = new Array<i32>(queries)
414
- this.qFailed = new Array<boolean>(queries)
415
- this.qFree = new Array<i32>(queries)
416
- fillFree(this.qFree)
417
- this.qFreeCount = queries
418
- this.eBatchers = new Array<i32>(executions)
419
- this.eRuns = new Array<i32>(executions)
420
- this.eEnded = new Array<boolean>(executions)
421
- this.eFree = new Array<i32>(executions)
422
- fillFree(this.eFree)
423
- this.eFreeCount = executions
424
- // At least twice the tickets ever held, a power of two for the mask.
425
- let mapSize: i32 = 2
426
- while (mapSize / 2 < queries + executions) {
427
- mapSize *= 2
446
+ this.tlsInput =
447
+ this.tlsMode !== PG_TLS_DISABLE
448
+ ? new Array<u8>(PG_CARRIER_TLS_READ_BYTES)
449
+ : new Array<u8>(PG_CARRIER_ZERO)
450
+ // The free stacks are filled highest first, so that the lowest number is
451
+ // handed out first.
452
+ const held: PgCarrierQuery[] = []
453
+ const qFree: PgCarrierQuery[] = []
454
+ for (let q: i32 = 0; q < queries; q++) {
455
+ held.push(new PgCarrierQuery(q))
456
+ }
457
+ let k: i32 = toI32(held.length)
458
+ while (k > 0) {
459
+ k -= 1
460
+ if (k < toI32(held.length)) {
461
+ qFree.push(held[k])
462
+ }
428
463
  }
429
- this.mapKeys = new Array<i32>(mapSize)
430
- this.mapValues = new Array<i32>(mapSize)
431
- for (let i: i32 = 0; i < mapSize && i < toI32(this.mapKeys.length); i++) {
432
- this.mapKeys[i] = -1
464
+ this.held = held
465
+ this.qFree = qFree
466
+ const runs: PgCarrierExecution[] = []
467
+ const eFree: PgCarrierExecution[] = []
468
+ for (let e: i32 = 0; e < executions; e++) {
469
+ runs.push(new PgCarrierExecution(toI32(toU32(queries) + toU32(e))))
470
+ }
471
+ k = toI32(runs.length)
472
+ while (k > 0) {
473
+ k -= 1
474
+ if (k < toI32(runs.length)) {
475
+ eFree.push(runs[k])
476
+ }
433
477
  }
478
+ this.executions = runs
479
+ this.eFree = eFree
480
+ // At least twice the tickets ever held, a power of two for the mask.
481
+ const tickets: u32 = toU32(queries) + toU32(executions)
482
+ let mapSize: u32 = 2
483
+ while (mapSize / 2 < tickets) {
484
+ mapSize = mapSize << 1
485
+ }
486
+ const keys: i32[] = []
487
+ const values: i32[] = []
488
+ for (let i: u32 = 0; i < mapSize; i++) {
489
+ keys.push(-1)
490
+ values.push(-1)
491
+ }
492
+ this.mapKeys = keys
493
+ this.mapValues = values
434
494
  this.mapMask = mapSize - 1
435
495
  this.batchers = []
436
- this.batchQueued = new Array<boolean>(PG_CARRIER_MAX_BATCHERS)
437
- this.batchStalled = new Array<boolean>(PG_CARRIER_MAX_BATCHERS)
438
496
  this.loop = pollCreate()
439
497
  if (this.loop < 0) {
440
498
  panic("nish/net/postgres-carrier: PgCarrier could not create its poll set")
@@ -460,7 +518,7 @@ export class PgCarrier {
460
518
  "nish/net/postgres-carrier: PgCarrier.attach past 8 batchers, after the pool connected, on a NUL in the name or SQL, or past 256 statements"
461
519
  )
462
520
  }
463
- this.batchers.push(batcher)
521
+ this.batchers.push(new PgCarrierBatch(batcher))
464
522
  return id
465
523
  }
466
524
 
@@ -478,18 +536,18 @@ export class PgCarrier {
478
536
  * final event has passed, or `PG_CARRIER_FULL`.
479
537
  */
480
538
  run(req: HttpRequest | null, tag: i32): i32 {
481
- const q: i32 = this.take(req, tag)
482
- if (q < 0) {
539
+ const held: PgCarrierQuery | null = this.take(req, tag)
540
+ if (held === null) {
483
541
  return PG_CARRIER_FULL
484
542
  }
485
543
  const ticket: i32 = this.pool.request()
486
544
  if (ticket < 0) {
487
- this.free(q)
545
+ this.free(held)
488
546
  return PG_CARRIER_FULL
489
547
  }
490
- this.qTickets[q] = ticket
491
- this.mapPut(ticket, q)
492
- return q
548
+ held.ticket = ticket
549
+ this.mapPut(ticket, held.index)
550
+ return held.index
493
551
  }
494
552
 
495
553
  /**
@@ -506,16 +564,16 @@ export class PgCarrier {
506
564
  * waiting, the batcher's queue is full or the carrier is closed.
507
565
  */
508
566
  runInt(req: HttpRequest | null, batcher: i32, key: i64, tag: i32): i32 {
509
- const b: PgBatcher = this.batcher(batcher)
510
- const q: i32 = this.take(req, tag)
511
- if (q < 0) {
567
+ const batch: PgCarrierBatch = this.attached(batcher)
568
+ const held: PgCarrierQuery | null = this.take(req, tag)
569
+ if (held === null) {
512
570
  return PG_CARRIER_FULL
513
571
  }
514
- if (!b.addInt(key, q)) {
515
- this.free(q)
572
+ if (!batch.batcher.addInt(key, held.index)) {
573
+ this.free(held)
516
574
  return PG_CARRIER_FULL
517
575
  }
518
- return q
576
+ return held.index
519
577
  }
520
578
 
521
579
  /**
@@ -537,16 +595,16 @@ export class PgCarrier {
537
595
  * key too long for one execution.
538
596
  */
539
597
  runText(req: HttpRequest | null, batcher: i32, bytes: u8[], start: i32, length: i32, tag: i32): i32 {
540
- const b: PgBatcher = this.batcher(batcher)
541
- const q: i32 = this.take(req, tag)
542
- if (q < 0) {
598
+ const batch: PgCarrierBatch = this.attached(batcher)
599
+ const held: PgCarrierQuery | null = this.take(req, tag)
600
+ if (held === null) {
543
601
  return PG_CARRIER_FULL
544
602
  }
545
- if (!b.addText(bytes, start, length, q)) {
546
- this.free(q)
603
+ if (!batch.batcher.addText(bytes, start, length, held.index)) {
604
+ this.free(held)
547
605
  return PG_CARRIER_FULL
548
606
  }
549
- return q
607
+ return held.index
550
608
  }
551
609
 
552
610
  /**
@@ -557,8 +615,8 @@ export class PgCarrier {
557
615
  * the `next()` after the query's final event.
558
616
  */
559
617
  request(): HttpRequest {
560
- const q: i32 = this.query
561
- const req: HttpRequest | null = q >= 0 && q < toI32(this.qRequests.length) ? this.qRequests[q] : null
618
+ const held: PgCarrierQuery | null = this.queryAt(this.query)
619
+ const req: HttpRequest | null = held !== null ? held.request : null
562
620
  if (req === null) {
563
621
  panic("nish/net/postgres-carrier: PgCarrier.request with no request for the query")
564
622
  }
@@ -592,8 +650,8 @@ export class PgCarrier {
592
650
  * nothing was written and the query fails.
593
651
  */
594
652
  send(): boolean {
595
- const q: i32 = this.building
596
- if (q < 0 || q >= toI32(this.qTickets.length)) {
653
+ const held: PgCarrierQuery | null = this.queryAt(this.building)
654
+ if (held === null) {
597
655
  panic("nish/net/postgres-carrier: PgCarrier.send with no PG_CARRIER_BUILD open")
598
656
  }
599
657
  this.building = -1
@@ -603,7 +661,7 @@ export class PgCarrier {
603
661
  const conn: PgConnection = this.connection()
604
662
  if (conn.bindCancel()) {
605
663
  // The group may hold the connection's prepares, which still go; the query does not.
606
- this.qFailed[q] = true
664
+ held.failed = true
607
665
  if (this.pool.send()) {
608
666
  return true
609
667
  }
@@ -611,8 +669,8 @@ export class PgCarrier {
611
669
  // Nothing was written: a live connection dropped the query, and a failed
612
670
  // one reports it lost with the rest.
613
671
  if (conn.failure === PG_ERR_NONE) {
614
- this.mapRemove(this.qTickets[q])
615
- this.dropped = q
672
+ this.mapRemove(held.ticket)
673
+ this.dropped = held.index
616
674
  }
617
675
  return false
618
676
  }
@@ -638,16 +696,21 @@ export class PgCarrier {
638
696
  panic("nish/net/postgres-carrier: PgCarrier.next before send() ended the PG_CARRIER_BUILD")
639
697
  }
640
698
  if (this.finished >= 0) {
641
- this.free(this.finished)
699
+ const done: PgCarrierQuery | null = this.queryAt(this.finished)
700
+ if (done !== null) {
701
+ this.free(done)
702
+ }
642
703
  this.finished = -1
643
704
  }
644
705
  while (true) {
645
706
  if (this.dropped >= 0) {
646
- const q: i32 = this.dropped
707
+ const held: PgCarrierQuery | null = this.queryAt(this.dropped)
647
708
  this.dropped = -1
648
- this.failure = PG_ERR_NONE
649
- this.socketError = 0
650
- return this.final(q, PG_CARRIER_FAILED)
709
+ if (held !== null) {
710
+ this.failure = PG_ERR_NONE
711
+ this.socketError = 0
712
+ return this.final(held, PG_CARRIER_FAILED)
713
+ }
651
714
  }
652
715
  if (this.drainRun >= 0) {
653
716
  const event: i32 = this.drain()
@@ -655,11 +718,13 @@ export class PgCarrier {
655
718
  return event
656
719
  }
657
720
  }
658
- if (this.shedLeft > 0) {
659
- this.shedLeft -= 1
660
- const q: i32 = this.batcher(this.shedBatcher).shed()
661
- if (q >= 0) {
662
- return this.final(q, PG_CARRIER_FAILED)
721
+ const left: i32 = this.shedLeft
722
+ if (left > 0) {
723
+ this.shedLeft = left - 1
724
+ const batch: PgCarrierBatch | null = this.batcher(this.shedBatcher)
725
+ const held: PgCarrierQuery | null = batch !== null ? this.queryAt(batch.batcher.shed()) : null
726
+ if (held !== null) {
727
+ return this.final(held, PG_CARRIER_FAILED)
663
728
  }
664
729
  this.shedLeft = 0
665
730
  }
@@ -717,20 +782,22 @@ export class PgCarrier {
717
782
  return
718
783
  }
719
784
  this.closed = true
720
- for (let s: i32 = 0; s < toI32(this.fds.length); s++) {
721
- const fd: i32 = this.fds[s]
785
+ const sockets: PgCarrierSlot[] = this.sockets
786
+ for (let s: i32 = 0; s < toI32(sockets.length); s++) {
787
+ const slot: PgCarrierSlot = sockets[s]
788
+ const fd: i32 = slot.fd
722
789
  if (fd >= 0) {
723
- const conn: PgConnection = this.pool.connection(s)
724
- const tls: TlsRecordClient | null = this.tlsConns[s]
725
- if (this.tlsPhase[s] === PG_CARRIER_TLS_OFF || this.tlsPhase[s] === PG_CARRIER_TLS_OPEN) {
790
+ const conn: PgConnection = this.pool.connection(slot.index)
791
+ const tls: TlsRecordClient | null = slot.tls
792
+ if (slot.tlsPhase === PG_CARRIER_TLS_OFF || slot.tlsPhase === PG_CARRIER_TLS_OPEN) {
726
793
  conn.terminate()
727
- this.flushSlot(s, conn, fd)
794
+ this.flushSlot(slot, conn, fd)
728
795
  }
729
796
  if (tls !== null) {
730
797
  tls.close()
731
798
  this.flushTls(tls, fd)
732
799
  }
733
- this.dropSocket(s, fd)
800
+ this.dropSocket(slot, fd)
734
801
  }
735
802
  }
736
803
  netClose(this.loop)
@@ -740,51 +807,67 @@ export class PgCarrier {
740
807
  // connects, and fails the connections past their connect timeout.
741
808
  private poll(): void {
742
809
  if (this.handler.fired(this.watchId)) {
743
- const n: i32 = pollWait(this.loop, this.ready, toI32(0))
744
- for (let k: i32 = 0; k < n && 2 * k + 1 < toI32(this.ready.length); k++) {
745
- const s: i32 = this.ready[2 * k]
746
- const bits: i32 = this.ready[2 * k + 1]
747
- if (s >= 0 && s < toI32(this.fds.length) && this.fds[s] >= 0) {
748
- this.service(s, bits)
810
+ const ready: i32[] = this.ready
811
+ const n: i32 = pollWait(this.loop, ready, toI32(0))
812
+ // `ready` holds a slot and its event bits for each of the `n` sockets.
813
+ // In `u32`, which wraps by definition, and checked against `ready`
814
+ // before each read.
815
+ let k: u32 = 0
816
+ let left: i32 = n
817
+ while (left > 0) {
818
+ left -= 1
819
+ const i: i32 = toI32(k)
820
+ k += 2
821
+ if (i < 0 || i >= toI32(ready.length) - 1) {
822
+ break
823
+ }
824
+ const at: i32 = ready[i]
825
+ const bits: i32 = ready[i + 1]
826
+ const slot: PgCarrierSlot | null = this.slotAt(at)
827
+ if (slot !== null && slot.fd >= 0) {
828
+ this.service(slot, bits)
749
829
  }
750
830
  }
751
831
  }
752
832
  let now: i64 = 0
753
- for (let s: i32 = 0; s < toI32(this.deadlines.length); s++) {
754
- const deadline: i64 = this.deadlines[s]
833
+ const sockets: PgCarrierSlot[] = this.sockets
834
+ for (let s: i32 = 0; s < toI32(sockets.length); s++) {
835
+ const slot: PgCarrierSlot = sockets[s]
836
+ const deadline: i64 = slot.deadline
755
837
  if (deadline > 0) {
756
838
  if (now === 0) {
757
839
  now = monotonicNanos()
758
840
  }
759
841
  // A connection the server already failed keeps that failure.
760
- if (now >= deadline && this.pool.connection(s).failure === PG_ERR_NONE) {
761
- this.fail(s, PG_CARRIER_TIMED_OUT)
842
+ if (now >= deadline && this.pool.connection(slot.index).failure === PG_ERR_NONE) {
843
+ this.fail(slot, PG_CARRIER_TIMED_OUT)
762
844
  }
763
845
  }
764
846
  }
765
- for (let b: i32 = 0; b < toI32(this.batchStalled.length); b++) {
766
- this.batchStalled[b] = false
847
+ const batchers: PgCarrierBatch[] = this.batchers
848
+ for (let b: i32 = 0; b < toI32(batchers.length); b++) {
849
+ batchers[b].stalled = false
767
850
  }
768
851
  }
769
852
 
770
853
  // One ready socket: a connect finished, or input to read.
771
- private service(s: i32, bits: i32): void {
772
- const conn: PgConnection = this.pool.connection(s)
854
+ private service(slot: PgCarrierSlot, bits: i32): void {
855
+ const conn: PgConnection = this.pool.connection(slot.index)
773
856
  if (conn.failure !== PG_ERR_NONE) {
774
857
  return
775
858
  }
776
- const fd: i32 = this.fds[s]
777
- if (!this.connected[s]) {
859
+ const fd: i32 = slot.fd
860
+ if (!slot.connected) {
778
861
  const result: i32 = connectResult(fd)
779
862
  if (result !== 0) {
780
- this.fail(s, result)
863
+ this.fail(slot, result)
781
864
  return
782
865
  }
783
- this.connected[s] = true
866
+ slot.connected = true
784
867
  }
785
- if (this.tlsPhase[s] !== PG_CARRIER_TLS_OFF) {
868
+ if (slot.tlsPhase !== PG_CARRIER_TLS_OFF) {
786
869
  if ((bits & PG_CARRIER_READ_BITS) !== 0) {
787
- this.serviceTls(s, fd, conn)
870
+ this.serviceTls(slot, fd, conn)
788
871
  }
789
872
  return
790
873
  }
@@ -794,24 +877,24 @@ export class PgCarrier {
794
877
  if (got > 0) {
795
878
  conn.received(got)
796
879
  } else if (got !== PG_CARRIER_AGAIN) {
797
- this.fail(s, got)
880
+ this.fail(slot, got)
798
881
  }
799
882
  }
800
883
  }
801
884
 
802
- // Ends slot `s`'s connection for `error`: its events then say so.
803
- private fail(s: i32, error: i32): void {
804
- this.pool.connection(s).closed()
805
- this.errors[s] = error
806
- this.deadlines[s] = 0
885
+ // Ends `slot`'s connection for `error`: its events then say so.
886
+ private fail(slot: PgCarrierSlot, error: i32): void {
887
+ this.pool.connection(slot.index).closed()
888
+ slot.error = error
889
+ slot.deadline = 0
807
890
  }
808
891
 
809
- // Reads slot `s`'s socket under TLS: the one byte answering the
892
+ // Reads `slot`'s socket under TLS: the one byte answering the
810
893
  // SSLRequest, else ciphertext for the record layer, whose plaintext is
811
894
  // then moved into the connection's input.
812
- private serviceTls(s: i32, fd: i32, conn: PgConnection): void {
813
- if (this.tlsPhase[s] === PG_CARRIER_TLS_ASK) {
814
- if (this.tlsAsked[s] < toI32(this.sslRequest.length)) {
895
+ private serviceTls(slot: PgCarrierSlot, fd: i32, conn: PgConnection): void {
896
+ if (slot.tlsPhase === PG_CARRIER_TLS_ASK) {
897
+ if (slot.tlsAsked < toI32(this.sslRequest.length)) {
815
898
  // The answer is read only once the whole SSLRequest is out.
816
899
  return
817
900
  }
@@ -820,47 +903,49 @@ export class PgCarrier {
820
903
  // never read, and bytes after `S` go to the record layer as the start
821
904
  // of the handshake, which a TLS server's ServerHello must be, so
822
905
  // anything else fails it.
823
- const got: i32 = netRead(fd, this.tlsAnswer, 0, 1)
906
+ const answer: u8[] = this.tlsAnswer
907
+ const got: i32 = netRead(fd, answer, 0, 1)
824
908
  if (got === PG_CARRIER_AGAIN) {
825
909
  return
826
910
  }
827
911
  if (got <= 0) {
828
- this.fail(s, got)
829
- } else if (toI32(this.tlsAnswer[0]) !== PG_CARRIER_TLS_YES) {
830
- this.failTls(s, PG_CARRIER_ZERO)
912
+ this.fail(slot, got)
913
+ } else if (answer.length > 0 && toI32(answer[0]) === PG_CARRIER_TLS_YES) {
914
+ slot.tls = pgCarrierTlsStart(this.tlsName)
915
+ slot.tlsPhase = PG_CARRIER_TLS_HANDSHAKE
831
916
  } else {
832
- this.tlsConns[s] = pgCarrierTlsStart(this.tlsName)
833
- this.tlsPhase[s] = PG_CARRIER_TLS_HANDSHAKE
917
+ this.failTls(slot, PG_CARRIER_ZERO)
834
918
  }
835
919
  return
836
920
  }
837
- const tls: TlsRecordClient | null = this.tlsConns[s]
921
+ const tls: TlsRecordClient | null = slot.tls
838
922
  if (tls === null) {
839
923
  return
840
924
  }
925
+ const input: u8[] = this.tlsInput
841
926
  let room: i32 = tls.space()
842
- if (room > toI32(this.tlsInput.length)) {
843
- room = toI32(this.tlsInput.length)
927
+ if (room > toI32(input.length)) {
928
+ room = toI32(input.length)
844
929
  }
845
930
  if (room > 0) {
846
- const got: i32 = netRead(fd, this.tlsInput, 0, room)
931
+ const got: i32 = netRead(fd, input, 0, room)
847
932
  if (got > 0) {
848
- tls.receive(this.tlsInput, PG_CARRIER_ZERO, got)
933
+ tls.receive(input, PG_CARRIER_ZERO, got)
849
934
  } else if (got === 0) {
850
935
  tls.end()
851
936
  } else if (got !== PG_CARRIER_AGAIN) {
852
937
  tls.abort()
853
- this.fail(s, got)
938
+ this.fail(slot, got)
854
939
  return
855
940
  }
856
941
  }
857
- this.advanceTls(s, tls, conn)
942
+ this.advanceTls(slot, tls, conn)
858
943
  }
859
944
 
860
- // Gives slot `s`'s handshake the verdict it waits on, notes it open, and
945
+ // Gives `slot`'s handshake the verdict it waits on, notes it open, and
861
946
  // moves what plaintext `conn` has room for into its input: whether any
862
947
  // moved, so that `next()` looks at the slot again.
863
- private advanceTls(s: i32, tls: TlsRecordClient, conn: PgConnection): boolean {
948
+ private advanceTls(slot: PgCarrierSlot, tls: TlsRecordClient, conn: PgConnection): boolean {
864
949
  if ((tls.interest() & TLS_RECORD_AUTH) !== 0) {
865
950
  const trust: TlsClientTrust | null = this.trust
866
951
  if (trust !== null) {
@@ -877,13 +962,13 @@ export class PgCarrier {
877
962
  }
878
963
  }
879
964
  if (tls.state === TLS_RECORD_STATE_FAILED) {
880
- this.failTls(s, tls.failure())
965
+ this.failTls(slot, tls.failure())
881
966
  return false
882
967
  }
883
- if (this.tlsPhase[s] === PG_CARRIER_TLS_HANDSHAKE && tls.state === TLS_RECORD_STATE_OPEN) {
884
- this.tlsPhase[s] = PG_CARRIER_TLS_OPEN
968
+ if (slot.tlsPhase === PG_CARRIER_TLS_HANDSHAKE && tls.state === TLS_RECORD_STATE_OPEN) {
969
+ slot.tlsPhase = PG_CARRIER_TLS_OPEN
885
970
  }
886
- if (this.tlsPhase[s] !== PG_CARRIER_TLS_OPEN) {
971
+ if (slot.tlsPhase !== PG_CARRIER_TLS_OPEN) {
887
972
  return false
888
973
  }
889
974
  let moved: boolean = false
@@ -895,11 +980,11 @@ export class PgCarrier {
895
980
  moved = true
896
981
  } else if (got === 0) {
897
982
  // The server's close_notify: the stream ended cleanly.
898
- this.fail(s, PG_CARRIER_ZERO)
983
+ this.fail(slot, PG_CARRIER_ZERO)
899
984
  return true
900
985
  } else {
901
986
  if (tls.state === TLS_RECORD_STATE_FAILED) {
902
- this.failTls(s, tls.failure())
987
+ this.failTls(slot, tls.failure())
903
988
  return true
904
989
  }
905
990
  return moved
@@ -909,15 +994,15 @@ export class PgCarrier {
909
994
  return moved
910
995
  }
911
996
 
912
- // Fails slot `s` with `PG_ERR_TLS`: `alert` the TLS alert behind it, 0
997
+ // Fails `slot` with `PG_ERR_TLS`: `alert` the TLS alert behind it, 0
913
998
  // for a refused SSLRequest. One write sends the alert a refusal sealed,
914
999
  // when the socket has room for it.
915
- private failTls(s: i32, alert: i32): void {
916
- this.pool.connection(s).tlsFailed()
917
- this.errors[s] = alert
918
- this.deadlines[s] = 0
919
- const tls: TlsRecordClient | null = this.tlsConns[s]
920
- const fd: i32 = this.fds[s]
1000
+ private failTls(slot: PgCarrierSlot, alert: i32): void {
1001
+ this.pool.connection(slot.index).tlsFailed()
1002
+ slot.error = alert
1003
+ slot.deadline = 0
1004
+ const tls: TlsRecordClient | null = slot.tls
1005
+ const fd: i32 = slot.fd
921
1006
  if (tls !== null && fd >= 0) {
922
1007
  this.flushTls(tls, fd)
923
1008
  }
@@ -925,95 +1010,119 @@ export class PgCarrier {
925
1010
 
926
1011
  // The events of every slot's connection, resuming at `at`.
927
1012
  private slots(): i32 {
928
- while (this.at < toI32(this.made.length)) {
1013
+ const sockets: PgCarrierSlot[] = this.sockets
1014
+ while (true) {
929
1015
  const s: i32 = this.at
930
- if (!this.made[s]) {
931
- this.at += 1
1016
+ if (s < 0 || s >= toI32(sockets.length)) {
1017
+ return PG_CARRIER_NONE
1018
+ }
1019
+ const slot: PgCarrierSlot = sockets[s]
1020
+ if (!slot.made) {
1021
+ this.at = s + 1
932
1022
  continue
933
1023
  }
934
1024
  const event: i32 = this.pool.next(s)
935
1025
  if (event === PG_NEED_MORE) {
936
- this.at += 1
1026
+ this.at = s + 1
937
1027
  continue
938
1028
  }
939
- const routed: i32 = this.route(s, event)
1029
+ const routed: i32 = this.route(slot, event)
940
1030
  if (routed !== PG_CARRIER_NONE) {
941
1031
  return routed
942
1032
  }
943
1033
  }
944
- return PG_CARRIER_NONE
945
1034
  }
946
1035
 
947
- // One pool event from slot `s`, as the carrier's own; `PG_CARRIER_NONE`
1036
+ // One pool event from `slot`, as the carrier's own; `PG_CARRIER_NONE`
948
1037
  // for one it keeps, `PG_CARRIER_AGAIN_PASS` when a run's events are owed.
949
- private route(s: i32, event: i32): i32 {
950
- const conn: PgConnection = this.pool.connection(s)
1038
+ private route(slot: PgCarrierSlot, event: i32): i32 {
1039
+ const conn: PgConnection = this.pool.connection(slot.index)
951
1040
  if (event === PG_READY) {
952
- this.deadlines[s] = 0
1041
+ slot.deadline = 0
953
1042
  return PG_CARRIER_NONE
954
1043
  }
955
1044
  if (event === PG_FAILED) {
956
- const fd: i32 = this.fds[s]
1045
+ const fd: i32 = slot.fd
957
1046
  if (fd >= 0) {
958
- this.dropSocket(s, fd)
1047
+ this.dropSocket(slot, fd)
959
1048
  }
960
- this.deadlines[s] = 0
1049
+ slot.deadline = 0
961
1050
  return PG_CARRIER_NONE
962
1051
  }
963
1052
  const ticket: i32 = this.pool.ticket
964
1053
  const v: i32 = ticket < 0 ? -1 : this.mapGet(ticket)
965
- if (v < 0) {
1054
+ const held: PgCarrierQuery[] = this.held
1055
+ const queries: i32 = toI32(held.length)
1056
+ if (v >= 0 && v < queries) {
1057
+ this.conn = conn
1058
+ return this.routePlain(slot, held[v], ticket, event, conn)
1059
+ }
1060
+ const exec: PgCarrierExecution | null = this.executionFor(v)
1061
+ if (exec === null) {
966
1062
  // A notice, or the event of a query no longer held.
967
1063
  return PG_CARRIER_NONE
968
1064
  }
969
1065
  this.conn = conn
970
- const queries: i32 = toI32(this.qTags.length)
971
- if (v < queries) {
972
- return this.routePlain(s, v, ticket, event, conn)
973
- }
974
- return this.routeBatch(s, v - queries, ticket, event, conn)
1066
+ return this.routeBatch(slot, exec, ticket, event, conn)
975
1067
  }
976
1068
 
977
- private routePlain(s: i32, q: i32, ticket: i32, event: i32, conn: PgConnection): i32 {
1069
+ private routePlain(
1070
+ slot: PgCarrierSlot,
1071
+ held: PgCarrierQuery,
1072
+ ticket: i32,
1073
+ event: i32,
1074
+ conn: PgConnection
1075
+ ): i32 {
978
1076
  if (event === PG_ROW || event === PG_COMPLETE) {
979
- this.announce(q)
1077
+ this.announce(held)
980
1078
  return event === PG_ROW ? PG_CARRIER_ROW : PG_CARRIER_COMPLETE
981
1079
  }
982
1080
  if (event === PG_ERROR) {
983
- this.qFailed[q] = true
984
- this.announce(q)
1081
+ held.failed = true
1082
+ this.announce(held)
985
1083
  return PG_CARRIER_ERROR
986
1084
  }
987
1085
  if (event === PG_GROUP_DONE) {
988
1086
  this.mapRemove(ticket)
989
- const failed: boolean = this.qFailed[q] || conn.groupFailed
1087
+ const failed: boolean = held.failed || conn.groupFailed
990
1088
  this.failure = PG_ERR_NONE
991
1089
  this.socketError = 0
992
- return this.final(q, failed ? PG_CARRIER_FAILED : PG_CARRIER_DONE)
1090
+ return this.final(held, failed ? PG_CARRIER_FAILED : PG_CARRIER_DONE)
993
1091
  }
994
1092
  if (event === PG_LOST) {
995
1093
  this.mapRemove(ticket)
996
1094
  this.failure = conn.failure
997
- this.socketError = this.errors[s]
998
- return this.final(q, PG_CARRIER_FAILED)
1095
+ this.socketError = slot.error
1096
+ return this.final(held, PG_CARRIER_FAILED)
999
1097
  }
1000
1098
  return PG_CARRIER_NONE
1001
1099
  }
1002
1100
 
1003
- private routeBatch(s: i32, e: i32, ticket: i32, event: i32, conn: PgConnection): i32 {
1004
- const id: i32 = this.eBatchers[e]
1005
- const b: PgBatcher = this.batcher(id)
1006
- const r: i32 = this.eRuns[e]
1101
+ private routeBatch(
1102
+ slot: PgCarrierSlot,
1103
+ exec: PgCarrierExecution,
1104
+ ticket: i32,
1105
+ event: i32,
1106
+ conn: PgConnection
1107
+ ): i32 {
1108
+ const id: i32 = exec.batcher
1109
+ const batch: PgCarrierBatch | null = this.batcher(id)
1110
+ if (batch === null) {
1111
+ // Not reached: no batcher is ever detached, so this releases nothing.
1112
+ return PG_CARRIER_NONE
1113
+ }
1114
+ const b: PgBatcher = batch.batcher
1115
+ const r: i32 = exec.run
1007
1116
  if (event === PG_LOST && r < 0) {
1008
1117
  // Lost before a connection took it: its requests would wait on a
1009
1118
  // server that cannot be reached, so one execution's worth fail now.
1010
1119
  this.mapRemove(ticket)
1011
- this.freeExecution(e)
1012
- this.batchQueued[id] = false
1120
+ this.freeExecution(exec)
1121
+ batch.queued = false
1013
1122
  this.shedBatcher = id
1014
1123
  this.shedLeft = b.maxRequests
1015
1124
  this.failure = conn.failure
1016
- this.socketError = this.errors[s]
1125
+ this.socketError = slot.error
1017
1126
  return PG_CARRIER_AGAIN_PASS
1018
1127
  }
1019
1128
  if (r < 0) {
@@ -1028,7 +1137,7 @@ export class PgCarrier {
1028
1137
  }
1029
1138
  if (event === PG_COMPLETE) {
1030
1139
  b.end(r)
1031
- this.eEnded[e] = true
1140
+ exec.ended = true
1032
1141
  this.startDrain(id, r, PG_ERR_NONE, toI32(0))
1033
1142
  return PG_CARRIER_AGAIN_PASS
1034
1143
  }
@@ -1036,12 +1145,12 @@ export class PgCarrier {
1036
1145
  const lost: boolean = event === PG_LOST
1037
1146
  if (event !== PG_ERROR) {
1038
1147
  this.mapRemove(ticket)
1039
- this.freeExecution(e)
1148
+ this.freeExecution(exec)
1040
1149
  }
1041
- if (!this.eEnded[e]) {
1150
+ if (!exec.ended) {
1042
1151
  b.fail(r)
1043
- this.eEnded[e] = true
1044
- this.startDrain(id, r, lost ? conn.failure : PG_ERR_NONE, lost ? this.errors[s] : toI32(0))
1152
+ exec.ended = true
1153
+ this.startDrain(id, r, lost ? conn.failure : PG_ERR_NONE, lost ? slot.error : toI32(0))
1045
1154
  return PG_CARRIER_AGAIN_PASS
1046
1155
  }
1047
1156
  }
@@ -1057,51 +1166,58 @@ export class PgCarrier {
1057
1166
 
1058
1167
  // The next of the events a run owes, as the carrier's own.
1059
1168
  private drain(): i32 {
1060
- const b: PgBatcher = this.batcher(this.drainBatcher)
1169
+ const batch: PgCarrierBatch | null = this.batcher(this.drainBatcher)
1170
+ if (batch === null) {
1171
+ this.drainRun = -1
1172
+ return PG_CARRIER_NONE
1173
+ }
1174
+ const b: PgBatcher = batch.batcher
1061
1175
  const event: i32 = b.next(this.drainRun)
1062
- const q: i32 = b.tag
1063
- if (event === PG_BATCH_NONE || q < 0 || q >= toI32(this.qTags.length)) {
1176
+ const held: PgCarrierQuery | null = this.queryAt(b.tag)
1177
+ if (event === PG_BATCH_NONE || held === null) {
1064
1178
  this.drainRun = -1
1065
1179
  return PG_CARRIER_NONE
1066
1180
  }
1067
1181
  if (event === PG_BATCH_ROW) {
1068
- this.announce(q)
1182
+ this.announce(held)
1069
1183
  return PG_CARRIER_ROW
1070
1184
  }
1071
1185
  if (event === PG_BATCH_FAILED) {
1072
1186
  this.failure = this.drainFailure
1073
1187
  this.socketError = this.drainError
1074
- return this.final(q, PG_CARRIER_FAILED)
1188
+ return this.final(held, PG_CARRIER_FAILED)
1075
1189
  }
1076
1190
  this.failure = PG_ERR_NONE
1077
1191
  this.socketError = 0
1078
- return this.final(q, event === PG_BATCH_DONE ? PG_CARRIER_DONE : PG_CARRIER_MISSING)
1192
+ return this.final(held, event === PG_BATCH_DONE ? PG_CARRIER_DONE : PG_CARRIER_MISSING)
1079
1193
  }
1080
1194
 
1081
1195
  // Queues an execution for each batcher with requests waiting, then hands
1082
1196
  // waiting tickets to connections: a plain query's is the program's to
1083
1197
  // build, an execution's is bound here.
1084
1198
  private dispatch(): i32 {
1085
- for (let id: i32 = 0; id < toI32(this.batchers.length) && id < PG_CARRIER_MAX_BATCHERS; id++) {
1086
- if (
1087
- this.batchQueued[id] ||
1088
- this.batchStalled[id] ||
1089
- this.batchers[id].waiting() === 0 ||
1090
- this.eFreeCount === 0
1091
- ) {
1199
+ const batchers: PgCarrierBatch[] = this.batchers
1200
+ const eFree: PgCarrierExecution[] = this.eFree
1201
+ for (let id: i32 = 0; id < toI32(batchers.length); id++) {
1202
+ const batch: PgCarrierBatch = batchers[id]
1203
+ if (batch.queued || batch.stalled || batch.batcher.waiting() === 0) {
1092
1204
  continue
1093
1205
  }
1094
- const ticket: i32 = this.pool.request()
1095
- if (ticket < 0) {
1096
- break
1206
+ const lastExec: i32 = toI32(eFree.length) - 1
1207
+ if (lastExec >= 0 && eFree.length > 0) {
1208
+ const exec: PgCarrierExecution = eFree[lastExec]
1209
+ eFree.pop()
1210
+ const ticket: i32 = this.pool.request()
1211
+ if (ticket < 0) {
1212
+ eFree.push(exec)
1213
+ break
1214
+ }
1215
+ exec.batcher = id
1216
+ exec.run = -1
1217
+ exec.ended = false
1218
+ this.mapPut(ticket, exec.value)
1219
+ batch.queued = true
1097
1220
  }
1098
- this.eFreeCount -= 1
1099
- const e: i32 = this.eFree[this.eFreeCount]
1100
- this.eBatchers[e] = id
1101
- this.eRuns[e] = -1
1102
- this.eEnded[e] = false
1103
- this.mapPut(ticket, toI32(this.qTags.length) + e)
1104
- this.batchQueued[id] = true
1105
1221
  }
1106
1222
  const slot: i32 = this.pool.dispatch()
1107
1223
  if (slot < 0) {
@@ -1110,39 +1226,40 @@ export class PgCarrier {
1110
1226
  const conn: PgConnection = this.pool.connection(slot)
1111
1227
  const ticket: i32 = this.pool.ticket
1112
1228
  const v: i32 = this.mapGet(ticket)
1113
- const queries: i32 = toI32(this.qTags.length)
1229
+ const held: PgCarrierQuery[] = this.held
1230
+ const queries: i32 = toI32(held.length)
1114
1231
  if (v >= 0 && v < queries) {
1115
1232
  this.conn = conn
1116
1233
  this.building = v
1117
- this.announce(v)
1234
+ this.announce(held[v])
1118
1235
  return PG_CARRIER_BUILD
1119
1236
  }
1120
- if (v < 0) {
1237
+ const exec: PgCarrierExecution | null = this.executionFor(v)
1238
+ const batch: PgCarrierBatch | null = exec !== null ? this.batcher(exec.batcher) : null
1239
+ if (exec === null || batch === null) {
1121
1240
  this.pool.send()
1122
1241
  return PG_CARRIER_AGAIN_PASS
1123
1242
  }
1124
- const e: i32 = v - queries
1125
- const id: i32 = this.eBatchers[e]
1126
- const b: PgBatcher = this.batcher(id)
1127
- this.batchQueued[id] = false
1243
+ const b: PgBatcher = batch.batcher
1244
+ batch.queued = false
1128
1245
  const r: i32 = b.take(conn)
1129
1246
  if (r < 0) {
1130
1247
  // Every run is in flight, or the connection had no room for the bind:
1131
1248
  // the requests stay queued for the next turn.
1132
- this.batchStalled[id] = true
1249
+ batch.stalled = true
1133
1250
  this.pool.send()
1134
1251
  if (conn.failure === PG_ERR_NONE) {
1135
1252
  this.mapRemove(ticket)
1136
- this.freeExecution(e)
1253
+ this.freeExecution(exec)
1137
1254
  }
1138
1255
  return PG_CARRIER_AGAIN_PASS
1139
1256
  }
1140
- this.eRuns[e] = r
1257
+ exec.run = r
1141
1258
  if (!this.pool.send() && conn.failure === PG_ERR_NONE) {
1142
1259
  this.mapRemove(ticket)
1143
- this.freeExecution(e)
1260
+ this.freeExecution(exec)
1144
1261
  b.fail(r)
1145
- this.startDrain(id, r, PG_ERR_NONE, toI32(0))
1262
+ this.startDrain(exec.batcher, r, PG_ERR_NONE, toI32(0))
1146
1263
  }
1147
1264
  return PG_CARRIER_AGAIN_PASS
1148
1265
  }
@@ -1152,42 +1269,47 @@ export class PgCarrier {
1152
1269
  // True when a socket failed and its events are owed.
1153
1270
  private connectAndFlush(connect: boolean): boolean {
1154
1271
  let failed: boolean = false
1272
+ const sockets: PgCarrierSlot[] = this.sockets
1155
1273
  if (connect) {
1156
- let slot: i32 = this.pool.wanted()
1157
- const deadline: i64 = slot >= 0 ? monotonicNanos() + this.timeoutNs : toI64(0)
1158
- while (slot >= 0 && slot < toI32(this.fds.length)) {
1274
+ let want: i32 = this.pool.wanted()
1275
+ // In `u64`, which wraps by definition: a monotonic clock is centuries
1276
+ // short of the top.
1277
+ const deadline: i64 = want >= 0 ? toI64(toU64(monotonicNanos()) + toU64(this.timeoutNs)) : toI64(0)
1278
+ while (want >= 0 && want < toI32(sockets.length)) {
1279
+ const slot: PgCarrierSlot = sockets[want]
1159
1280
  crypto.getRandomValues(this.nonce)
1160
- this.pool.connect(slot, this.nonce)
1161
- this.made[slot] = true
1162
- this.connected[slot] = false
1163
- this.interest[slot] = H1_WANT_WRITE
1164
- this.errors[slot] = 0
1165
- this.deadlines[slot] = deadline
1166
- this.tlsPhase[slot] = this.tlsMode !== PG_TLS_DISABLE ? PG_CARRIER_TLS_ASK : PG_CARRIER_TLS_OFF
1167
- this.tlsAsked[slot] = 0
1281
+ this.pool.connect(want, this.nonce)
1282
+ slot.made = true
1283
+ slot.connected = false
1284
+ slot.interest = H1_WANT_WRITE
1285
+ slot.error = 0
1286
+ slot.deadline = deadline
1287
+ slot.tlsPhase = this.tlsMode !== PG_TLS_DISABLE ? PG_CARRIER_TLS_ASK : PG_CARRIER_TLS_OFF
1288
+ slot.tlsAsked = 0
1168
1289
  const fd: i32 = this.unix ? unixConnect(this.path) : tcpConnect(this.address)
1169
- this.fds[slot] = fd
1290
+ slot.fd = fd
1170
1291
  if (fd < 0) {
1171
1292
  this.fail(slot, fd)
1172
1293
  failed = true
1173
- } else if (pollAdd(this.loop, fd, H1_WANT_WRITE, slot) < 0) {
1294
+ } else if (pollAdd(this.loop, fd, H1_WANT_WRITE, want) < 0) {
1174
1295
  this.fail(slot, toI32(-1))
1175
1296
  failed = true
1176
1297
  }
1177
- slot = this.pool.wanted()
1298
+ want = this.pool.wanted()
1178
1299
  }
1179
1300
  }
1180
- for (let s: i32 = 0; s < toI32(this.fds.length); s++) {
1181
- const fd: i32 = this.fds[s]
1182
- if (fd < 0 || !this.connected[s]) {
1301
+ for (let s: i32 = 0; s < toI32(sockets.length); s++) {
1302
+ const slot: PgCarrierSlot = sockets[s]
1303
+ const fd: i32 = slot.fd
1304
+ if (fd < 0 || !slot.connected) {
1183
1305
  continue
1184
1306
  }
1185
1307
  const conn: PgConnection = this.pool.connection(s)
1186
1308
  if (conn.failure !== PG_ERR_NONE) {
1187
1309
  continue
1188
1310
  }
1189
- const tls: TlsRecordClient | null = this.tlsConns[s]
1190
- if (tls !== null && this.advanceTls(s, tls, conn)) {
1311
+ const tls: TlsRecordClient | null = slot.tls
1312
+ if (tls !== null && this.advanceTls(slot, tls, conn)) {
1191
1313
  // Plaintext the record layer held, which no poll would report.
1192
1314
  failed = true
1193
1315
  }
@@ -1195,31 +1317,31 @@ export class PgCarrier {
1195
1317
  failed = true
1196
1318
  continue
1197
1319
  }
1198
- const written: i32 = this.flushSlot(s, conn, fd)
1320
+ const written: i32 = this.flushSlot(slot, conn, fd)
1199
1321
  if (written < 0) {
1200
1322
  if (tls !== null) {
1201
1323
  tls.abort()
1202
1324
  }
1203
- this.fail(s, written)
1325
+ this.fail(slot, written)
1204
1326
  failed = true
1205
1327
  continue
1206
1328
  }
1207
- const wants: i32 = this.pending(s, conn) ? H1_WANT_READ | H1_WANT_WRITE : H1_WANT_READ
1208
- if (wants !== this.interest[s]) {
1329
+ const wants: i32 = this.pending(slot, conn) ? H1_WANT_READ | H1_WANT_WRITE : H1_WANT_READ
1330
+ if (wants !== slot.interest) {
1209
1331
  pollModify(this.loop, fd, wants, s)
1210
- this.interest[s] = wants
1332
+ slot.interest = wants
1211
1333
  }
1212
1334
  }
1213
1335
  return failed
1214
1336
  }
1215
1337
 
1216
- // Whether slot `s` has bytes for the socket that it has not taken.
1217
- private pending(s: i32, conn: PgConnection): boolean {
1218
- const phase: i32 = this.tlsPhase[s]
1338
+ // Whether `slot` has bytes for the socket that it has not taken.
1339
+ private pending(slot: PgCarrierSlot, conn: PgConnection): boolean {
1340
+ const phase: i32 = slot.tlsPhase
1219
1341
  if (phase === PG_CARRIER_TLS_ASK) {
1220
- return this.tlsAsked[s] < toI32(this.sslRequest.length)
1342
+ return slot.tlsAsked < toI32(this.sslRequest.length)
1221
1343
  }
1222
- const tls: TlsRecordClient | null = this.tlsConns[s]
1344
+ const tls: TlsRecordClient | null = slot.tls
1223
1345
  if (tls !== null && tls.outputEnd > tls.outputStart) {
1224
1346
  return true
1225
1347
  }
@@ -1228,29 +1350,34 @@ export class PgCarrier {
1228
1350
  )
1229
1351
  }
1230
1352
 
1231
- // Writes what slot `s` owes its socket, as its TLS phase has it: the
1353
+ // Writes what `slot` owes its socket, as its TLS phase has it: the
1232
1354
  // SSLRequest, the handshake, `conn`'s bytes sealed, or `conn`'s bytes as
1233
1355
  // they are. The socket error when it fails, else 0.
1234
- private flushSlot(s: i32, conn: PgConnection, fd: i32): i32 {
1235
- const phase: i32 = this.tlsPhase[s]
1356
+ private flushSlot(slot: PgCarrierSlot, conn: PgConnection, fd: i32): i32 {
1357
+ const phase: i32 = slot.tlsPhase
1236
1358
  if (phase === PG_CARRIER_TLS_OFF) {
1237
1359
  return this.flush(conn, fd)
1238
1360
  }
1239
1361
  if (phase === PG_CARRIER_TLS_ASK) {
1240
1362
  const request: u8[] = this.sslRequest
1241
- while (this.tlsAsked[s] < toI32(request.length)) {
1242
- const n: i32 = netWrite(fd, request, this.tlsAsked[s], toI32(request.length) - this.tlsAsked[s])
1363
+ const length: i32 = toI32(request.length)
1364
+ let asked: i32 = slot.tlsAsked
1365
+ while (asked >= 0 && asked < length) {
1366
+ const n: i32 = netWrite(fd, request, asked, length - asked)
1243
1367
  if (n === PG_CARRIER_AGAIN) {
1244
1368
  return 0
1245
1369
  }
1246
1370
  if (n <= 0) {
1247
1371
  return n < 0 ? n : -1
1248
1372
  }
1249
- this.tlsAsked[s] += n
1373
+ // A write takes at most what it was given, so this is at most `length`.
1374
+ const sent: i64 = toI64(asked) + toI64(n)
1375
+ asked = sent < toI64(length) ? toI32(sent) : length
1376
+ slot.tlsAsked = asked
1250
1377
  }
1251
1378
  return 0
1252
1379
  }
1253
- const tls: TlsRecordClient | null = this.tlsConns[s]
1380
+ const tls: TlsRecordClient | null = slot.tls
1254
1381
  if (tls === null) {
1255
1382
  return 0
1256
1383
  }
@@ -1259,14 +1386,12 @@ export class PgCarrier {
1259
1386
  if (written !== 0 || tls.outputEnd > tls.outputStart) {
1260
1387
  return written
1261
1388
  }
1262
- if (phase !== PG_CARRIER_TLS_OPEN || conn.outputEnd() <= conn.outputStart()) {
1389
+ const start: i32 = conn.outputStart()
1390
+ const end: i32 = conn.outputEnd()
1391
+ if (phase !== PG_CARRIER_TLS_OPEN || start < 0 || end < 0 || end <= start) {
1263
1392
  return 0
1264
1393
  }
1265
- const sealed: i32 = tls.write(
1266
- conn.outputBuffer(),
1267
- conn.outputStart(),
1268
- conn.outputEnd() - conn.outputStart()
1269
- )
1394
+ const sealed: i32 = tls.write(conn.outputBuffer(), start, end - start)
1270
1395
  if (sealed <= 0) {
1271
1396
  return 0
1272
1397
  }
@@ -1277,8 +1402,13 @@ export class PgCarrier {
1277
1402
  // Writes the records `tls` has sealed until the socket takes no more; the
1278
1403
  // socket error when it fails, else 0.
1279
1404
  private flushTls(tls: TlsRecordClient, fd: i32): i32 {
1280
- while (tls.outputEnd > tls.outputStart) {
1281
- const n: i32 = netWrite(fd, tls.output, tls.outputStart, tls.outputEnd - tls.outputStart)
1405
+ while (true) {
1406
+ const start: i32 = tls.outputStart
1407
+ const end: i32 = tls.outputEnd
1408
+ if (start < 0 || end < 0 || end <= start) {
1409
+ return 0
1410
+ }
1411
+ const n: i32 = netWrite(fd, tls.output, start, end - start)
1282
1412
  if (n === PG_CARRIER_AGAIN) {
1283
1413
  return 0
1284
1414
  }
@@ -1287,19 +1417,18 @@ export class PgCarrier {
1287
1417
  }
1288
1418
  tls.consume(n)
1289
1419
  }
1290
- return 0
1291
1420
  }
1292
1421
 
1293
1422
  // Writes what `conn` queued until the socket takes no more; the socket
1294
1423
  // error when it fails, else 0.
1295
1424
  private flush(conn: PgConnection, fd: i32): i32 {
1296
- while (conn.outputEnd() > conn.outputStart()) {
1297
- const n: i32 = netWrite(
1298
- fd,
1299
- conn.outputBuffer(),
1300
- conn.outputStart(),
1301
- conn.outputEnd() - conn.outputStart()
1302
- )
1425
+ while (true) {
1426
+ const start: i32 = conn.outputStart()
1427
+ const end: i32 = conn.outputEnd()
1428
+ if (start < 0 || end < 0 || end <= start) {
1429
+ return 0
1430
+ }
1431
+ const n: i32 = netWrite(fd, conn.outputBuffer(), start, end - start)
1303
1432
  if (n === PG_CARRIER_AGAIN) {
1304
1433
  return 0
1305
1434
  }
@@ -1308,116 +1437,190 @@ export class PgCarrier {
1308
1437
  }
1309
1438
  conn.sent(n)
1310
1439
  }
1311
- return 0
1312
1440
  }
1313
1441
 
1314
- // Names query `q` as the one the event is about.
1315
- private announce(q: i32): void {
1316
- this.query = q
1317
- this.tag = this.qTags[q]
1442
+ // Names `held` as the query the event is about.
1443
+ private announce(held: PgCarrierQuery): void {
1444
+ this.query = held.index
1445
+ this.tag = held.tag
1318
1446
  }
1319
1447
 
1320
- // Takes slot `s`'s socket `fd` off the poll set and closes it.
1321
- private dropSocket(s: i32, fd: i32): void {
1448
+ // Takes `slot`'s socket `fd` off the poll set and closes it.
1449
+ private dropSocket(slot: PgCarrierSlot, fd: i32): void {
1322
1450
  pollRemove(this.loop, fd)
1323
1451
  netClose(fd)
1324
- this.fds[s] = -1
1325
- const tls: TlsRecordClient | null = this.tlsConns[s]
1452
+ slot.fd = -1
1453
+ const tls: TlsRecordClient | null = slot.tls
1326
1454
  if (tls !== null) {
1327
1455
  tls.wipe()
1328
- this.tlsConns[s] = null
1456
+ slot.tls = null
1329
1457
  }
1330
1458
  }
1331
1459
 
1332
- // Answers query `q`'s final `event`; the query is freed on the next call.
1333
- private final(q: i32, event: i32): i32 {
1334
- this.announce(q)
1335
- this.finished = q
1460
+ // Answers `held`'s final `event`; the query is freed on the next call.
1461
+ private final(held: PgCarrierQuery, event: i32): i32 {
1462
+ this.announce(held)
1463
+ this.finished = held.index
1336
1464
  return event
1337
1465
  }
1338
1466
 
1339
- private batcher(id: i32): PgBatcher {
1340
- if (id < 0 || id >= toI32(this.batchers.length)) {
1467
+ // The slot numbered `s`, or null for a number no slot has.
1468
+ private slotAt(s: i32): PgCarrierSlot | null {
1469
+ const sockets: PgCarrierSlot[] = this.sockets
1470
+ return s >= 0 && s < toI32(sockets.length) ? sockets[s] : null
1471
+ }
1472
+
1473
+ // The query numbered `q`, or null for a number no query has.
1474
+ private queryAt(q: i32): PgCarrierQuery | null {
1475
+ const held: PgCarrierQuery[] = this.held
1476
+ return q >= 0 && q < toI32(held.length) ? held[q] : null
1477
+ }
1478
+
1479
+ // The execution whose ticket-map value is `v` (the query count plus its
1480
+ // number), or null for a value no execution has.
1481
+ private executionFor(v: i32): PgCarrierExecution | null {
1482
+ const queries: i32 = toI32(this.held.length)
1483
+ const runs: PgCarrierExecution[] = this.executions
1484
+ const e: i32 = queries >= 0 && v >= queries && v >= 0 ? v - queries : -1
1485
+ return e >= 0 && e < toI32(runs.length) ? runs[e] : null
1486
+ }
1487
+
1488
+ // The batcher `attach` numbered `id`, or null for an id it never gave.
1489
+ private batcher(id: i32): PgCarrierBatch | null {
1490
+ const batchers: PgCarrierBatch[] = this.batchers
1491
+ return id >= 0 && id < toI32(batchers.length) ? batchers[id] : null
1492
+ }
1493
+
1494
+ // The batcher a `runInt` or `runText` caller names; panics on an id `attach`
1495
+ // never gave, the one contract those two keep. The frame paths use `batcher`.
1496
+ private attached(id: i32): PgCarrierBatch {
1497
+ const batch: PgCarrierBatch | null = this.batcher(id)
1498
+ if (batch === null) {
1341
1499
  panic("nish/net/postgres-carrier: PgCarrier batcher id out of range")
1342
1500
  }
1343
- return this.batchers[id]
1501
+ return batch
1344
1502
  }
1345
1503
 
1346
- // A free query for `req` and `tag`, or -1 when none is free or the carrier is closed.
1347
- private take(req: HttpRequest | null, tag: i32): i32 {
1348
- if (this.closed || this.qFreeCount === 0) {
1349
- return -1
1350
- }
1351
- this.qFreeCount -= 1
1352
- const q: i32 = this.qFree[this.qFreeCount]
1353
- this.qTags[q] = tag
1354
- this.qRequests[q] = req
1355
- this.qTickets[q] = -1
1356
- this.qFailed[q] = false
1357
- return q
1504
+ // A free query for `req` and `tag`, or null when none is free or the carrier is closed.
1505
+ private take(req: HttpRequest | null, tag: i32): PgCarrierQuery | null {
1506
+ const free: PgCarrierQuery[] = this.qFree
1507
+ if (this.closed || free.length === 0) {
1508
+ return null
1509
+ }
1510
+ const last: i32 = toI32(free.length) - 1
1511
+ if (last >= 0 && free.length > 0) {
1512
+ const held: PgCarrierQuery = free[last]
1513
+ free.pop()
1514
+ held.tag = tag
1515
+ held.request = req
1516
+ held.ticket = -1
1517
+ held.failed = false
1518
+ return held
1519
+ }
1520
+ return null
1358
1521
  }
1359
1522
 
1360
- private free(q: i32): void {
1361
- this.qRequests[q] = null
1362
- this.qFree[this.qFreeCount] = q
1363
- this.qFreeCount += 1
1523
+ private free(held: PgCarrierQuery): void {
1524
+ held.request = null
1525
+ this.qFree.push(held)
1364
1526
  }
1365
1527
 
1366
- private freeExecution(e: i32): void {
1367
- this.eFree[this.eFreeCount] = e
1368
- this.eFreeCount += 1
1528
+ private freeExecution(exec: PgCarrierExecution): void {
1529
+ this.eFree.push(exec)
1369
1530
  }
1370
1531
 
1532
+ // The map's size is twice the tickets ever held, so a probe always meets
1533
+ // an empty slot and every loop below ends. A slot index outside the
1534
+ // arrays cannot occur, since the mask is their length less one; the
1535
+ // guards on it take the path for a key not found.
1371
1536
  private mapPut(key: i32, value: i32): void {
1372
- const mask: i32 = this.mapMask
1373
- let i: i32 = key & mask
1374
- while (this.mapKeys[i] !== -1) {
1375
- i = (i + 1) & mask
1537
+ const keys: i32[] = this.mapKeys
1538
+ const values: i32[] = this.mapValues
1539
+ const mask: u32 = this.mapMask
1540
+ let b: u32 = toU32(key) & mask
1541
+ while (true) {
1542
+ const i: i32 = toI32(b)
1543
+ if (i < 0 || i >= toI32(keys.length)) {
1544
+ return
1545
+ }
1546
+ if (keys[i] === -1) {
1547
+ if (i < toI32(values.length)) {
1548
+ keys[i] = key
1549
+ values[i] = value
1550
+ }
1551
+ return
1552
+ }
1553
+ b = (b + 1) & mask
1376
1554
  }
1377
- this.mapKeys[i] = key
1378
- this.mapValues[i] = value
1379
1555
  }
1380
1556
 
1381
1557
  private mapGet(key: i32): i32 {
1382
- const mask: i32 = this.mapMask
1383
- let i: i32 = key & mask
1384
- while (this.mapKeys[i] !== -1) {
1385
- if (this.mapKeys[i] === key) {
1386
- return this.mapValues[i]
1558
+ const keys: i32[] = this.mapKeys
1559
+ const values: i32[] = this.mapValues
1560
+ const mask: u32 = this.mapMask
1561
+ let b: u32 = toU32(key) & mask
1562
+ while (true) {
1563
+ const i: i32 = toI32(b)
1564
+ if (i < 0 || i >= toI32(keys.length)) {
1565
+ return -1
1566
+ }
1567
+ const k: i32 = keys[i]
1568
+ if (k === key) {
1569
+ return i < toI32(values.length) ? values[i] : -1
1570
+ }
1571
+ if (k === -1) {
1572
+ return -1
1387
1573
  }
1388
- i = (i + 1) & mask
1574
+ b = (b + 1) & mask
1389
1575
  }
1390
- return -1
1391
1576
  }
1392
1577
 
1393
1578
  // Removes `key`, shifting back the entries after it that would no longer
1394
1579
  // be found past the hole, so that no tombstone is ever left.
1395
1580
  private mapRemove(key: i32): void {
1396
- const mask: i32 = this.mapMask
1397
- let i: i32 = key & mask
1398
- while (this.mapKeys[i] !== key) {
1399
- if (this.mapKeys[i] === -1) {
1581
+ const keys: i32[] = this.mapKeys
1582
+ const values: i32[] = this.mapValues
1583
+ const mask: u32 = this.mapMask
1584
+ let b: u32 = toU32(key) & mask
1585
+ let hole: i32 = -1
1586
+ while (true) {
1587
+ const i: i32 = toI32(b)
1588
+ if (i < 0 || i >= toI32(keys.length)) {
1400
1589
  return
1401
1590
  }
1402
- i = (i + 1) & mask
1591
+ const k: i32 = keys[i]
1592
+ if (k === key) {
1593
+ hole = i
1594
+ break
1595
+ }
1596
+ if (k === -1) {
1597
+ return
1598
+ }
1599
+ b = (b + 1) & mask
1403
1600
  }
1404
- let j: i32 = i
1601
+ let j: u32 = b
1405
1602
  while (true) {
1406
1603
  j = (j + 1) & mask
1407
- const k: i32 = this.mapKeys[j]
1604
+ const at: i32 = toI32(j)
1605
+ if (at < 0 || at >= toI32(keys.length) || at >= toI32(values.length)) {
1606
+ break
1607
+ }
1608
+ const k: i32 = keys[at]
1408
1609
  if (k === -1) {
1409
1610
  break
1410
1611
  }
1411
- // The entry at `j` stays when its home is cyclically within (i, j].
1412
- const home: i32 = k & mask
1413
- const stays: boolean = i <= j ? home > i && home <= j : home > i || home <= j
1414
- if (!stays) {
1415
- this.mapKeys[i] = k
1416
- this.mapValues[i] = this.mapValues[j]
1417
- i = j
1612
+ // The entry at `at` stays when its home is cyclically within (hole, at].
1613
+ const home: i32 = toI32(toU32(k) & mask)
1614
+ const stays: boolean = hole <= at ? home > hole && home <= at : home > hole || home <= at
1615
+ if (!stays && hole >= 0 && hole < toI32(keys.length) && hole < toI32(values.length)) {
1616
+ keys[hole] = k
1617
+ values[hole] = values[at]
1618
+ hole = at
1418
1619
  }
1419
1620
  }
1420
- this.mapKeys[i] = -1
1621
+ if (hole >= 0 && hole < toI32(keys.length)) {
1622
+ keys[hole] = -1
1623
+ }
1421
1624
  }
1422
1625
  }
1423
1626