nish 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/README.md +1 -1
  2. package/docs/AI.md +18 -16
  3. package/docs/INSTALL.md +3 -1
  4. package/package.json +5 -5
  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
@@ -224,6 +224,59 @@ const QUIC_RECOVERY_CUBIC_SCALE: i64 = 5000000000
224
224
  /** The larger of two times or durations. */
225
225
  const quicRecoveryMax = (a: i64, b: i64): i64 => (a > b ? a : b)
226
226
 
227
+ /** 2^63 − 1, the largest `i64`, which a sum or product past it is held at. */
228
+ const quicRecoveryTop = (): i64 => toI64((toU64(1) << toU64(63)) - toU64(1))
229
+
230
+ /**
231
+ * `a + b`, held at 2^63 − 1 when the true sum is past it and at −2^63 when
232
+ * it is below. Recovery's times, durations and byte counts are far inside
233
+ * an `i64`, so a held sum is one no clock or window reaches: a deadline that
234
+ * never comes, a window nothing fills. Taken in `u64`, whose `+` wraps, and
235
+ * read back as the `i64` with the same bits, which is the true sum whenever
236
+ * that fits.
237
+ */
238
+ const quicRecoverySum = (a: i64, b: i64): i64 => {
239
+ const sum: i64 = toI64(toU64(a) + toU64(b))
240
+ if (a >= 0 && b >= 0 && sum < 0) {
241
+ return quicRecoveryTop()
242
+ }
243
+ if (a < 0 && b < 0 && sum >= 0) {
244
+ return toI64(toU64(1) << toU64(63))
245
+ }
246
+ return sum
247
+ }
248
+
249
+ /** `a − b`, held at 2^63 − 1 / −2^63 where the true difference passes either. */
250
+ const quicRecoveryLess = (a: i64, b: i64): i64 => {
251
+ const diff: i64 = toI64(toU64(a) - toU64(b))
252
+ if (a >= 0 && b < 0 && diff < 0) {
253
+ return quicRecoveryTop()
254
+ }
255
+ if (a < 0 && b >= 0 && diff >= 0) {
256
+ return toI64(toU64(1) << toU64(63))
257
+ }
258
+ return diff
259
+ }
260
+
261
+ /** `n + 1` for a counter, held at 2^31 − 1, which no connection's events reach. */
262
+ const quicRecoveryStep = (n: i32): i32 => (n >= 2147483647 ? n : n + 1)
263
+
264
+ /**
265
+ * `a × b` for two non-negative times, durations or byte counts, held at
266
+ * 2^63 − 1 when the true product is past it, as `quicRecoverySum` holds a
267
+ * sum. Below that it is taken in `u64` and is exact. A negative operand
268
+ * counts as 0: every caller passes values it has kept at 0 or above.
269
+ */
270
+ const quicRecoveryProduct = (a: i64, b: i64): i64 => {
271
+ if (a <= 0 || b <= 0) {
272
+ return 0
273
+ }
274
+ if (b > quicRecoveryTop() / a) {
275
+ return quicRecoveryTop()
276
+ }
277
+ return toI64(toU64(a) * toU64(b))
278
+ }
279
+
227
280
  /** `x` clamped to −`QUIC_RECOVERY_CUBIC_SPAN` .. `QUIC_RECOVERY_CUBIC_SPAN`. */
228
281
  const quicRecoveryCubicClamp = (x: i64): i64 => {
229
282
  if (x > QUIC_RECOVERY_CUBIC_SPAN) {
@@ -241,24 +294,27 @@ const quicRecoveryCubicClamp = (x: i64): i64 => {
241
294
  * @returns Its root, 0 to 40000.
242
295
  */
243
296
  export const quicRecoveryCbrt = (x: i64): i64 => {
244
- let lo: i64 = 0
245
- let hi: i64 = QUIC_RECOVERY_CUBIC_SPAN
246
- if (x <= lo) {
247
- return lo
248
- }
249
- if (x >= hi * hi * hi) {
250
- return hi
297
+ if (x <= 0) {
298
+ return 0
299
+ }
300
+ // Unsigned, and exact: every value below is at most the span, 40000, and every cube at most
301
+ // 40000³ = 6.4 × 10¹³, far inside 64 bits.
302
+ const cube: u64 = toU64(x)
303
+ let lo: u64 = 0
304
+ let hi: u64 = toU64(QUIC_RECOVERY_CUBIC_SPAN)
305
+ if (cube >= hi * hi * hi) {
306
+ return QUIC_RECOVERY_CUBIC_SPAN
251
307
  }
252
308
  // lo³ <= x < hi³ holds at every step.
253
309
  while (hi - lo > 1) {
254
- const mid: i64 = (lo + hi) / 2
255
- if (mid * mid * mid <= x) {
310
+ const mid: u64 = (lo + hi) / 2
311
+ if (mid * mid * mid <= cube) {
256
312
  lo = mid
257
313
  } else {
258
314
  hi = mid
259
315
  }
260
316
  }
261
- return lo
317
+ return toI64(lo)
262
318
  }
263
319
 
264
320
  /**
@@ -297,7 +353,7 @@ export class QuicSentPackets {
297
353
  lastAckEliciting: i64 = -1
298
354
  /** The bytes of this space's packets in flight. */
299
355
  bytesInFlight: i64 = 0
300
- private capacity: i32 = 0
356
+ private capacity: u8 = 0
301
357
  private head: i32 = 0
302
358
  /**
303
359
  * How many packets the ring keeps, in flight and settled; a test reads it to see what a flight
@@ -324,9 +380,10 @@ export class QuicSentPackets {
324
380
  *
325
381
  * @param capacity How many sent packets the ring records at once, fixed for its life: in flight and
326
382
  * settled alike, until `compact` drops the settled ones. `QUIC_RECOVERY_HANDSHAKE_CAPACITY` or
327
- * `QUIC_RECOVERY_APPLICATION_CAPACITY`.
383
+ * `QUIC_RECOVERY_APPLICATION_CAPACITY`. A `u8`, so no ring is larger than 255 slots and an
384
+ * index sum of two slots stays far inside `i32`.
328
385
  */
329
- constructor(capacity: i32) {
386
+ constructor(capacity: u8) {
330
387
  this.capacity = capacity
331
388
  this.pn = new Array<i64>(capacity)
332
389
  this.timeSent = new Array<i64>(capacity)
@@ -342,7 +399,14 @@ export class QuicSentPackets {
342
399
  * @internal Read by quic.ts, and in quic-recovery.ts outside the class.
343
400
  */
344
401
  slot(k: i32): i32 {
345
- return (this.head + k) % this.capacity
402
+ const capacity: i32 = toI32(this.capacity)
403
+ const head: i32 = this.head
404
+ // Never taken for a `k` below `count`: `head` stays below the capacity, and `count` never passes it.
405
+ if (k < 0 || k >= capacity || head < 0 || head >= capacity) {
406
+ return -1
407
+ }
408
+ const s: i32 = head + k
409
+ return s >= capacity ? s - capacity : s
346
410
  }
347
411
 
348
412
  /**
@@ -497,7 +561,7 @@ export class QuicSentPackets {
497
561
  */
498
562
  full(): boolean {
499
563
  this.compact()
500
- return this.count >= this.capacity
564
+ return this.count >= toI32(this.capacity)
501
565
  }
502
566
 
503
567
  /**
@@ -505,11 +569,17 @@ export class QuicSentPackets {
505
569
  * @internal Read by quic.ts, and in quic-recovery.ts outside the class.
506
570
  */
507
571
  compact(): void {
508
- while (this.count > 0 && this.stateAt(this.head) !== QUIC_RECOVERY_SENT) {
509
- this.setState(this.head, QUIC_RECOVERY_FREE)
510
- this.head = (this.head + 1) % this.capacity
511
- this.count = this.count - 1
512
- }
572
+ const capacity: i32 = toI32(this.capacity)
573
+ let count: i32 = this.count
574
+ let head: i32 = this.head
575
+ while (count > 0 && head >= 0 && head < capacity && this.stateAt(head) !== QUIC_RECOVERY_SENT) {
576
+ this.setState(head, QUIC_RECOVERY_FREE)
577
+ const next: i32 = head + 1
578
+ head = next >= capacity ? 0 : next
579
+ count = count - 1
580
+ }
581
+ this.head = head
582
+ this.count = count
513
583
  if (this.count === 0) {
514
584
  this.head = 0
515
585
  }
@@ -529,9 +599,18 @@ export class QuicSentPackets {
529
599
  * @internal Read in quic-recovery.ts outside the class.
530
600
  */
531
601
  settle(s: i32, state: i32): void {
602
+ const size: i64 = toI64(this.sizeAt(s))
532
603
  this.setState(s, state)
533
- this.inFlight = this.inFlight - 1
534
- this.bytesInFlight = this.bytesInFlight - toI64(this.sizeAt(s))
604
+ // Both guards hold for a packet in flight, the only kind a caller settles: it is counted in
605
+ // `inFlight` and its size in `bytesInFlight`.
606
+ const inFlight: i32 = this.inFlight
607
+ if (inFlight > 0) {
608
+ this.inFlight = inFlight - 1
609
+ }
610
+ const bytes: i64 = this.bytesInFlight
611
+ if (bytes >= 0 && size >= 0) {
612
+ this.bytesInFlight = bytes - size
613
+ }
535
614
  }
536
615
 
537
616
  /**
@@ -540,9 +619,11 @@ export class QuicSentPackets {
540
619
  */
541
620
  markLost(s: i32): void {
542
621
  this.settle(s, QUIC_RECOVERY_LOST_NOW)
543
- if (this.lostCount < toI32(this.lost.length)) {
544
- this.lost[this.lostCount] = s
545
- this.lostCount = this.lostCount + 1
622
+ const lost: i32[] = this.lost
623
+ const n: i32 = this.lostCount
624
+ if (n >= 0 && n < toI32(lost.length)) {
625
+ lost[n] = s
626
+ this.lostCount = n + 1
546
627
  }
547
628
  }
548
629
 
@@ -580,15 +661,19 @@ export class QuicSentPackets {
580
661
 
581
662
  /** Whether `pn` falls in one of the first `count` `[smallest, largest]` pairs of `ranges`. */
582
663
  const quicRecoveryInRanges = (ranges: i64[], count: i32, pn: i64): boolean => {
583
- for (let k: i32 = 0; k < count; k += 1) {
584
- const low: i32 = k * 2
664
+ const n: i32 = toI32(ranges.length)
665
+ let left: i32 = count
666
+ let low: i32 = 0
667
+ while (left > 0 && low >= 0 && low < n) {
585
668
  const high: i32 = low + 1
586
- if (low < 0 || low >= toI32(ranges.length) || high < 0 || high >= toI32(ranges.length)) {
669
+ if (high >= n) {
587
670
  return false
588
671
  }
589
672
  if (pn >= ranges[low] && pn <= ranges[high]) {
590
673
  return true
591
674
  }
675
+ low = high + 1
676
+ left = left - 1
592
677
  }
593
678
  return false
594
679
  }
@@ -707,9 +792,9 @@ export class QuicRecovery {
707
792
  */
708
793
  constructor() {
709
794
  this.spaces = [
710
- new QuicSentPackets(QUIC_RECOVERY_HANDSHAKE_CAPACITY),
711
- new QuicSentPackets(QUIC_RECOVERY_HANDSHAKE_CAPACITY),
712
- new QuicSentPackets(QUIC_RECOVERY_APPLICATION_CAPACITY),
795
+ new QuicSentPackets(toU8(QUIC_RECOVERY_HANDSHAKE_CAPACITY)),
796
+ new QuicSentPackets(toU8(QUIC_RECOVERY_HANDSHAKE_CAPACITY)),
797
+ new QuicSentPackets(toU8(QUIC_RECOVERY_APPLICATION_CAPACITY)),
713
798
  ]
714
799
  this.reset()
715
800
  }
@@ -773,8 +858,8 @@ export class QuicRecovery {
773
858
  * @returns The initial window in bytes: 12000 at 1200 bytes.
774
859
  */
775
860
  initialWindow(): i64 {
776
- const ten: i64 = 10 * this.datagramSize
777
- const floor: i64 = quicRecoveryMax(14720, 2 * this.datagramSize)
861
+ const ten: i64 = quicRecoveryProduct(10, this.datagramSize)
862
+ const floor: i64 = quicRecoveryMax(14720, quicRecoveryProduct(2, this.datagramSize))
778
863
  return ten < floor ? ten : floor
779
864
  }
780
865
 
@@ -784,7 +869,7 @@ export class QuicRecovery {
784
869
  * @returns The minimum window in bytes: 2400 at 1200 bytes.
785
870
  */
786
871
  minimumWindow(): i64 {
787
- return 2 * this.datagramSize
872
+ return quicRecoveryProduct(2, this.datagramSize)
788
873
  }
789
874
 
790
875
  /**
@@ -853,7 +938,7 @@ export class QuicRecovery {
853
938
  bytesInFlight(): i64 {
854
939
  let total: i64 = 0
855
940
  for (const sp of this.spaces) {
856
- total = total + sp.bytesInFlight
941
+ total = quicRecoverySum(total, sp.bytesInFlight)
857
942
  }
858
943
  return total
859
944
  }
@@ -865,7 +950,7 @@ export class QuicRecovery {
865
950
  * @returns True when the bytes in flight plus `datagramSize` bytes fit the congestion window.
866
951
  */
867
952
  canSend(): boolean {
868
- return this.bytesInFlight() + this.datagramSize <= this.congestionWindow
953
+ return quicRecoverySum(this.bytesInFlight(), this.datagramSize) <= this.congestionWindow
869
954
  }
870
955
 
871
956
  /**
@@ -886,26 +971,38 @@ export class QuicRecovery {
886
971
  if (sp === null || size <= 0 || pn <= sp.lastPn || pn > QUIC_MAX_VARINT || sp.full()) {
887
972
  return -1
888
973
  }
889
- const s: i32 = sp.slot(sp.count)
974
+ const count: i32 = sp.count
975
+ const inFlight: i32 = sp.inFlight
976
+ const s: i32 = sp.slot(count)
977
+ const pns: i64[] = sp.pn
978
+ const times: i64[] = sp.timeSent
979
+ const fines: i64[] = sp.fineSent
980
+ const sizes: i32[] = sp.size
981
+ // Never taken: `full()` answered false, so the ring has a free slot, and
982
+ // every array is the ring's capacity long, which neither count reaches.
890
983
  if (
891
984
  s < 0 ||
892
- s >= toI32(sp.pn.length) ||
893
- s >= toI32(sp.timeSent.length) ||
894
- s >= toI32(sp.fineSent.length) ||
895
- s >= toI32(sp.size.length)
985
+ s >= toI32(pns.length) ||
986
+ s >= toI32(times.length) ||
987
+ s >= toI32(fines.length) ||
988
+ s >= toI32(sizes.length) ||
989
+ count < 0 ||
990
+ count >= toI32(pns.length) ||
991
+ inFlight < 0 ||
992
+ inFlight >= toI32(pns.length)
896
993
  ) {
897
994
  return -1
898
995
  }
899
- sp.pn[s] = pn
900
- sp.timeSent[s] = now
901
- sp.fineSent[s] = this.fineNow(now)
902
- sp.size[s] = size
996
+ pns[s] = pn
997
+ times[s] = now
998
+ fines[s] = this.fineNow(now)
999
+ sizes[s] = size
903
1000
  sp.setMarked(s, false)
904
1001
  sp.setProbe(s, false)
905
1002
  sp.setState(s, QUIC_RECOVERY_SENT)
906
- sp.count = sp.count + 1
907
- sp.inFlight = sp.inFlight + 1
908
- sp.bytesInFlight = sp.bytesInFlight + toI64(size)
1003
+ sp.count = count + 1
1004
+ sp.inFlight = inFlight + 1
1005
+ sp.bytesInFlight = quicRecoverySum(sp.bytesInFlight, toI64(size))
909
1006
  sp.lastPn = pn
910
1007
  sp.lastAckEliciting = now
911
1008
  return s
@@ -955,15 +1052,15 @@ export class QuicRecovery {
955
1052
  if (sp === null) {
956
1053
  return
957
1054
  }
958
- sp.ectSent = sp.ectSent + 1
1055
+ sp.ectSent = quicRecoverySum(sp.ectSent, 1)
959
1056
  const recorded: boolean = sp.inFlightAt(slot) && !sp.markedAt(slot)
960
1057
  if (recorded) {
961
1058
  sp.setMarked(slot, true)
962
1059
  }
963
1060
  if (this.ecnState === QUIC_RECOVERY_ECN_TESTING || this.ecnState === QUIC_RECOVERY_ECN_UNKNOWN) {
964
- this.ecnTestSent = this.ecnTestSent + 1
1061
+ this.ecnTestSent = quicRecoveryStep(this.ecnTestSent)
965
1062
  if (recorded) {
966
- this.ecnTestRecorded = this.ecnTestRecorded + 1
1063
+ this.ecnTestRecorded = quicRecoveryStep(this.ecnTestRecorded)
967
1064
  }
968
1065
  if (this.ecnTestSent >= QUIC_RECOVERY_ECN_TESTING_PACKETS) {
969
1066
  this.ecnState = QUIC_RECOVERY_ECN_UNKNOWN
@@ -1038,7 +1135,7 @@ export class QuicRecovery {
1038
1135
  }
1039
1136
  // The bytes in flight before this ACK decide whether the window was in
1040
1137
  // use, and so whether it may grow (§7.8).
1041
- const inUse: boolean = this.bytesInFlight() * 2 >= this.congestionWindow
1138
+ const inUse: boolean = quicRecoveryProduct(this.bytesInFlight(), 2) >= this.congestionWindow
1042
1139
  let sampleAt: i32 = -1
1043
1140
  // The send time of the first recorded packet past the largest acknowledged, or `now`: no earlier than the largest's own.
1044
1141
  let later: i64 = now
@@ -1051,9 +1148,11 @@ export class QuicRecovery {
1051
1148
  }
1052
1149
  if (sp.stateAt(s) === QUIC_RECOVERY_SENT && quicRecoveryInRanges(ranges, count, sp.pnAt(s))) {
1053
1150
  sp.settle(s, QUIC_RECOVERY_ACKED)
1054
- if (sp.ackedCount < toI32(sp.acked.length)) {
1055
- sp.acked[sp.ackedCount] = s
1056
- sp.ackedCount = sp.ackedCount + 1
1151
+ const acked: i32[] = sp.acked
1152
+ const n: i32 = sp.ackedCount
1153
+ if (n >= 0 && n < toI32(acked.length)) {
1154
+ acked[n] = s
1155
+ sp.ackedCount = n + 1
1057
1156
  }
1058
1157
  if (sp.pnAt(s) === largest) {
1059
1158
  sampleAt = s
@@ -1070,7 +1169,7 @@ export class QuicRecovery {
1070
1169
  return 0
1071
1170
  }
1072
1171
  if (sampleAt >= 0) {
1073
- this.latestRtt = quicRecoveryMax(now - sp.timeAt(sampleAt), 0)
1172
+ this.latestRtt = quicRecoveryMax(quicRecoveryLess(now, sp.timeAt(sampleAt)), 0)
1074
1173
  // §5.3: the Initial space's ACKs are not delayed by the peer, so their delay is ignored.
1075
1174
  const none: i64 = 0
1076
1175
  const delay: i64 = space === QUIC_RECOVERY_INITIAL ? none : quicRecoveryMax(ackDelay, none)
@@ -1115,7 +1214,7 @@ export class QuicRecovery {
1115
1214
  let marked: i64 = 0
1116
1215
  for (let k: i32 = 0; k < sp.ackedCount; k += 1) {
1117
1216
  if (sp.markedAt(sp.ackedSlot(k))) {
1118
- marked = marked + 1
1217
+ marked = quicRecoverySum(marked, 1)
1119
1218
  }
1120
1219
  }
1121
1220
  if (ect0 < 0 || ect1 < 0 || ce < 0) {
@@ -1136,9 +1235,9 @@ export class QuicRecovery {
1136
1235
  ect1 < sp.ect1 ||
1137
1236
  ce < sp.ce ||
1138
1237
  ect0 > sent ||
1139
- ect1 > sent - ect0 ||
1140
- ce > sent - ect0 - ect1 ||
1141
- ect0 - sp.ect0 + (ce - sp.ce) < marked
1238
+ ect1 > quicRecoveryLess(sent, ect0) ||
1239
+ ce > quicRecoveryLess(quicRecoveryLess(sent, ect0), ect1) ||
1240
+ quicRecoverySum(quicRecoveryLess(ect0, sp.ect0), quicRecoveryLess(ce, sp.ce)) < marked
1142
1241
  ) {
1143
1242
  this.ecnState = QUIC_RECOVERY_ECN_FAILED
1144
1243
  return
@@ -1152,9 +1251,10 @@ export class QuicRecovery {
1152
1251
  sp.ce = ce
1153
1252
  if (rose) {
1154
1253
  const events: i32 = this.congestionEvents
1155
- this.congestionEvent(sp.ackedCount > 0 ? sp.timeAt(sp.ackedSlot(sp.ackedCount - 1)) : later, now)
1254
+ const n: i32 = sp.ackedCount
1255
+ this.congestionEvent(n > 0 ? sp.timeAt(sp.ackedSlot(n - 1)) : later, now)
1156
1256
  if (this.congestionEvents > events) {
1157
- this.ecnCongestionEvents = this.ecnCongestionEvents + 1
1257
+ this.ecnCongestionEvents = quicRecoveryStep(this.ecnCongestionEvents)
1158
1258
  }
1159
1259
  }
1160
1260
  }
@@ -1175,12 +1275,14 @@ export class QuicRecovery {
1175
1275
  const delay: i64 = this.boundAckDelay(ackDelay)
1176
1276
  // The delay is taken off only where that leaves the sample at or above min_rtt.
1177
1277
  let adjusted: i64 = latest
1178
- if (latest >= this.minRtt + delay) {
1179
- adjusted = latest - delay
1278
+ if (quicRecoveryLess(latest, this.minRtt) >= delay) {
1279
+ adjusted = quicRecoveryLess(latest, delay)
1180
1280
  }
1181
- const gap: i64 = this.smoothedRtt > adjusted ? this.smoothedRtt - adjusted : adjusted - this.smoothedRtt
1182
- this.rttVar = (3 * this.rttVar + gap) / 4
1183
- this.smoothedRtt = (7 * this.smoothedRtt + adjusted) / 8
1281
+ const smoothed: i64 = this.smoothedRtt
1282
+ const gap: i64 =
1283
+ smoothed > adjusted ? quicRecoveryLess(smoothed, adjusted) : quicRecoveryLess(adjusted, smoothed)
1284
+ this.rttVar = quicRecoverySum(quicRecoveryProduct(3, this.rttVar), gap) / 4
1285
+ this.smoothedRtt = quicRecoverySum(quicRecoveryProduct(7, smoothed), adjusted) / 8
1184
1286
  }
1185
1287
 
1186
1288
  /** The peer's ACK delay in milliseconds as §5.3 counts it: at most max_ack_delay once the handshake is confirmed. */
@@ -1196,8 +1298,10 @@ export class QuicRecovery {
1196
1298
  * @returns The same instant in microseconds.
1197
1299
  */
1198
1300
  fineNow(now: i64): i64 {
1199
- const coarse: i64 = now * QUIC_RECOVERY_MICROS
1200
- return this.micros >= coarse && this.micros < coarse + QUIC_RECOVERY_MICROS ? this.micros : coarse
1301
+ const coarse: i64 = quicRecoveryProduct(now, QUIC_RECOVERY_MICROS)
1302
+ return this.micros >= coarse && this.micros < quicRecoverySum(coarse, QUIC_RECOVERY_MICROS)
1303
+ ? this.micros
1304
+ : coarse
1201
1305
  }
1202
1306
 
1203
1307
  /**
@@ -1211,8 +1315,9 @@ export class QuicRecovery {
1211
1315
  */
1212
1316
  private fineSample(now: i64, sent: i64): i64 {
1213
1317
  const at: i64 = this.fineNow(now)
1214
- const floor: i64 = at === this.micros ? 1 : QUIC_RECOVERY_GRANULARITY * QUIC_RECOVERY_MICROS
1215
- return quicRecoveryMax(at - sent, floor)
1318
+ const floor: i64 =
1319
+ at === this.micros ? 1 : quicRecoveryProduct(QUIC_RECOVERY_GRANULARITY, QUIC_RECOVERY_MICROS)
1320
+ return quicRecoveryMax(quicRecoveryLess(at, sent), floor)
1216
1321
  }
1217
1322
 
1218
1323
  /**
@@ -1227,12 +1332,12 @@ export class QuicRecovery {
1227
1332
  this.pacerRtt = sample
1228
1333
  return
1229
1334
  }
1230
- const delay: i64 = this.boundAckDelay(ackDelay) * QUIC_RECOVERY_MICROS
1335
+ const delay: i64 = quicRecoveryProduct(this.boundAckDelay(ackDelay), QUIC_RECOVERY_MICROS)
1231
1336
  let adjusted: i64 = sample
1232
- if (sample >= this.minRtt * QUIC_RECOVERY_MICROS + delay) {
1233
- adjusted = sample - delay
1337
+ if (quicRecoveryLess(sample, quicRecoveryProduct(this.minRtt, QUIC_RECOVERY_MICROS)) >= delay) {
1338
+ adjusted = quicRecoveryLess(sample, delay)
1234
1339
  }
1235
- this.pacerRtt = (7 * this.pacerRtt + adjusted) / 8
1340
+ this.pacerRtt = quicRecoverySum(quicRecoveryProduct(7, this.pacerRtt), adjusted) / 8
1236
1341
  }
1237
1342
 
1238
1343
  /**
@@ -1241,7 +1346,7 @@ export class QuicRecovery {
1241
1346
  * @returns How long after a packet was sent it is lost by the time threshold, in milliseconds, at least 1.
1242
1347
  */
1243
1348
  lossDelay(): i64 {
1244
- const delay: i64 = (9 * quicRecoveryMax(this.latestRtt, this.smoothedRtt)) / 8
1349
+ const delay: i64 = quicRecoveryProduct(9, quicRecoveryMax(this.latestRtt, this.smoothedRtt)) / 8
1245
1350
  return quicRecoveryMax(delay, QUIC_RECOVERY_GRANULARITY)
1246
1351
  }
1247
1352
 
@@ -1254,7 +1359,7 @@ export class QuicRecovery {
1254
1359
  private detectLoss(sp: QuicSentPackets, now: i64): void {
1255
1360
  sp.lossTime = -1
1256
1361
  const delay: i64 = this.lossDelay()
1257
- const lostSendTime: i64 = now - delay
1362
+ const lostSendTime: i64 = quicRecoveryLess(now, delay)
1258
1363
  for (let k: i32 = 0; k < sp.count; k += 1) {
1259
1364
  const s: i32 = sp.slot(k)
1260
1365
  const pn: i64 = sp.pnAt(s)
@@ -1264,10 +1369,13 @@ export class QuicRecovery {
1264
1369
  if (sp.stateAt(s) !== QUIC_RECOVERY_SENT) {
1265
1370
  continue
1266
1371
  }
1267
- if (sp.timeAt(s) <= lostSendTime || sp.largestAcked >= pn + QUIC_RECOVERY_PACKET_THRESHOLD) {
1372
+ if (
1373
+ sp.timeAt(s) <= lostSendTime ||
1374
+ quicRecoveryLess(sp.largestAcked, pn) >= QUIC_RECOVERY_PACKET_THRESHOLD
1375
+ ) {
1268
1376
  sp.markLost(s)
1269
1377
  } else {
1270
- const at: i64 = sp.timeAt(s) + delay
1378
+ const at: i64 = quicRecoverySum(sp.timeAt(s), delay)
1271
1379
  if (sp.lossTime < 0 || at < sp.lossTime) {
1272
1380
  sp.lossTime = at
1273
1381
  }
@@ -1290,7 +1398,7 @@ export class QuicRecovery {
1290
1398
  newest = quicRecoveryMax(newest, sp.timeAt(sp.lostSlot(k)))
1291
1399
  }
1292
1400
  if (sp.markedAt(sp.lostSlot(k)) && this.ecnState !== QUIC_RECOVERY_ECN_CAPABLE) {
1293
- this.ecnTestLost = this.ecnTestLost + 1
1401
+ this.ecnTestLost = quicRecoveryStep(this.ecnTestLost)
1294
1402
  }
1295
1403
  }
1296
1404
  // RFC 9000 A.4: every packet marked while testing that could be lost was.
@@ -1311,7 +1419,7 @@ export class QuicRecovery {
1311
1419
  // CUBIC starts over from slow start (RFC 9438 §4.8): its next stage is a new epoch.
1312
1420
  this.cubicEpoch = -1
1313
1421
  this.cubicIdle = -1
1314
- this.persistentCongestions = this.persistentCongestions + 1
1422
+ this.persistentCongestions = quicRecoveryStep(this.persistentCongestions)
1315
1423
  }
1316
1424
  for (let k: i32 = 0; k < sp.lostCount; k += 1) {
1317
1425
  sp.setState(sp.lostSlot(k), QUIC_RECOVERY_LOST)
@@ -1329,7 +1437,10 @@ export class QuicRecovery {
1329
1437
  if (this.firstSampleTime < 0) {
1330
1438
  return false
1331
1439
  }
1332
- const span: i64 = (this.ptoBase() + this.maxAckDelay) * QUIC_RECOVERY_PERSISTENT_THRESHOLD
1440
+ const span: i64 = quicRecoveryProduct(
1441
+ quicRecoverySum(this.ptoBase(), this.maxAckDelay),
1442
+ QUIC_RECOVERY_PERSISTENT_THRESHOLD
1443
+ )
1333
1444
  let first: i64 = -1
1334
1445
  let fresh: boolean = false
1335
1446
  for (let k: i32 = 0; k < sp.count; k += 1) {
@@ -1357,7 +1468,7 @@ export class QuicRecovery {
1357
1468
  first = sp.timeAt(s)
1358
1469
  }
1359
1470
  fresh = fresh || state === QUIC_RECOVERY_LOST_NOW
1360
- if (fresh && sp.timeAt(s) - first > span) {
1471
+ if (fresh && quicRecoveryLess(sp.timeAt(s), first) > span) {
1361
1472
  return true
1362
1473
  }
1363
1474
  }
@@ -1382,16 +1493,16 @@ export class QuicRecovery {
1382
1493
  this.recoveryStart = now
1383
1494
  const cwnd: i64 = this.congestionWindow
1384
1495
  if (this.controller === QUIC_RECOVERY_CUBIC) {
1385
- this.cubicMax = cwnd < this.cubicMax ? (cwnd * 17) / 20 : cwnd
1496
+ this.cubicMax = cwnd < this.cubicMax ? quicRecoveryProduct(cwnd, 17) / 20 : cwnd
1386
1497
  this.cubicPrior = cwnd
1387
1498
  this.cubicEpoch = -1
1388
1499
  this.cubicIdle = -1
1389
- this.ssthresh = (cwnd * 7) / 10
1500
+ this.ssthresh = quicRecoveryProduct(cwnd, 7) / 10
1390
1501
  } else {
1391
1502
  this.ssthresh = cwnd / 2
1392
1503
  }
1393
1504
  this.congestionWindow = quicRecoveryMax(this.ssthresh, this.minimumWindow())
1394
- this.congestionEvents = this.congestionEvents + 1
1505
+ this.congestionEvents = quicRecoveryStep(this.congestionEvents)
1395
1506
  }
1396
1507
 
1397
1508
  /**
@@ -1405,12 +1516,14 @@ export class QuicRecovery {
1405
1516
  return
1406
1517
  }
1407
1518
  const size: i64 = toI64(sp.sizeAt(s))
1408
- if (this.congestionWindow < this.ssthresh) {
1409
- this.congestionWindow = this.congestionWindow + size
1519
+ const cwnd: i64 = this.congestionWindow
1520
+ // A window of nothing has no fraction to grow by, so it grows as in slow start.
1521
+ if (cwnd < this.ssthresh || cwnd <= 0) {
1522
+ this.congestionWindow = quicRecoverySum(cwnd, size)
1410
1523
  } else if (this.controller === QUIC_RECOVERY_CUBIC) {
1411
1524
  this.cubicAcked(size, now)
1412
1525
  } else {
1413
- this.congestionWindow = this.congestionWindow + (this.datagramSize * size) / this.congestionWindow
1526
+ this.congestionWindow = quicRecoverySum(cwnd, quicRecoveryProduct(this.datagramSize, size) / cwnd)
1414
1527
  }
1415
1528
  }
1416
1529
 
@@ -1429,6 +1542,10 @@ export class QuicRecovery {
1429
1542
  */
1430
1543
  private cubicAcked(size: i64, now: i64): void {
1431
1544
  const cwnd: i64 = this.congestionWindow
1545
+ // Never taken: `packetAcked` grows a window of nothing as slow start does.
1546
+ if (cwnd <= 0) {
1547
+ return
1548
+ }
1432
1549
  if (this.cubicEpoch < 0) {
1433
1550
  this.cubicEpoch = now
1434
1551
  this.cubicIdle = -1
@@ -1436,29 +1553,39 @@ export class QuicRecovery {
1436
1553
  this.cubicEstCarry = 0
1437
1554
  this.cubicCarry = 0
1438
1555
  if (this.cubicMax > cwnd) {
1439
- this.cubicK = this.cubicSpan(this.cubicMax - cwnd)
1556
+ this.cubicK = this.cubicSpan(quicRecoveryLess(this.cubicMax, cwnd))
1440
1557
  } else {
1441
1558
  this.cubicK = 0
1442
1559
  this.cubicMax = cwnd
1443
1560
  }
1444
1561
  }
1445
1562
  const alpha: i64 = this.cubicEst >= this.cubicPrior ? 17 : 9
1446
- const num: i64 = alpha * size * this.datagramSize + this.cubicEstCarry
1447
- const den: i64 = 17 * cwnd
1448
- this.cubicEst = this.cubicEst + num / den
1563
+ const num: i64 = quicRecoverySum(
1564
+ quicRecoveryProduct(quicRecoveryProduct(alpha, size), this.datagramSize),
1565
+ this.cubicEstCarry
1566
+ )
1567
+ const den: i64 = quicRecoveryProduct(17, cwnd)
1568
+ // Never taken: 17 × a window past 0 is past 0, or held at 2^63 − 1.
1569
+ if (den <= 0) {
1570
+ return
1571
+ }
1572
+ this.cubicEst = quicRecoverySum(this.cubicEst, num / den)
1449
1573
  this.cubicEstCarry = num % den
1450
- const t: i64 = now - this.cubicEpoch
1574
+ const t: i64 = quicRecoveryLess(now, this.cubicEpoch)
1451
1575
  if (this.cubicWindow(t) < this.cubicEst) {
1452
1576
  this.congestionWindow = quicRecoveryMax(cwnd, this.cubicEst)
1453
1577
  return
1454
1578
  }
1455
- const ceiling: i64 = cwnd + cwnd / 2
1456
- const curve: i64 = this.cubicWindow(t + this.smoothedRtt)
1579
+ const ceiling: i64 = quicRecoverySum(cwnd, cwnd / 2)
1580
+ const curve: i64 = this.cubicWindow(quicRecoverySum(t, this.smoothedRtt))
1457
1581
  const target: i64 = quicRecoveryMax(cwnd, curve > ceiling ? ceiling : curve)
1458
1582
  // The window only grows within a stage, so the carry, below the window
1459
1583
  // it was a fraction of, stays below the one it is added against.
1460
- const step: i64 = (target - cwnd) * size + this.cubicCarry
1461
- this.congestionWindow = cwnd + step / cwnd
1584
+ const step: i64 = quicRecoverySum(
1585
+ quicRecoveryProduct(quicRecoveryLess(target, cwnd), size),
1586
+ this.cubicCarry
1587
+ )
1588
+ this.congestionWindow = quicRecoverySum(cwnd, step / cwnd)
1462
1589
  this.cubicCarry = step % cwnd
1463
1590
  }
1464
1591
 
@@ -1469,8 +1596,13 @@ export class QuicRecovery {
1469
1596
  * division truncates toward zero.
1470
1597
  */
1471
1598
  private cubicWindow(t: i64): i64 {
1472
- const d: i64 = quicRecoveryCubicClamp(t - this.cubicK)
1473
- return this.cubicMax + (2 * this.datagramSize * (d * d * d)) / QUIC_RECOVERY_CUBIC_SCALE
1599
+ const d: i64 = quicRecoveryCubicClamp(quicRecoveryLess(t, this.cubicK))
1600
+ // |t − K| is at most 40000, so its cube, 6.4 × 10¹³ at most, fits; the sign goes back on after the division.
1601
+ const size: u64 = d < 0 ? toU64(0) - toU64(d) : toU64(d)
1602
+ const cube: i64 = toI64(size * size * size)
1603
+ const move: i64 =
1604
+ quicRecoveryProduct(cube, quicRecoveryProduct(2, this.datagramSize)) / QUIC_RECOVERY_CUBIC_SCALE
1605
+ return d < 0 ? quicRecoveryLess(this.cubicMax, move) : quicRecoverySum(this.cubicMax, move)
1474
1606
  }
1475
1607
 
1476
1608
  /**
@@ -1479,13 +1611,15 @@ export class QuicRecovery {
1479
1611
  * milliseconds, rounded down; a fall too deep for the cube to fit is 40 s.
1480
1612
  */
1481
1613
  private cubicSpan(drop: i64): i64 {
1482
- const two: i64 = 2 * this.datagramSize
1614
+ const two: i64 = quicRecoveryProduct(2, this.datagramSize)
1483
1615
  const span: i64 = QUIC_RECOVERY_CUBIC_SPAN
1484
- // drop >= span³ × 2 × size / (5 × 10⁹), which is 25600 × size, has K at the span or past it.
1485
- if (drop >= 25600 * this.datagramSize) {
1616
+ // drop >= span³ × 2 × size / (5 × 10⁹), which is 25600 × size, has K at the span or past it, as has a
1617
+ // datagram size of nothing. The product below is held at 2^63 − 1 only for a size past 72000, where
1618
+ // what it is divided to is past span³ too.
1619
+ if (drop >= quicRecoveryProduct(25600, this.datagramSize) || two <= 0) {
1486
1620
  return span
1487
1621
  }
1488
- return quicRecoveryCbrt((drop * QUIC_RECOVERY_CUBIC_SCALE) / two)
1622
+ return quicRecoveryCbrt(quicRecoveryProduct(drop, QUIC_RECOVERY_CUBIC_SCALE) / two)
1489
1623
  }
1490
1624
 
1491
1625
  /**
@@ -1505,7 +1639,7 @@ export class QuicRecovery {
1505
1639
  return
1506
1640
  }
1507
1641
  if (this.cubicIdle >= 0) {
1508
- this.cubicEpoch = this.cubicEpoch + (now - this.cubicIdle)
1642
+ this.cubicEpoch = quicRecoverySum(this.cubicEpoch, quicRecoveryLess(now, this.cubicIdle))
1509
1643
  this.cubicIdle = -1
1510
1644
  }
1511
1645
  }
@@ -1520,12 +1654,15 @@ export class QuicRecovery {
1520
1654
  */
1521
1655
  probeTimeout(): i64 {
1522
1656
  const base: i64 = this.ptoBase()
1523
- return this.handshakeConfirmed ? base + this.maxAckDelay : base
1657
+ return this.handshakeConfirmed ? quicRecoverySum(base, this.maxAckDelay) : base
1524
1658
  }
1525
1659
 
1526
1660
  /** smoothed_rtt + max(4 × rttvar, kGranularity): every probe timeout's period before max_ack_delay and the backoff. */
1527
1661
  private ptoBase(): i64 {
1528
- return this.smoothedRtt + quicRecoveryMax(4 * this.rttVar, QUIC_RECOVERY_GRANULARITY)
1662
+ return quicRecoverySum(
1663
+ this.smoothedRtt,
1664
+ quicRecoveryMax(quicRecoveryProduct(4, this.rttVar), QUIC_RECOVERY_GRANULARITY)
1665
+ )
1529
1666
  }
1530
1667
 
1531
1668
  /**
@@ -1553,9 +1690,9 @@ export class QuicRecovery {
1553
1690
  const backoff: i64 = toI64(1) << toI64(this.ptoCount)
1554
1691
  let duration: i64 = this.ptoBase()
1555
1692
  if (space === QUIC_RECOVERY_APPLICATION) {
1556
- duration = duration + this.maxAckDelay
1693
+ duration = quicRecoverySum(duration, this.maxAckDelay)
1557
1694
  }
1558
- return sp.lastAckEliciting + duration * backoff
1695
+ return quicRecoverySum(sp.lastAckEliciting, quicRecoveryProduct(duration, backoff))
1559
1696
  }
1560
1697
 
1561
1698
  /** The space whose probe timeout fires first, or -1 when no space may have one. */
@@ -1600,9 +1737,9 @@ export class QuicRecovery {
1600
1737
  * @returns The time `onTimeout` is next due, in the caller's milliseconds, or -1 when no timer is set.
1601
1738
  */
1602
1739
  deadline(amplificationBlocked: boolean): i64 {
1603
- const loss: i32 = this.lossSpace()
1604
- if (loss >= 0) {
1605
- return this.spaces[loss].lossTime
1740
+ const loss: QuicSentPackets | null = this.space(this.lossSpace())
1741
+ if (loss !== null) {
1742
+ return loss.lossTime
1606
1743
  }
1607
1744
  if (amplificationBlocked) {
1608
1745
  return -1
@@ -1634,16 +1771,17 @@ export class QuicRecovery {
1634
1771
  return QUIC_RECOVERY_TIMEOUT_NONE
1635
1772
  }
1636
1773
  const loss: i32 = this.lossSpace()
1637
- if (loss >= 0) {
1638
- const sp: QuicSentPackets = this.spaces[loss]
1774
+ const sp: QuicSentPackets | null = this.space(loss)
1775
+ if (sp !== null) {
1639
1776
  this.detectLoss(sp, now)
1640
1777
  this.packetsLost(sp, now)
1641
1778
  this.timeoutSpace = loss
1642
1779
  return QUIC_RECOVERY_TIMEOUT_LOSS
1643
1780
  }
1644
1781
  this.timeoutSpace = this.ptoSpace()
1645
- if (this.ptoCount < QUIC_RECOVERY_MAX_BACKOFF) {
1646
- this.ptoCount = this.ptoCount + 1
1782
+ const count: i32 = this.ptoCount
1783
+ if (count < QUIC_RECOVERY_MAX_BACKOFF) {
1784
+ this.ptoCount = count + 1
1647
1785
  }
1648
1786
  return QUIC_RECOVERY_TIMEOUT_PTO
1649
1787
  }
@@ -1708,13 +1846,17 @@ export class QuicRecovery {
1708
1846
  return
1709
1847
  }
1710
1848
  // A minute is far past what fills the budget, and keeps the product in range.
1711
- let elapsed: i64 = at - this.pacerTime
1849
+ let elapsed: i64 = quicRecoveryLess(at, this.pacerTime)
1712
1850
  if (elapsed > 60000000) {
1713
1851
  elapsed = 60000000
1714
1852
  }
1715
- const rtt: i64 = quicRecoveryMax(this.pacerRtt, 1)
1716
- const earned: i64 = (elapsed * this.pacerWindow() * 5) / (rtt * 4)
1717
- this.pacerBudget = this.pacerBudget + earned
1853
+ const per: i64 = quicRecoveryProduct(quicRecoveryMax(this.pacerRtt, 1), 4)
1854
+ // Never taken: 4 × an RTT of at least 1 is past 0, or held at 2^63 − 1.
1855
+ if (per <= 0) {
1856
+ return
1857
+ }
1858
+ const earned: i64 = quicRecoveryProduct(quicRecoveryProduct(elapsed, this.pacerWindow()), 5) / per
1859
+ this.pacerBudget = quicRecoverySum(this.pacerBudget, earned)
1718
1860
  if (this.pacerBudget > this.initialWindow()) {
1719
1861
  this.pacerBudget = this.initialWindow()
1720
1862
  }
@@ -1733,14 +1875,18 @@ export class QuicRecovery {
1733
1875
  */
1734
1876
  pacerDelay(now: i64, size: i32): i64 {
1735
1877
  this.pacerRefill(now)
1736
- const need: i64 = toI64(size) - this.pacerBudget
1878
+ const need: i64 = quicRecoveryLess(toI64(size), this.pacerBudget)
1737
1879
  if (need <= 0) {
1738
1880
  return 0
1739
1881
  }
1740
- const rtt: i64 = quicRecoveryMax(this.pacerRtt, 1)
1741
- const rate: i64 = quicRecoveryMax(this.pacerWindow() * 5, 1)
1742
- const wait: i64 = (need * rtt * 4 + rate - 1) / rate
1743
- return quicRecoveryMax((wait + QUIC_RECOVERY_MICROS - 1) / QUIC_RECOVERY_MICROS, 1)
1882
+ const rate: i64 = quicRecoveryMax(quicRecoveryProduct(this.pacerWindow(), 5), 1)
1883
+ // Never taken: the rate is at least 1.
1884
+ if (rate <= 0) {
1885
+ return 1
1886
+ }
1887
+ const time: i64 = quicRecoveryProduct(quicRecoveryProduct(need, quicRecoveryMax(this.pacerRtt, 1)), 4)
1888
+ const wait: i64 = quicRecoverySum(time, rate - 1) / rate
1889
+ return quicRecoveryMax(quicRecoverySum(wait, QUIC_RECOVERY_MICROS - 1) / QUIC_RECOVERY_MICROS, 1)
1744
1890
  }
1745
1891
 
1746
1892
  /**
@@ -1755,8 +1901,13 @@ export class QuicRecovery {
1755
1901
  * @returns The bytes the pacer earns in a millisecond, held to `least` and `most`.
1756
1902
  */
1757
1903
  pacerQuantum(least: i32, most: i32): i32 {
1758
- const rtt: i64 = quicRecoveryMax(this.pacerRtt, 1)
1759
- const milli: i64 = (this.pacerWindow() * 5 * QUIC_RECOVERY_MICROS) / (rtt * 4)
1904
+ const per: i64 = quicRecoveryProduct(quicRecoveryMax(this.pacerRtt, 1), 4)
1905
+ // Never taken: 4 × an RTT of at least 1 is past 0, or held at 2^63 − 1.
1906
+ if (per <= 0) {
1907
+ return least
1908
+ }
1909
+ const milli: i64 =
1910
+ quicRecoveryProduct(quicRecoveryProduct(this.pacerWindow(), 5), QUIC_RECOVERY_MICROS) / per
1760
1911
  if (milli < toI64(least)) {
1761
1912
  return least
1762
1913
  }
@@ -1771,6 +1922,6 @@ export class QuicRecovery {
1771
1922
  */
1772
1923
  onPaced(now: i64, size: i32): void {
1773
1924
  this.pacerRefill(now)
1774
- this.pacerBudget = this.pacerBudget - toI64(size)
1925
+ this.pacerBudget = quicRecoveryLess(this.pacerBudget, toI64(size))
1775
1926
  }
1776
1927
  }