@swf2es/codegen 0.1.41 → 0.1.43

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -220,11 +220,18 @@ export async function createCodegen(module) {
220
220
  if (!loaded) {
221
221
  return false;
222
222
  }
223
- for (const [index, abc] of saved.abcs) {
224
- if (into.domainLoadAbc(index, abc) !== 0) {
223
+ for (const domain of saved.revive?.domains ?? []) {
224
+ if (into.domainLoadRevive(domain) !== 0) {
225
225
  return false;
226
226
  }
227
227
  }
228
+ for (const abcs of [saved.abcs, saved.revive?.abcs ?? []]) {
229
+ for (const [index, abc] of abcs) {
230
+ if (into.domainLoadAbc(index, abc) !== 0) {
231
+ return false;
232
+ }
233
+ }
234
+ }
228
235
  return into.domainLoadRebuild();
229
236
  };
230
237
  // The rebuild domainCompact, domainRevive or domainLoadRebuild started in
@@ -285,6 +292,7 @@ export async function createCodegen(module) {
285
292
  started: 0,
286
293
  completed: 0,
287
294
  aborted: { spare: 0, refused: 0, unjournaled: 0, superseded: 0 },
295
+ revived: 0,
288
296
  lastCaughtUp: 0,
289
297
  lastSavedBytes: 0,
290
298
  };
@@ -295,8 +303,16 @@ export async function createCodegen(module) {
295
303
  // same domain would likely fail the same way.
296
304
  let lettings = 0;
297
305
  let retryAt = 0;
306
+ const revivals = [];
307
+ // Whether the last spare could not be instantiated, so that a revival waiting for one is done in place.
308
+ let spareFailed = false;
298
309
  const abortMove = (why) => {
299
310
  if (move !== null) {
311
+ if (why === "spare" || why === "refused") {
312
+ for (const r of move.revivals) {
313
+ r.failed = true;
314
+ }
315
+ }
300
316
  move = null;
301
317
  moves.aborted[why]++;
302
318
  if (why !== "superseded") {
@@ -313,8 +329,10 @@ export async function createCodegen(module) {
313
329
  instantiate(module).then((made) => {
314
330
  spare = made;
315
331
  making = false;
332
+ spareFailed = false;
316
333
  }, () => {
317
334
  making = false;
335
+ spareFailed = true;
318
336
  });
319
337
  }
320
338
  };
@@ -357,8 +375,10 @@ export async function createCodegen(module) {
357
375
  let bytes = 0;
358
376
  if (checkpoint) {
359
377
  bytes += checkpoint.state.byteLength;
360
- for (const abc of checkpoint.abcs.values()) {
361
- bytes += abc.byteLength;
378
+ for (const abcs of [checkpoint.abcs, checkpoint.revive?.abcs ?? new Map()]) {
379
+ for (const abc of abcs.values()) {
380
+ bytes += abc.byteLength;
381
+ }
362
382
  }
363
383
  }
364
384
  for (const entry of journal) {
@@ -381,8 +401,6 @@ export async function createCodegen(module) {
381
401
  };
382
402
  const restores = new Map();
383
403
  const abcDomains = [];
384
- // Each ABC's length, by its index, which a replay of its verification takes time in proportion to.
385
- const abcLengths = [];
386
404
  let replayable = true;
387
405
  const journaled = (entry, owner = 0) => {
388
406
  const id = owner | 0;
@@ -517,6 +535,12 @@ export async function createCodegen(module) {
517
535
  into.domainEvict(entry.appDomain);
518
536
  break;
519
537
  case "restore":
538
+ // One of a domain a move's load revived (Checkpoint.revive), which
539
+ // this instance, still evicting it, gave back for a revival of
540
+ // another under it: `into` holds it, live.
541
+ if (into.domainState(abcDomains[entry.index] ?? -1) === 0) {
542
+ break;
543
+ }
520
544
  if (into.domainRestore(entry.index, entry.bytes) !== 0) {
521
545
  throw new Error("a journaled ABC did not restore");
522
546
  }
@@ -592,10 +616,10 @@ export async function createCodegen(module) {
592
616
  // snapshot of this instance, as a move by compact is, and never reading
593
617
  // this instance again, while the host goes on with it, each call it
594
618
  // makes journaled and replayed into the spare once the spare has rebuilt.
595
- const startMove = (into) => {
619
+ const startMove = (into, carried = []) => {
596
620
  let saved;
597
621
  try {
598
- saved = saveCheckpoint();
622
+ saved = withRevivals(saveCheckpoint(), carried);
599
623
  }
600
624
  catch (error) {
601
625
  if (error instanceof CodegenOutOfMemory) {
@@ -604,7 +628,9 @@ export async function createCodegen(module) {
604
628
  }
605
629
  throw error;
606
630
  }
607
- // Parts that block the host for little: a part's time is its garbage's, but for one large method's verification.
631
+ // Parts that block the host for little, the rebuild's and catch-up's verifications alike, a
632
+ // long method's verification across parts too: a part's time is about its budget's, but for
633
+ // linking one large ABC, which a part does whole.
608
634
  into.domainRebuildBudget(MOVE_PartBudget);
609
635
  move = {
610
636
  into,
@@ -612,25 +638,103 @@ export async function createCodegen(module) {
612
638
  entries: [],
613
639
  replayed: 0,
614
640
  word: 0,
641
+ verifying: false,
615
642
  restored: new Map(),
616
- abcs: null,
643
+ abcs: [],
644
+ given: 0,
645
+ revivals: carried,
646
+ evicted: new Set([...journalDomains.keys()].filter((d) => wasm.domainState(d) === 1)),
617
647
  phase: "load",
618
648
  catchUp: 0,
619
649
  };
620
650
  moves.started++;
621
651
  return true;
622
652
  };
653
+ // `saved` with `carried` revived by its load, their ABCs' bytes joined,
654
+ // each once. A revival's ABCs were gathered when it was asked for: those
655
+ // of domains revived since, live in the snapshot, which holds them, are
656
+ // left out, else the load would take one twice and refuse the move. The
657
+ // chain's states here are the snapshot's, for a snapshot taken now and for
658
+ // one a revival is folded into (see scheduleRevivals).
659
+ const withRevivals = (saved, carried) => {
660
+ if (!carried.length) {
661
+ return saved;
662
+ }
663
+ const domains = [...(saved.revive?.domains ?? [])];
664
+ const abcs = new Map(saved.revive?.abcs ?? []);
665
+ for (const r of carried) {
666
+ // One an earlier revival's chain takes in is revived by that one's load.
667
+ if (!domains.some((d) => inChain(r.appDomain, d))) {
668
+ domains.push(r.appDomain);
669
+ }
670
+ for (const [index, bytes] of r.abcs) {
671
+ if (!abcs.has(index) && !saved.abcs.has(index) && evictedAbc(index)) {
672
+ abcs.set(index, bytes);
673
+ }
674
+ }
675
+ }
676
+ return { state: saved.state, abcs: saved.abcs, revive: { domains, abcs } };
677
+ };
678
+ const evictedAbc = (index) => Number.isInteger(index) &&
679
+ index >= 0 &&
680
+ index < abcDomains.length &&
681
+ wasm.domainState(abcDomains[index]) === 1;
682
+ // Domain `appDomain` and its evicted ancestors, the last a child of a live one.
683
+ const evictedChain = (appDomain) => {
684
+ const chain = new Set();
685
+ for (let d = appDomain; d > 0 && wasm.domainState(d) === 1; d = wasm.domainParent(d)) {
686
+ chain.add(d);
687
+ }
688
+ return chain;
689
+ };
690
+ // The domains the load of `m` revived, live in its spare, caught up, that
691
+ // this instance evicts and no revival still wants: those between a
692
+ // revival's domain and its chain's top, the domain dropped since. Evicted
693
+ // in the spare too before it takes this instance's place, else the move
694
+ // would leave them live where a compiler that never moved has them
695
+ // evicted; in order, a parent before its children, which it evicts.
696
+ const unwantedRevived = (m) => {
697
+ if (!m.revivals.length) {
698
+ return [];
699
+ }
700
+ const wanted = new Set();
701
+ for (const r of m.revivals) {
702
+ for (const d of evictedChain(r.appDomain)) {
703
+ wanted.add(d);
704
+ }
705
+ }
706
+ const unwanted = [];
707
+ for (const d of [...journalDomains.keys()].sort((x, y) => x - y)) {
708
+ if (d > 0 &&
709
+ !wanted.has(d) &&
710
+ wasm.domainState(d) === 1 &&
711
+ m.into.domainState(d) === 0 &&
712
+ !unwanted.some((u) => inChain(u, d))) {
713
+ unwanted.push(d);
714
+ }
715
+ }
716
+ return unwanted;
717
+ };
623
718
  // The spare, rebuilt and caught up, takes this instance's place, as a
624
719
  // recovery's does, and the snapshot it rebuilt is the checkpoint, the
625
720
  // calls since it the journal.
626
721
  const swapMove = (m) => {
627
722
  move = null;
628
723
  moves.completed++;
724
+ moves.revived += m.revivals.length;
725
+ for (const r of m.revivals) {
726
+ const at = revivals.indexOf(r);
727
+ if (at >= 0) {
728
+ revivals.splice(at, 1);
729
+ }
730
+ }
629
731
  const kept = m.entries.filter((entry) => journal.has(entry));
630
732
  moves.lastCaughtUp = kept.length;
631
733
  moves.lastSavedBytes = m.saved.state.length;
632
- for (const abc of m.saved.abcs.values()) {
633
- moves.lastSavedBytes += abc.length;
734
+ for (const abcs of [m.saved.abcs, m.saved.revive?.abcs ?? new Map()]) {
735
+ for (const abc of abcs.values()) {
736
+ moves.lastSavedBytes += abc.length;
737
+ }
634
738
  }
635
739
  journal.clear();
636
740
  restores.clear();
@@ -668,19 +772,28 @@ export async function createCodegen(module) {
668
772
  abortMove("refused");
669
773
  return "none";
670
774
  }
671
- m.abcs = m.saved.abcs.entries();
775
+ // domainLoad's MOVE_Limit counts the snapshot alone: the chains
776
+ // revived after it add their ABCs at their weight, three times
777
+ // their bytes, small beside the limit's gigabyte.
778
+ for (const domain of m.saved.revive?.domains ?? []) {
779
+ if (into.domainLoadRevive(domain) !== 0) {
780
+ abortMove("refused");
781
+ return "none";
782
+ }
783
+ }
784
+ m.abcs = [...m.saved.abcs, ...(m.saved.revive?.abcs ?? [])];
672
785
  m.phase = "abcs";
673
786
  return "moving";
674
787
  case "abcs": {
675
- const next = m.abcs?.next();
676
- if (next && !next.done) {
677
- if (into.domainLoadAbc(next.value[0], next.value[1]) !== 0) {
788
+ if (m.given < m.abcs.length) {
789
+ const [index, bytes] = m.abcs[m.given++];
790
+ if (into.domainLoadAbc(index, bytes) !== 0) {
678
791
  abortMove("refused");
679
792
  return "none";
680
793
  }
681
794
  return "moving";
682
795
  }
683
- m.abcs = null;
796
+ m.abcs = [];
684
797
  if (!into.domainLoadRebuild()) {
685
798
  abortMove("refused");
686
799
  return "none";
@@ -703,23 +816,42 @@ export async function createCodegen(module) {
703
816
  // Entries of domains dropped since are out of the journal (see
704
817
  // forgetDropped), but one half replayed is finished.
705
818
  while (m.word === 0 &&
819
+ !m.verifying &&
706
820
  m.replayed < m.entries.length &&
707
821
  !journal.has(m.entries[m.replayed])) {
708
822
  m.replayed++;
709
823
  }
710
824
  if (m.replayed === m.entries.length) {
825
+ const unwanted = unwantedRevived(m);
826
+ for (const domain of unwanted) {
827
+ into.domainEvict(domain);
828
+ }
711
829
  swapMove(m);
830
+ // Owned by the root, as evictDomain's: a revival journaled later
831
+ // needs it though the domain is dropped after.
832
+ for (const domain of unwanted) {
833
+ journaled({ kind: "evict", appDomain: domain });
834
+ }
712
835
  return "done";
713
836
  }
714
- // A compile's log a log entry a part, a run of them taking long.
837
+ // A compile's log a log entry a part, a run of them taking long,
838
+ // and a verification, as long as the compile's, a part of about a
839
+ // rebuild's part at a time, the entries after it waiting.
715
840
  const entry = m.entries[m.replayed];
716
841
  if (entry.kind === "log") {
717
842
  const w = entry.words;
718
843
  const i = m.word;
719
- if (!into.replayEntry(w[i], w[i + 1], w[i + 2], w[i + 3])) {
844
+ const step = m.verifying
845
+ ? into.replayEntryStep()
846
+ : into.replayEntryStepped(w[i], w[i + 1], w[i + 2], w[i + 3]);
847
+ if (step < 0) {
720
848
  abortMove("refused");
721
849
  return "none";
722
850
  }
851
+ m.verifying = step === 0;
852
+ if (m.verifying) {
853
+ return "moving";
854
+ }
723
855
  m.word = i + 4 < w.length ? i + 4 : 0;
724
856
  if (m.word === 0) {
725
857
  m.replayed++;
@@ -741,17 +873,12 @@ export async function createCodegen(module) {
741
873
  return "none";
742
874
  }
743
875
  };
744
- // Whether the move's next part replays a call that takes long in one:
745
- // a compile's verification of a large ABC, as long as the compile
746
- // verified it for, or the add of a large ABC, which parses and links it.
876
+ // The length of the ABC whose add the move's next part replays if that
877
+ // takes long in one call, parsing and linking it, else 0. A compile's
878
+ // verification is replayed a part at a time.
747
879
  const longNext = (m) => {
748
880
  const entry = m.phase === "catch up" ? m.entries[m.replayed] : undefined;
749
- if (entry?.kind === "add") {
750
- return entry.abc.length > LARGE_Add;
751
- }
752
- return (entry?.kind === "log" &&
753
- entry.words[m.word] === LOG_Verify &&
754
- (abcLengths[entry.words[m.word + 1]] ?? 0) > LARGE_Verify);
881
+ return entry?.kind === "add" && entry.abc.length > LARGE_Add ? entry.abc.length : 0;
755
882
  };
756
883
  // What compactStep starts, as compact decides, but that a large domain
757
884
  // moves rather than rebuild in place, which would take one call.
@@ -785,6 +912,158 @@ export async function createCodegen(module) {
785
912
  spare = null;
786
913
  return startMove(into) ? "moving" : "none";
787
914
  };
915
+ // The revivals left to a move whose domains are still evicted here, those
916
+ // revived otherwise or dropped since let go of, as are those whose chain
917
+ // has grown by an ancestor evicted since, which the host, asking again,
918
+ // gives the bytes of; and the revivals waiting for a move, failed ones apart.
919
+ const waitingRevivals = () => {
920
+ for (let i = revivals.length - 1; i >= 0; i--) {
921
+ const r = revivals[i];
922
+ if (wasm.domainState(r.appDomain) !== 1 || !givenAll(r)) {
923
+ revivals.splice(i, 1);
924
+ }
925
+ }
926
+ return revivals.filter((r) => !r.failed && !move?.revivals.includes(r));
927
+ };
928
+ const givenAll = (r) => {
929
+ const chain = evictedChain(r.appDomain);
930
+ for (let index = 0; index < abcDomains.length; index++) {
931
+ if (chain.has(abcDomains[index]) && !r.abcs.has(index)) {
932
+ return false;
933
+ }
934
+ }
935
+ return true;
936
+ };
937
+ // Whether the chain of `r` is not what it was at the snapshot of `m`, an
938
+ // ancestor evicted or revived since, by a revival of its own or of
939
+ // another under it: its load would then revive a chain other than the one
940
+ // here, with ABCs of it not given, or the spare replay a revival or an
941
+ // eviction its load had made moot.
942
+ const chainChanged = (m, r) => {
943
+ if (m.entries.some((e) => (e.kind === "evict" || e.kind === "revive") && inChain(e.appDomain, r.appDomain))) {
944
+ return true;
945
+ }
946
+ const now = evictedChain(r.appDomain);
947
+ let d = r.appDomain;
948
+ for (; d > 0 && m.evicted.has(d); d = wasm.domainParent(d)) {
949
+ if (!now.delete(d)) {
950
+ return true;
951
+ }
952
+ }
953
+ return now.size > 0;
954
+ };
955
+ // Revivals waiting, given to the move under way while its spare has not
956
+ // begun to rebuild, as its load revives them, unless their chain changed
957
+ // since its snapshot; else, if it only compacts, the move is given up for
958
+ // one that revives as it compacts, as going on would cost its rest and
959
+ // then a rebuild; those a move that revives cannot take wait for the next.
960
+ const scheduleRevivals = (waiting) => {
961
+ const m = move;
962
+ if (m === null || !waiting.length) {
963
+ return;
964
+ }
965
+ const fresh = m.phase === "load" || m.phase === "abcs" ? waiting.filter((r) => !chainChanged(m, r)) : [];
966
+ if (fresh.length < waiting.length && !m.revivals.length) {
967
+ abortMove("superseded");
968
+ return;
969
+ }
970
+ if (!fresh.length) {
971
+ return;
972
+ }
973
+ const loaded = m.phase === "abcs";
974
+ const before = m.saved.revive;
975
+ const saved = withRevivals(m.saved, fresh);
976
+ try {
977
+ for (const domain of saved.revive?.domains ?? []) {
978
+ if (loaded && !before?.domains.includes(domain) && m.into.domainLoadRevive(domain) !== 0) {
979
+ for (const r of fresh) {
980
+ r.failed = true;
981
+ }
982
+ abortMove("refused");
983
+ return;
984
+ }
985
+ }
986
+ }
987
+ catch (error) {
988
+ for (const r of fresh) {
989
+ r.failed = true;
990
+ }
991
+ abortMove(error instanceof CodegenPanic || error instanceof CodegenOutOfMemory ? "spare" : "refused");
992
+ return;
993
+ }
994
+ m.revivals.push(...fresh);
995
+ m.saved = saved;
996
+ if (loaded) {
997
+ for (const entry of saved.revive?.abcs ?? []) {
998
+ if (!before?.abcs.has(entry[0])) {
999
+ m.abcs.push(entry);
1000
+ }
1001
+ }
1002
+ }
1003
+ };
1004
+ // A move that revives the domains waiting, and compacts as it does, once
1005
+ // there is a spare to make it in; where none can be made, or the journal
1006
+ // cannot catch one up, they are done in place when asked for again.
1007
+ const beginRevival = (waiting) => {
1008
+ const fail = () => {
1009
+ for (const r of waiting) {
1010
+ r.failed = true;
1011
+ }
1012
+ return "none";
1013
+ };
1014
+ if (!replayable) {
1015
+ return fail();
1016
+ }
1017
+ const into = spare;
1018
+ if (into === null) {
1019
+ if (spareFailed && !making) {
1020
+ spareFailed = false;
1021
+ return fail();
1022
+ }
1023
+ prepare();
1024
+ return "moving";
1025
+ }
1026
+ spare = null;
1027
+ return startMove(into, waiting) ? "moving" : fail();
1028
+ };
1029
+ // The ABCs of evicted domain `appDomain` and its evicted ancestors, the
1030
+ // first a child of a live one, among `abcs`, copied: each must be there,
1031
+ // else the revival throws, as reviveDomain does when it cannot revive.
1032
+ const revivalAbcs = (appDomain, abcs) => {
1033
+ const chain = evictedChain(appDomain);
1034
+ const top = [...chain].pop() ?? appDomain;
1035
+ let missing = false;
1036
+ const owned = new Map();
1037
+ for (let index = 0; index < abcDomains.length; index++) {
1038
+ if (chain.has(abcDomains[index])) {
1039
+ const bytes = abcs.get(index);
1040
+ if (bytes === undefined) {
1041
+ missing = true;
1042
+ }
1043
+ else {
1044
+ owned.set(index, new Uint8Array(bytes));
1045
+ }
1046
+ }
1047
+ }
1048
+ if (missing || wasm.domainState(wasm.domainParent(top)) !== 0) {
1049
+ throw new Error(`application domain ${appDomain}: not evicted, its parent not live, or an ABC of it not given`);
1050
+ }
1051
+ return owned;
1052
+ };
1053
+ // Whether the domain, with the chain a revival links again, its ABCs
1054
+ // `abcs`, is large enough that the revival's rebuild goes to a move
1055
+ // rather than block: a small domain reviving a large chain rebuilds as
1056
+ // long as one that was large all along.
1057
+ const revivalSteps = (abcs) => {
1058
+ if (!replayable || moveRefused) {
1059
+ return false;
1060
+ }
1061
+ let weight = Number(wasm.domainUsage().split(" ")[0]);
1062
+ for (const bytes of abcs.values()) {
1063
+ weight += REVIVED_Weight * bytes.length;
1064
+ }
1065
+ return weight >= IN_PLACE_Live;
1066
+ };
788
1067
  // Whether the domain was lost to a panic recovery could not undo (see fresh).
789
1068
  let lost = false;
790
1069
  const api = {
@@ -799,6 +1078,7 @@ export async function createCodegen(module) {
799
1078
  reset(version = API_VERSION) {
800
1079
  checkApiVersion(version);
801
1080
  abortMove("superseded");
1081
+ revivals.length = 0;
802
1082
  moveRefused = false;
803
1083
  retryAt = 0;
804
1084
  wasm.domainReset(version);
@@ -811,7 +1091,6 @@ export async function createCodegen(module) {
811
1091
  journal.clear();
812
1092
  restores.clear();
813
1093
  abcDomains.length = 0;
814
- abcLengths.length = 0;
815
1094
  replayable = true;
816
1095
  checkpoint = null;
817
1096
  },
@@ -827,7 +1106,6 @@ export async function createCodegen(module) {
827
1106
  if (error === 0 || wasm.lastAddLinked()) {
828
1107
  if (error === 0) {
829
1108
  abcDomains.push(appDomain);
830
- abcLengths.push(bytes.length);
831
1109
  }
832
1110
  journaled({
833
1111
  kind: "add",
@@ -917,39 +1195,68 @@ export async function createCodegen(module) {
917
1195
  return wasm.domainCompact(true) && rebuild(wasm, null, true);
918
1196
  },
919
1197
  compactStep(deadline) {
1198
+ const waiting = waitingRevivals();
1199
+ scheduleRevivals(waiting);
920
1200
  let m = move;
921
1201
  if (m === null) {
922
- const begun = beginCompaction();
1202
+ const begun = waiting.length ? beginRevival(waiting) : beginCompaction();
923
1203
  if (begun !== "moving" || move === null) {
924
1204
  return begun;
925
1205
  }
926
1206
  m = move;
927
1207
  }
928
1208
  // Catch-up chases what the host goes on doing. Past CATCH_UP_Slices
929
- // slices it goes on CATCH_UP_Overrun past the deadline, a long part
930
- // still starting a slice of its own; past twice as many, it finishes
931
- // in this slice, which has only what came in since the last to
932
- // replay (a revival's rebuild still a part at a time).
1209
+ // slices, a slice whose deadline is sooner goes on starting parts
1210
+ // until CATCH_UP_Overrun from its start, and past twice as many
1211
+ // until CATCH_UP_OverrunLate, so a busy page's short slices catch up
1212
+ // as an idle page's do, a long part still starting a slice of its
1213
+ // own. Never longer: replaying a compile costs no more than the
1214
+ // compile did, so catch-up falls behind only while the host spends
1215
+ // more of its time compiling than that, and catches up once its
1216
+ // loads pause, rather than block for all it is behind by.
933
1217
  let slices = 0;
934
1218
  if (m.phase === "catch up") {
935
1219
  slices = ++m.catchUp;
936
1220
  }
1221
+ const start = deadline.timeRemaining();
1222
+ let read = start;
1223
+ let unchanged = 0;
937
1224
  for (;;) {
938
1225
  const progress = moveUnit(m);
939
1226
  if (progress !== "moving") {
940
1227
  return progress;
941
1228
  }
942
- const catchingUp = m.phase === "catch up";
943
- if (catchingUp && slices > CATCH_UP_Slices * 2) {
944
- continue;
1229
+ // SLICE_Parts bounds a deadline that has stopped telling time, one
1230
+ // that reads the same however many parts it is asked after.
1231
+ // Counting every part instead would cut slices of parts of a few
1232
+ // microseconds, as catch-up replays a compile's log, at a few
1233
+ // milliseconds.
1234
+ const left = deadline.timeRemaining();
1235
+ unchanged = left === read ? unchanged + 1 : 0;
1236
+ read = left;
1237
+ if (unchanged >= SLICE_Parts) {
1238
+ return "moving";
945
1239
  }
946
- // A part that takes long in one call starts a slice of its own,
947
- // so that it adds to no other part's time.
948
- if (longNext(m)) {
1240
+ // A part that takes long in one call starts a slice of its own, so
1241
+ // that it adds to no other part's time, unless this one has the
1242
+ // time left for it: ending a slice tens of milliseconds early at
1243
+ // each large add left catch-up behind the host's loads.
1244
+ const adding = longNext(m);
1245
+ if (adding > 0 && !(left > adding / ADD_Rate)) {
949
1246
  return "moving";
950
1247
  }
951
- const overrun = catchingUp && slices > CATCH_UP_Slices ? CATCH_UP_Overrun : 0;
952
- if (deadline.timeRemaining() <= -overrun) {
1248
+ // A rebuild's parts start until REBUILD_Floor into a slice given
1249
+ // any time, so that a busy page's short slices still move it along.
1250
+ // That and catch-up's overrun go past the deadline only where it
1251
+ // tells the time past it, below 0: an IdleDeadline stops at 0. Not
1252
+ // by its falling, as a coarse clock reads the same across short
1253
+ // parts.
1254
+ const telling = left < 0;
1255
+ const overrun = m.phase === "catch up" &&
1256
+ slices > CATCH_UP_Slices &&
1257
+ left > start - (slices > 2 * CATCH_UP_Slices ? CATCH_UP_OverrunLate : CATCH_UP_Overrun);
1258
+ const floor = m.phase === "rebuild" && start > 0 && left > start - REBUILD_Floor;
1259
+ if (left <= 0 && !(telling && (overrun || floor))) {
953
1260
  return "moving";
954
1261
  }
955
1262
  }
@@ -957,7 +1264,35 @@ export async function createCodegen(module) {
957
1264
  isLive(appDomain) {
958
1265
  return wasm.domainState(appDomain) === 0;
959
1266
  },
960
- reviveDomain(appDomain, abcs) {
1267
+ reviveDomain(appDomain, abcs, stepped = false) {
1268
+ // A revival left to a move already, of this domain or a descendant,
1269
+ // which revives it too.
1270
+ const queued = revivals.find((r) => inChain(appDomain, r.appDomain));
1271
+ if (stepped && queued !== undefined && !queued.failed && replayable) {
1272
+ return false;
1273
+ }
1274
+ // One the move under way makes, finished here, as compact finishes it;
1275
+ // a revival given up is done in place below.
1276
+ const m = move;
1277
+ if (queued !== undefined && m?.revivals.includes(queued)) {
1278
+ let progress = moveUnit(m);
1279
+ while (progress === "moving") {
1280
+ progress = moveUnit(m);
1281
+ }
1282
+ if (wasm.domainState(appDomain) === 0) {
1283
+ return true;
1284
+ }
1285
+ }
1286
+ if (queued !== undefined && revivals.includes(queued)) {
1287
+ revivals.splice(revivals.indexOf(queued), 1);
1288
+ }
1289
+ if (stepped && queued === undefined && wasm.domainReviveNeeds(appDomain) === 1) {
1290
+ const owned = revivalAbcs(appDomain, abcs);
1291
+ if (revivalSteps(owned)) {
1292
+ revivals.push({ appDomain, abcs: owned, failed: false });
1293
+ return false;
1294
+ }
1295
+ }
961
1296
  // A move's spare lets go of the ABCs of the domains evicted before its
962
1297
  // snapshot, and before any rebuild a replayed revival makes in it,
963
1298
  // which this instance may still hold: restores journaled before the
@@ -972,6 +1307,14 @@ export async function createCodegen(module) {
972
1307
  let revived = wasm.domainRevive(appDomain, true);
973
1308
  if (revived < 0) {
974
1309
  for (const [index, bytes] of abcs) {
1310
+ // One of a domain live here, which a move revived after the host
1311
+ // gathered the chain's bytes, is held already.
1312
+ if (Number.isInteger(index) &&
1313
+ index >= 0 &&
1314
+ index < abcDomains.length &&
1315
+ wasm.domainState(abcDomains[index]) === 0) {
1316
+ continue;
1317
+ }
975
1318
  const owned = new Uint8Array(bytes);
976
1319
  const error = wasm.domainRestore(index, owned);
977
1320
  if (error !== 0) {
@@ -1011,6 +1354,7 @@ export async function createCodegen(module) {
1011
1354
  if (!done) {
1012
1355
  throw new Error(`application domain ${appDomain}: not evicted, its parent not live, or an ABC of it not given`);
1013
1356
  }
1357
+ return true;
1014
1358
  },
1015
1359
  found({ domain, nsKind, uri, name, abc, asType }) {
1016
1360
  wasm.domainFound(domain, nsKind, uri, name, abc, asType);
@@ -1236,21 +1580,47 @@ const WANT_Spare = 3;
1236
1580
  * rebuilds in place, in one call, rather than move a part at a time.
1237
1581
  */
1238
1582
  const IN_PLACE_Live = 4 << 20;
1239
- /** A compile's log entry that verified an ABC: LogKind.verify (avm2/link/domain.zig). */
1240
- const LOG_Verify = 4;
1583
+ // The weight of an ABC's byte a revival links again, before its compiles:
1584
+ // the compiler's own ABC_Weight (zig/src/avm2/link/domain.zig).
1585
+ const REVIVED_Weight = 3;
1241
1586
  /**
1242
- * The length of an ABC whose verification, replayed, starts a slice of its
1243
- * own: some 20 ms of it on real content, 750 ms for 2.3 MB.
1587
+ * The garbage a part of a move's rebuild, and of a verification its catch-up replays, makes at
1588
+ * most (domainRebuildBudget): some 5 ms of verifying. Halving it from 256 KiB cost no time in all.
1244
1589
  */
1245
- const LARGE_Verify = 64 << 10;
1246
- /** The garbage a part of a move's rebuild makes at most (domainRebuildBudget). */
1247
- const MOVE_PartBudget = 256 << 10;
1590
+ const MOVE_PartBudget = 128 << 10;
1248
1591
  /** The slices compactStep catches a move up in before it goes on past their deadlines (see compactStep). */
1249
1592
  const CATCH_UP_Slices = 32;
1250
- /** How far past its deadline a slice catches up after CATCH_UP_Slices, in milliseconds. */
1251
- const CATCH_UP_Overrun = 50;
1252
- /** The length of an ABC whose add, replayed, starts a slice of its own: some 4 ms of parsing and linking. */
1593
+ /**
1594
+ * How long after its start a slice starts catch-up's parts until, past CATCH_UP_Slices, in
1595
+ * milliseconds: a few parts, each some 5 ms at most since long methods are verified across parts.
1596
+ */
1597
+ const CATCH_UP_Overrun = 25;
1598
+ /**
1599
+ * As CATCH_UP_Overrun, past twice CATCH_UP_Slices: catch-up outrun that long still catches up
1600
+ * while the host spends up to about half its time compiling, where CATCH_UP_Overrun's would only
1601
+ * up to a quarter.
1602
+ */
1603
+ const CATCH_UP_OverrunLate = 50;
1604
+ /**
1605
+ * How long after its start a slice starts a move's rebuild parts until, past its deadline, in
1606
+ * milliseconds: the slice so takes this and one part more, of some 5 ms at most. With parts
1607
+ * that short, a busy page's slices of a few milliseconds would each take one, the move then
1608
+ * going on for many more loads, the dead ABCs it is to let go of meanwhile kept.
1609
+ */
1610
+ const REBUILD_Floor = 9;
1611
+ /**
1612
+ * The parts a slice makes at most while its deadline reads the same: more than a coarse clock's
1613
+ * tick holds of the shortest parts, a few microseconds each, so that only a deadline that stops
1614
+ * telling time meets it.
1615
+ */
1616
+ const SLICE_Parts = 1000;
1617
+ /** The length of an ABC whose add, replayed, starts a slice of its own unless the slice has time left for it: some 4 ms of parsing and linking. */
1253
1618
  const LARGE_Add = 256 << 10;
1619
+ /**
1620
+ * The bytes of a large add's ABC a slice reckons a millisecond of its time to replay: half
1621
+ * LARGE_Add's rate, so that one made within a slice still ends before its deadline.
1622
+ */
1623
+ const ADD_Rate = LARGE_Add / 4 / 2;
1254
1624
  /** The drops and evictions after a move given up as failed before compactStep starts another. */
1255
1625
  const RETRY_Lettings = 16;
1256
1626
  /** How many SWFs whose blocks or units are compiled codegen holds a copy of at most. */