diff --git a/docs/results/collect-capacity-v1-kernels.txt b/docs/results/collect-capacity-v1-kernels.txt new file mode 100644 index 000000000..8c49c42cf --- /dev/null +++ b/docs/results/collect-capacity-v1-kernels.txt @@ -0,0 +1,13 @@ +load median 3.56 max 4.27 +records base wall med 1.699s cycles med 7.195G + cap /base wall 1.0072 (0.9869-1.0165) cycles 1.0066 (0.9891-1.0193) instr 1.0000 (0.9999-1.0002) rss 1.0000 (1.0000-1.0000) +collections base wall med 2.074s cycles med 8.801G + cap /base wall 1.0232 (1.0173-1.0367) cycles 1.0226 (1.0214-1.0276) instr 0.9962 (0.9961-0.9963) rss 0.9974 (0.9955-1.0015) +strings base wall med 0.840s cycles med 3.535G + cap /base wall 0.9973 (0.9911-1.0209) cycles 0.9961 (0.9873-1.0211) instr 0.9940 (0.9939-0.9942) rss 1.0021 (0.9992-1.0042) +serialization base wall med 1.082s cycles med 4.646G + cap /base wall 0.9837 (0.9630-1.0011) cycles 0.9807 (0.9703-0.9953) instr 0.9879 (0.9877-0.9880) rss 1.0000 (0.9991-1.0024) +pipelines base wall med 1.119s cycles med 4.74G + cap /base wall 0.9920 (0.9789-1.0118) cycles 0.9924 (0.9828-1.0015) instr 0.9955 (0.9954-0.9956) rss 1.0000 (1.0000-1.0000) +closures base wall med 0.248s cycles med 1.049G + cap /base wall 0.9894 (0.9814-1.0169) cycles 0.9839 (0.9793-1.0032) instr 0.9907 (0.9902-0.9910) rss 1.0009 (1.0004-1.0031) diff --git a/docs/results/collect-capacity-v1-v7.txt b/docs/results/collect-capacity-v1-v7.txt new file mode 100644 index 000000000..57b7a4d6e --- /dev/null +++ b/docs/results/collect-capacity-v1-v7.txt @@ -0,0 +1,30 @@ +main-base 71.47 s 325233943208 cycles 1621989852566 instructions 2227814400 RSS +main-cap 68.45 s 322785339929 cycles 1615624832823 instructions 2226503680 RSS +main-base 66.59 s 318223495362 cycles 1621751918495 instructions 2226814976 RSS +main-cap 66.19 s 316444984438 cycles 1615281284475 instructions 2226913280 RSS +main-base 70.71 s 328220051345 cycles 1622243089211 instructions 2227683328 RSS +main-cap 70.64 s 328361524448 cycles 1615970883309 instructions 2227224576 RSS +main-base 66.33 s 318481917705 cycles 1621693994841 instructions 2227044352 RSS +main-cap 85.46 s 325425159431 cycles 1615136478203 instructions 2226864128 RSS +main-base 64.82 s 313506646407 cycles 1620970673476 instructions 2225815552 RSS +main-cap 83.14 s 325668714523 cycles 1614524190707 instructions 2226962432 RSS +main-base 64.65 s 312072583504 cycles 1620670394576 instructions 2226896896 RSS +main-cap 63.75 s 309209382263 cycles 1614274788064 instructions 2226880512 RSS +main-base 64.90 s 314375135738 cycles 1621460348766 instructions 2227683328 RSS +main-cap 66.19 s 317261019750 cycles 1615281494965 instructions 2226831360 RSS +main-base 70.59 s 329570311063 cycles 1622445629540 instructions 2227929088 RSS +main-cap 68.08 s 324169460937 cycles 1615972882605 instructions 2227273728 RSS +main-base 71.19 s 329223812998 cycles 1622337384097 instructions 2227388416 RSS +main-cap 68.58 s 323990977062 cycles 1615484394897 instructions 2226946048 RSS +main-base 72.29 s 330798277870 cycles 1622184939726 instructions 2225274880 RSS +main-cap 69.12 s 325637306910 cycles 1615670155788 instructions 2227585024 RSS +main-base 67.22 s 323698354115 cycles 1622045548627 instructions 2227650560 RSS +main-cap 67.11 s 319371897369 cycles 1615538828005 instructions 2227650560 RSS +main-base 66.81 s 319857886128 cycles 1621810305253 instructions 2226798592 RSS +main-cap 69.38 s 326984155798 cycles 1615936273743 instructions 2226536448 RSS +main-base 70.33 s 331662081195 cycles 1622558330324 instructions 2226339840 RSS +main-cap 69.06 s 326772509531 cycles 1615697598932 instructions 2226274304 RSS +main-base 85.70 s 343110350304 cycles 1622351019540 instructions 2227699712 RSS +main-cap 67.27 s 320122370665 cycles 1615562179735 instructions 2226896896 RSS +main-base 86.33 s 342385642619 cycles 1622579963061 instructions 2227142656 RSS +main-cap 74.60 s 333534956525 cycles 1616711911368 instructions 2225782784 RSS diff --git a/docs/results/collect-capacity-v1.md b/docs/results/collect-capacity-v1.md new file mode 100644 index 000000000..25fbf020b --- /dev/null +++ b/docs/results/collect-capacity-v1.md @@ -0,0 +1,112 @@ +# Collection costs off empty capacity + +24 November 2026, macOS arm64 (M5 Max). The fix is `7fb69dd25` on main +`183ec7044`, with `ceead4773` adapting `verify-heap-tools` to it. The +investigation looked for the fixed cost of a generational collection: work +that scales with heap capacity rather than with live data. + +## Breakdown + +A scratch timing patch +([worker-runtime-collection-costs-v1-timing.patch](worker-runtime-collection-costs-v1-timing.patch)) +timed each collection phase with the host clock. It is not part of any image. +The program churned 4, 32 and 256 MiB nurseries with serial and parallel +marking, holding 256 small arrays live, then forced a major collection with +the nursery half full and another with it empty. The records are in +[worker-runtime-collection-costs-v1-phases.txt](worker-runtime-collection-costs-v1-phases.txt). +They were measured on the Worker stream's branch (`5e489f8f0`), whose +collector code is the same as main's. + +Two costs scaled with capacity: + +- **Forced major, nursery not empty.** The compactor covered the whole span + up to old capacity plus the young top, including the untouched gap above + the old fill. Its live-bit clear, offset pass, forwarding and packing ran + over every bitmap word: 4.0 M words at 256 MiB, against 129 words for the + same live data with the nursery empty. With 32 KB live, it took 43 ms at + 256 MiB (parallel marking) and 4.9 ms at 32 MiB. +- **Bitmap clears.** The nursery's start bits (every minor collection), the + compactor's live bits and the dirty table were cleared with `fill`, which + stores element by element. The nursery clear took about 20 µs per minor + collection at 4 MiB, 155 µs at 32 MiB and 1.3 ms at 256 MiB. + +Everything else per collection was small and did not scale with capacity. +Holding and releasing the collector and clearing the remembered set took at +most a few microseconds each, after a one-time hold of about 0.1 ms at the +first collection. + +## Fix + +- A major collection while the nursery holds objects first evacuates the + nursery (an ordinary minor collection), then compacts only the packed old + prefix. A nursery whose survivors cannot fit the old prefix still compacts + in place. The pages released afterwards are the same. +- Bitmap clears use `set` from a permanent zero block, one copy per 2 KiB. + Typed `fill` codegen is a separate item for the codegen stream. + +## Results + +**Forced majors** (scratch probe, 15 repetitions: 3 runs of 5; records in +[worker-runtime-collection-costs-v1-e2e.txt](worker-runtime-collection-costs-v1-e2e.txt)), +median ms: + +| Nursery | Major, nursery half full | Major, nursery empty | 12 nurseries of minor churn | +| ------- | ------------------------ | -------------------- | --------------------------- | +| 4 MiB | 0.63 → 0.16 (−75%) | 0.069 → 0.061 | 16.6 → 16.7 (+0.3%) | +| 32 MiB | 4.05 → 0.50 (−88%) | 0.072 → 0.063 | 131.2 → 126.8 (−3.3%) | +| 256 MiB | 31.5 → 2.35 (−93%) | 0.075 → 0.067 | 1062 → 1037 (−2.4%) | + +**Frozen v7** (`--runtime-tree` images of main `183ec7044` and of the fix, +15 paired rounds, base first in each; load median 4.6, max 13.6; runs in +[collect-capacity-v1-v7.txt](collect-capacity-v1-v7.txt)): neutral to +slightly better. + +| Metric | Base median | Fix median | Paired fix/base: median (quartiles) | Fix lower in | +| ------------ | ----------- | ---------- | ----------------------------------- | ------------ | +| Cycles | 325.2 G | 324.2 G | 0.9908 (0.9841–1.0092) | 10/15 | +| Wall | 70.33 s | 68.58 s | 0.9861 (0.9577–1.0199) | 11/15 | +| Instructions | 1622.0 G | 1616.0 G | 0.9960 (0.9960–0.9961) | 15/15 | +| Peak RSS | 2.227 GB | 2.227 GB | 0.9999 | 11/15 | + +**Allocating feature kernels** (native images of main `183ec7044` and of +the fix, 15 rounds with the order rotating; load median 3.6, max 4.3; via +`/usr/bin/time -l`; results in +[collect-capacity-v1-kernels.txt](collect-capacity-v1-kernels.txt)). +Ratios are fix/base per round: median (quartiles). + +| Kernel | Base cycles | Cycles | Wall | Instructions | +| ------------- | ----------- | ---------------------- | ---------------------- | ------------ | +| records | 7.20 G | 1.0066 (0.9891–1.0193) | 1.0072 (0.9869–1.0165) | 1.0000 | +| collections | 8.80 G | 1.0226 (1.0214–1.0276) | 1.0232 (1.0173–1.0367) | 0.9962 | +| strings | 3.54 G | 0.9961 (0.9873–1.0211) | 0.9973 (0.9911–1.0209) | 0.9940 | +| serialization | 4.65 G | 0.9807 (0.9703–0.9953) | 0.9837 (0.9630–1.0011) | 0.9879 | +| pipelines | 4.74 G | 0.9924 (0.9828–1.0015) | 0.9920 (0.9789–1.0118) | 0.9955 | +| closures | 1.05 G | 0.9839 (0.9793–1.0032) | 0.9894 (0.9814–1.0169) | 0.9907 | + +**Collections: +2.3% cycles, not in the collector.** The kernel keeps +almost nothing live in a 32 MiB nursery, so it runs only minor collections. +The only fix it reaches is the faster nursery clear, and it retires 0.4% +fewer instructions. pprofessor profiles of debug-symbol builds (5 runs each, +999 Hz, all threads) put the allocator and collector at a median 15 ms per +run on main and 8 ms with the fix, about 1% of the 1.5 s main thread. The +extra cycles are in mutator code (map and set operations) that the fix does +not change. The measured image's text is exactly one page (4096 bytes) +larger, and in the debug-symbol builds the program's functions moved by +exactly 4096 bytes: each keeps its in-page alignment but not its page. The regression is consistent with a page-level placement +effect (instruction cache or branch predictor aliasing), not proven. The +stacked start-bitmap PR measures collections at 0.968 of this fix, about +1% below main. + +**Gates on main** (fix `7fb69dd25`, tools `ceead4773`): + +- Collector native fixtures, 16/16, under LLDB: compaction (small and large + heaps), granules, released semispaces, stack maps, weak cells on the + copying collector, the generational collector and its parallel paths, + safe-for-space and block forwarding. +- `verify-heap-tools`: passes after `ceead4773`. The collector has no + `if (minor)` line any more, and inside `collect` the young and old tops die + at the evacuation call, so LLDB cannot read them there. The tool now breaks + in `evacuateNursery` after its first call, where both are homed and nothing + has moved yet. +- Native compiler subset check; Node-built stage 1, then native stages 2 + and 3: stage2 == stage3. diff --git a/docs/results/worker-runtime-collection-costs-v1-e2e.txt b/docs/results/worker-runtime-collection-costs-v1-e2e.txt new file mode 100644 index 000000000..e0d0e15b6 --- /dev/null +++ b/docs/results/worker-runtime-collection-costs-v1-e2e.txt @@ -0,0 +1,111 @@ +Program-level timings (scratch probe, 15 repetitions each: 3 runs x 5): churn = 12 nurseries of +short-lived allocation with 256 live arrays; then a forced major with the nursery half full, then one +with it empty. base = 5e489f8f0, new = 29cbe418b. +== base-256-1 +E2E churn 1024.6192500591278 majorFull 35.26316595077515 majorEmpty 0.08500003814697266 +E2E churn 1027.7944161891937 majorFull 31.473000049591064 majorEmpty 0.10108304023742676 +E2E churn 1009.0022501945496 majorFull 30.111250162124634 majorEmpty 0.07474994659423828 +E2E churn 1007.4452500343323 majorFull 31.139874935150146 majorEmpty 0.08470797538757324 +E2E churn 998.1640000343323 majorFull 31.5853750705719 majorEmpty 0.07154083251953125 +== base-256-2 +E2E churn 1068.8254170417786 majorFull 35.673750162124634 majorEmpty 0.08691596984863281 +E2E churn 1061.665209054947 majorFull 30.54183292388916 majorEmpty 0.07208418846130371 +E2E churn 1070.5081250667572 majorFull 32.46141719818115 majorEmpty 0.08424997329711914 +E2E churn 1056.564374923706 majorFull 32.08633303642273 majorEmpty 0.10220909118652344 +E2E churn 1058.7056670188904 majorFull 31.38824987411499 majorEmpty 0.07129216194152832 +== base-256-3 +E2E churn 1082.8625419139862 majorFull 34.66204214096069 majorEmpty 0.08487486839294434 +E2E churn 1072.4947090148926 majorFull 31.650458097457886 majorEmpty 0.07450008392333984 +E2E churn 1066.7251670360565 majorFull 30.4858341217041 majorEmpty 0.07420802116394043 +E2E churn 1068.3137090206146 majorFull 30.176292181015015 majorEmpty 0.07004094123840332 +E2E churn 1074.4403331279755 majorFull 31.02466607093811 majorEmpty 0.07145905494689941 +== base-32-1 +E2E churn 134.03366708755493 majorFull 4.416583061218262 majorEmpty 0.0747079849243164 +E2E churn 130.81637501716614 majorFull 4.020792007446289 majorEmpty 0.06941604614257812 +E2E churn 132.85237503051758 majorFull 4.364042043685913 majorEmpty 0.07629108428955078 +E2E churn 131.7294580936432 majorFull 4.050374984741211 majorEmpty 0.07233309745788574 +E2E churn 130.9603750705719 majorFull 3.883542060852051 majorEmpty 0.07262492179870605 +== base-32-2 +E2E churn 134.00999999046326 majorFull 4.337291955947876 majorEmpty 0.07350015640258789 +E2E churn 131.09108304977417 majorFull 4.126166105270386 majorEmpty 0.06883406639099121 +E2E churn 128.92891693115234 majorFull 3.8962080478668213 majorEmpty 0.0696249008178711 +E2E churn 133.8891670703888 majorFull 4.078290939331055 majorEmpty 0.06587505340576172 +E2E churn 131.88062500953674 majorFull 3.851541042327881 majorEmpty 0.07158398628234863 +== base-32-3 +E2E churn 134.86504101753235 majorFull 5.196749925613403 majorEmpty 0.0779581069946289 +E2E churn 131.15574979782104 majorFull 3.977250099182129 majorEmpty 0.07120800018310547 +E2E churn 131.08087491989136 majorFull 4.132499933242798 majorEmpty 0.06829118728637695 +E2E churn 129.88679194450378 majorFull 4.040374994277954 majorEmpty 0.07404208183288574 +E2E churn 129.58025002479553 majorFull 4.031875133514404 majorEmpty 0.0728750228881836 +== base-4-1 +E2E churn 17.691582918167114 majorFull 0.8263750076293945 majorEmpty 0.07004189491271973 +E2E churn 16.24524998664856 majorFull 0.6299169063568115 majorEmpty 0.06399989128112793 +E2E churn 15.735167026519775 majorFull 0.617833137512207 majorEmpty 0.07195782661437988 +E2E churn 15.846082925796509 majorFull 0.6620829105377197 majorEmpty 0.06587505340576172 +E2E churn 15.733375072479248 majorFull 0.6607089042663574 majorEmpty 0.06808304786682129 +== base-4-2 +E2E churn 17.00012516975403 majorFull 0.679542064666748 majorEmpty 0.07112503051757812 +E2E churn 16.61074995994568 majorFull 0.5891249179840088 majorEmpty 0.06904220581054688 +E2E churn 15.722500085830688 majorFull 0.6162078380584717 majorEmpty 0.0672919750213623 +E2E churn 15.692749977111816 majorFull 0.6085829734802246 majorEmpty 0.06858301162719727 +E2E churn 16.214416980743408 majorFull 0.628291130065918 majorEmpty 0.06749987602233887 +== base-4-3 +E2E churn 16.963000059127808 majorFull 0.678333044052124 majorEmpty 0.08379197120666504 +E2E churn 17.018208980560303 majorFull 0.6832499504089355 majorEmpty 0.0748751163482666 +E2E churn 17.826541900634766 majorFull 0.656041145324707 majorEmpty 0.06712484359741211 +E2E churn 16.729333877563477 majorFull 0.6205830574035645 majorEmpty 0.06891703605651855 +E2E churn 16.636374950408936 majorFull 0.6031248569488525 majorEmpty 0.06766700744628906 +== new-256-1 +E2E churn 1044.8307919502258 majorFull 2.464207887649536 majorEmpty 0.07566690444946289 +E2E churn 1036.5117919445038 majorFull 2.3963329792022705 majorEmpty 0.07212495803833008 +E2E churn 1014.0035829544067 majorFull 2.1167080402374268 majorEmpty 0.0779578685760498 +E2E churn 975.2532908916473 majorFull 2.2715840339660645 majorEmpty 0.06308293342590332 +E2E churn 984.6681251525879 majorFull 2.350749969482422 majorEmpty 0.07129192352294922 +== new-256-2 +E2E churn 1058.6283328533173 majorFull 2.244874954223633 majorEmpty 0.0787498950958252 +E2E churn 1064.7582919597626 majorFull 3.9417920112609863 majorEmpty 0.06462502479553223 +E2E churn 1090.7399580478668 majorFull 2.355333089828491 majorEmpty 0.07024979591369629 +E2E churn 1062.2755000591278 majorFull 2.2355000972747803 majorEmpty 0.0634160041809082 +E2E churn 1070.3177089691162 majorFull 2.3962080478668213 majorEmpty 0.06133389472961426 +== new-256-3 +E2E churn 1012.9264998435974 majorFull 2.1137919425964355 majorEmpty 0.06541705131530762 +E2E churn 1032.6995000839233 majorFull 2.4395840167999268 majorEmpty 0.06541585922241211 +E2E churn 1020.6865410804749 majorFull 2.307708978652954 majorEmpty 0.06725001335144043 +E2E churn 1012.7030000686646 majorFull 2.2821249961853027 majorEmpty 0.06641697883605957 +E2E churn 1081.551207780838 majorFull 2.777333974838257 majorEmpty 0.07754111289978027 +== new-32-1 +E2E churn 129.42283391952515 majorFull 0.2992079257965088 majorEmpty 0.06037497520446777 +E2E churn 131.33487510681152 majorFull 0.6031668186187744 majorEmpty 0.061875104904174805 +E2E churn 127.97525000572205 majorFull 0.493333101272583 majorEmpty 0.06120896339416504 +E2E churn 131.94787502288818 majorFull 0.32700014114379883 majorEmpty 0.06420779228210449 +E2E churn 131.55495810508728 majorFull 0.5009589195251465 majorEmpty 0.0603330135345459 +== new-32-2 +E2E churn 125.0002920627594 majorFull 0.32866716384887695 majorEmpty 0.0717918872833252 +E2E churn 123.98624992370605 majorFull 0.5138339996337891 majorEmpty 0.06291604042053223 +E2E churn 120.86558318138123 majorFull 0.5193328857421875 majorEmpty 0.061167001724243164 +E2E churn 126.39945793151855 majorFull 0.6007080078125 majorEmpty 0.06287503242492676 +E2E churn 120.97262501716614 majorFull 0.30808281898498535 majorEmpty 0.0696251392364502 +== new-32-3 +E2E churn 129.10524988174438 majorFull 0.32000017166137695 majorEmpty 0.06329083442687988 +E2E churn 126.8203752040863 majorFull 0.5315840244293213 majorEmpty 0.06104087829589844 +E2E churn 124.1677918434143 majorFull 0.3041248321533203 majorEmpty 0.06533408164978027 +E2E churn 127.94941687583923 majorFull 0.519291877746582 majorEmpty 0.060791969299316406 +E2E churn 124.77966690063477 majorFull 0.7738749980926514 majorEmpty 0.06379103660583496 +== new-4-1 +E2E churn 17.162999868392944 majorFull 0.15108299255371094 majorEmpty 0.060500144958496094 +E2E churn 16.47191596031189 majorFull 0.17133402824401855 majorEmpty 0.06137490272521973 +E2E churn 16.375499963760376 majorFull 0.13579201698303223 majorEmpty 0.06049990653991699 +E2E churn 16.392791986465454 majorFull 0.15854096412658691 majorEmpty 0.06054210662841797 +E2E churn 16.483749866485596 majorFull 0.15862512588500977 majorEmpty 0.06262493133544922 +== new-4-2 +E2E churn 16.6788330078125 majorFull 0.15204215049743652 majorEmpty 0.060957908630371094 +E2E churn 16.350375175476074 majorFull 0.16199994087219238 majorEmpty 0.06412506103515625 +E2E churn 16.676125049591064 majorFull 0.3078329563140869 majorEmpty 0.0782918930053711 +E2E churn 16.669041872024536 majorFull 0.15124988555908203 majorEmpty 0.060916900634765625 +E2E churn 16.52970814704895 majorFull 0.21083307266235352 majorEmpty 0.06120800971984863 +== new-4-3 +E2E churn 16.776583909988403 majorFull 0.1502079963684082 majorEmpty 0.06099987030029297 +E2E churn 16.822041988372803 majorFull 0.16558289527893066 majorEmpty 0.06304216384887695 +E2E churn 16.72079110145569 majorFull 0.1657090187072754 majorEmpty 0.060957908630371094 +E2E churn 16.66716694831848 majorFull 0.16962504386901855 majorEmpty 0.0614168643951416 +E2E churn 16.254791975021362 majorFull 0.15366601943969727 majorEmpty 0.06124997138977051 diff --git a/docs/results/worker-runtime-collection-costs-v1-phases.txt b/docs/results/worker-runtime-collection-costs-v1-phases.txt new file mode 100644 index 000000000..ffcfbc920 --- /dev/null +++ b/docs/results/worker-runtime-collection-costs-v1-phases.txt @@ -0,0 +1,134 @@ +Per-collection records from the scratch timing patch (worker-runtime-collection-costs-v1-timing.patch), +base 5e489f8f0. Fields: kind (0 serial minor, 1 parallel minor, 2 major with serial marking, 3 major with +parallel marking), young top, old top before, old top after, remembered count, then nanoseconds: total, +hold begin, evacuation or compaction, start-bit clear or page release, hold finish, remembered clear, +then the evacuator's or compactor's phases, then the compactor's bitmap words. + +== par-4 (nursery 4 MiB, parallel) +GCREC 0 4194288 0 32888 0 833166 101041 712500 19375 125 125 570000 208 28084 41 0 0 0 0 0 +GCREC 0 4194264 32888 63608 1 53167 42 33541 19542 0 42 9458 6875 17042 0 0 0 0 0 0 +GCREC 0 4194264 63608 94328 1 57042 0 36833 20084 0 125 10000 8541 18084 41 0 0 0 0 0 +GCREC 0 4194264 94328 125048 1 51042 0 31583 19417 42 0 7875 6958 16667 41 0 0 0 0 0 +GCREC 0 4194264 125048 155768 1 53958 0 34791 19125 42 0 7667 11167 15875 41 0 0 0 0 0 +GCREC 0 4194264 155768 186488 1 51000 0 31750 19208 0 42 7666 7209 16791 0 0 0 0 0 0 +GCREC 0 4194296 186488 217208 1 50333 42 30916 19334 41 0 7541 6334 17000 41 0 0 0 0 0 +GCREC 0 4194264 217208 247928 1 51375 42 31875 19417 0 41 7500 6375 17916 0 0 0 0 0 0 +GCREC 0 4194264 247928 278648 1 73584 625 48209 24500 41 209 14792 8666 24292 0 0 0 0 0 0 +GCREC 0 4194264 278648 309368 1 68083 250 43291 24292 167 83 12333 7834 23000 41 0 0 0 0 0 +GCREC 0 4194264 309368 340088 1 53666 41 33375 20209 41 0 8250 8125 16917 0 0 0 0 0 0 +GCREC 0 4194296 340088 370808 1 52500 0 32334 20125 41 0 7875 7375 17000 0 0 0 0 0 0 +GCREC 0 4194264 370808 401528 1 51375 41 31209 20083 42 0 7750 7333 16042 0 0 0 0 0 0 +GCREC 0 4194264 401528 432248 1 51750 41 31542 20125 0 42 7708 7250 16500 42 0 0 0 0 0 +GCREC 0 4194264 432248 462968 1 52375 42 32167 20083 42 41 7750 6500 17833 0 0 0 0 0 0 +GCREC 0 4194264 462968 493688 1 52042 0 31958 20042 42 0 7875 6541 17459 41 0 0 0 0 0 +GCREC 3 3607384 493688 32888 1 962166 83 934042 27791 42 208 89916 250959 41 152709 41 9625 145334 285041 63244 +GCREC 2 0 32888 32888 0 74666 375 74166 42 42 41 250 37792 0 375 83 8834 41 26625 129 +GCREC 2 0 32888 32888 0 62917 83 62792 0 42 0 208 32250 42 292 41 7334 41 22500 129 + +== par-32 (nursery 32 MiB, parallel) +GCREC 1 33554432 0 65536 0 815500 84959 571666 158500 42 333 570459 375 0 0 0 0 0 0 0 +GCREC 1 33554408 65536 98304 1 237167 875 81333 154375 209 375 80417 500 0 0 0 0 0 0 0 +GCREC 1 33554408 98304 163840 1 244292 625 78917 164375 125 250 77667 625 0 0 0 0 0 0 0 +GCREC 1 33554408 163840 262144 1 270292 667 108166 161042 208 209 107458 167 0 0 0 0 0 0 0 +GCREC 1 33554384 262144 294912 1 237167 417 81417 154791 209 333 80875 292 0 0 0 0 0 0 0 +GCREC 1 33554408 294912 327680 1 259708 333 104167 154875 125 208 103416 500 0 0 0 0 0 0 0 +GCREC 1 33554408 327680 393216 1 251458 458 96459 154208 208 125 95750 375 0 0 0 0 0 0 0 +GCREC 1 33554384 393216 425984 1 246542 542 89042 156708 83 167 88625 167 0 0 0 0 0 0 0 +GCREC 1 33554408 425984 491520 1 254625 375 93792 159875 125 458 93000 500 0 0 0 0 0 0 0 +GCREC 1 33554408 491520 589824 1 251208 2500 76291 172042 167 208 75667 500 0 0 0 0 0 0 0 +GCREC 1 33554408 589824 655360 1 228750 333 70000 158167 125 125 69125 334 0 0 0 0 0 0 0 +GCREC 1 33554384 655360 688128 1 259792 3542 101208 154709 166 167 100833 292 0 0 0 0 0 0 0 +GCREC 1 33554408 688128 753664 1 260250 333 94542 165000 208 167 94000 291 0 0 0 0 0 0 0 +GCREC 1 33554408 753664 819200 1 250416 291 83917 165875 125 208 83667 83 0 0 0 0 0 0 0 +GCREC 1 33554408 819200 884736 1 262708 375 93833 168083 125 292 93458 292 0 0 0 0 0 0 0 +GCREC 1 33554384 884736 917504 1 271708 416 106459 164500 125 208 105709 208 0 0 0 0 0 0 0 +GCREC 3 28609272 917504 32888 1 4909333 166 4742167 166917 41 42 707959 80208 42 1221625 41 10625 450667 2129167 504971 +GCREC 2 0 32888 32888 0 73084 167 72875 0 0 42 250 39167 41 375 84 7791 0 25000 129 +GCREC 2 0 32888 32888 0 59584 42 59500 0 42 0 208 31125 0 334 0 7083 0 20708 129 + +== ser-32 (nursery 32 MiB, serial) +GCREC 0 33554432 0 32888 0 888500 104292 623833 159959 291 125 495583 292 32125 0 0 0 0 0 0 +GCREC 0 33554408 32888 63608 1 200208 625 44292 154875 208 208 13958 9250 20750 0 0 0 0 0 0 +GCREC 0 33554408 63608 94384 1 194000 500 39917 153208 84 291 12417 9208 18083 42 0 0 0 0 0 +GCREC 0 33554408 94384 125160 1 207625 500 46667 159916 84 458 14375 11167 20583 0 0 0 0 0 0 +GCREC 0 33554384 125160 155880 1 206125 583 48209 157000 125 208 14208 14250 19542 0 0 0 0 0 0 +GCREC 0 33554408 155880 186600 1 195625 250 38292 156916 84 83 11875 9375 16833 0 0 0 0 0 0 +GCREC 0 33554408 186600 217376 1 199792 542 43583 155375 84 208 15042 7250 21042 0 0 0 0 0 0 +GCREC 0 33554384 217376 248096 1 199459 417 44125 154750 42 125 11458 8042 24541 0 0 0 0 0 0 +GCREC 0 33554408 248096 278816 1 203583 125 42167 161000 125 166 10375 7458 24042 0 0 0 0 0 0 +GCREC 0 33554408 278816 309592 1 204041 166 37917 165833 84 41 12209 6916 18625 0 0 0 0 0 0 +GCREC 0 33554408 309592 340368 1 205000 459 42291 162000 125 125 12084 10041 20000 0 0 0 0 0 0 +GCREC 0 33554384 340368 371088 1 203292 334 46791 156042 42 83 9791 14084 22750 0 0 0 0 0 0 +GCREC 0 33554408 371088 401808 1 207541 208 42833 164375 42 83 9500 13500 19750 41 0 0 0 0 0 +GCREC 0 33554408 401808 432584 1 205708 541 44959 159833 208 167 15000 10209 19583 0 0 0 0 0 0 +GCREC 0 33554408 432584 463360 1 209875 41 43875 165875 42 42 10000 7875 26000 0 0 0 0 0 0 +GCREC 0 33554384 463360 494080 1 208667 500 44334 163708 42 83 10584 9083 24500 0 0 0 0 0 0 +GCREC 2 28609272 494080 32888 1 4463083 167 4288166 171500 0 3250 730916 45875 0 1245084 0 9625 0 2256208 504971 +GCREC 2 0 32888 32888 0 66333 250 66000 42 0 41 292 33916 0 334 41 7667 83 23500 129 +GCREC 2 0 32888 32888 0 59667 42 59541 42 0 42 208 31833 42 292 41 7292 42 19791 129 + +== par-256 (nursery 256 MiB, parallel) +GCREC 1 268435416 0 65536 0 2190792 102750 686750 1400542 375 375 685375 500 0 0 0 0 0 0 0 +GCREC 1 268435448 65536 131072 1 1385333 1083 102125 1281459 250 416 100583 459 0 0 0 0 0 0 0 +GCREC 1 268435424 131072 196608 1 1379542 1125 89125 1288875 125 292 87542 583 0 0 0 0 0 0 0 +GCREC 1 268435448 196608 294912 1 1467958 708 103417 1363083 250 500 102417 542 0 0 0 0 0 0 0 +GCREC 1 268435424 294912 327680 1 1436333 667 106541 1328584 208 333 105958 292 0 0 0 0 0 0 0 +GCREC 1 268435424 327680 393216 1 1382250 1250 101958 1278292 333 417 100042 458 0 0 0 0 0 0 0 +GCREC 1 268435448 393216 491520 1 1387292 667 106000 1280250 125 250 104792 458 0 0 0 0 0 0 0 +GCREC 1 268435424 491520 524288 1 1610500 1000 113125 1490667 333 5375 111625 584 0 0 0 0 0 0 0 +GCREC 1 268435448 524288 589824 1 1533334 667 103625 1428458 250 334 102834 250 0 0 0 0 0 0 0 +GCREC 1 268435424 589824 622592 1 1478458 375 87666 1389875 292 250 86792 250 0 0 0 0 0 0 0 +GCREC 1 268435448 622592 688128 1 1434917 708 110209 1321041 167 2792 106708 584 0 0 0 0 0 0 0 +GCREC 1 268435456 688128 720896 1 1395833 708 108417 1286666 42 0 107875 416 0 0 0 0 0 0 0 +GCREC 1 268435448 720896 819200 1 1445333 708 79583 1364584 208 250 78834 250 0 0 0 0 0 0 0 +GCREC 1 268435424 819200 851968 1 1473000 1042 105583 1365667 250 458 103875 625 0 0 0 0 0 0 0 +GCREC 1 268435448 851968 917504 1 1387458 1208 92500 1290875 125 2750 91666 334 0 0 0 0 0 0 0 +GCREC 1 268435424 917504 983040 1 1432708 791 108334 1323166 125 292 107709 333 0 0 0 0 0 0 0 +GCREC 3 228625008 983040 32888 1 43426833 750 41181375 2243958 375 375 6251875 143583 0 12794625 42 14083 4548792 17310708 4038795 +GCREC 2 0 32888 32888 0 86625 666 85875 42 42 0 292 46125 41 3209 41 9292 42 26500 129 +GCREC 2 0 32888 32888 0 66458 2917 63458 42 0 41 209 33458 42 333 42 7916 0 21417 129 + +== ser-256 (nursery 256 MiB, serial) +GCREC 0 268435416 0 32888 0 2225875 128667 726750 1369917 291 250 571750 458 35833 42 0 0 0 0 0 +GCREC 0 268435448 32888 63664 1 1394333 1041 60125 1332625 250 292 23708 10583 25459 0 0 0 0 0 0 +GCREC 0 268435424 63664 94384 1 1332667 958 53250 1278000 250 209 16375 16625 20125 0 0 0 0 0 0 +GCREC 0 268435448 94384 125160 1 1369542 1083 52042 1316125 83 209 15042 17333 19583 0 0 0 0 0 0 +GCREC 0 268435424 125160 155880 1 1337750 1375 50875 1284875 208 417 15750 14208 20375 0 0 0 0 0 0 +GCREC 0 268435424 155880 186600 1 1341167 959 51208 1288500 208 292 15292 14833 20833 0 0 0 0 0 0 +GCREC 0 268435448 186600 217376 1 1289292 1083 49000 1238750 125 334 17333 8500 22792 0 0 0 0 0 0 +GCREC 0 268435424 217376 248096 1 1397667 1250 56542 1339333 208 334 20000 9375 26959 0 0 0 0 0 0 +GCREC 0 268435448 248096 278872 1 1377625 750 50583 1325958 0 334 17459 8625 24041 42 0 0 0 0 0 +GCREC 0 268435424 278872 309592 1 1349916 791 47667 1301125 83 250 14584 8250 22916 0 0 0 0 0 0 +GCREC 0 268435448 309592 340368 1 1282750 1292 62583 1218250 292 333 19250 15834 27416 0 0 0 0 0 0 +GCREC 0 268435456 340368 371088 1 1304083 1500 47791 1254250 125 417 15375 13958 18084 0 0 0 0 0 0 +GCREC 0 268435448 371088 401864 1 1349959 1167 51125 1296792 333 542 17333 11750 21875 0 0 0 0 0 0 +GCREC 0 268435424 401864 432584 1 1389625 1167 68542 1319000 375 541 17500 14584 36166 0 0 0 0 0 0 +GCREC 0 268435448 432584 463360 1 1425750 1209 53291 1370417 500 333 17958 9292 25542 0 0 0 0 0 0 +GCREC 0 268435424 463360 494080 1 1359375 1250 55625 1301958 292 250 16500 14125 24875 41 0 0 0 0 0 +GCREC 2 228625008 494080 32888 1 35661042 1167 33510333 2148750 292 500 6242292 53917 0 10468958 42 13041 0 16731834 4038795 +GCREC 2 0 32888 32888 0 69125 541 68542 0 0 42 333 35958 42 333 0 7792 125 23583 129 +GCREC 2 0 32888 32888 0 59875 84 59708 42 0 41 209 29875 41 334 0 7083 0 22125 129 + +== records clone in one isolate, 32 MiB nursery, 300 operations +ELAPSED_MS 374.5989589691162 ops 300 +GCREC 1 33554408 0 1760480 0 927708 15333 757500 154375 167 333 756166 417 0 0 0 0 0 0 0 +GCREC 1 33554400 1760480 1907960 565 253625 833 96834 154458 208 1292 95667 625 0 0 0 0 0 0 0 +GCREC 1 33554424 1907960 3054840 9 528917 542 370917 154458 83 2917 369875 292 0 0 0 0 0 0 0 +GCREC 1 33554400 3054840 4103416 7 493083 292 337750 154416 209 416 336833 458 0 0 0 0 0 0 0 +GCREC 1 33554424 4103416 5315832 7 518500 375 360416 154792 167 2750 359750 250 0 0 0 0 0 0 0 +GCREC 1 33554384 5315832 5414136 962 281000 417 123250 155625 42 1666 122458 250 0 0 0 0 0 0 0 +GCREC 1 33554416 5414136 6888696 393 608250 417 450917 154375 125 2416 450250 250 0 0 0 0 0 0 0 +GCREC 1 33553848 6888696 7937272 7 449291 416 294084 154416 125 250 293542 250 0 0 0 0 0 0 0 +GCREC 1 33554376 7937272 8969488 5 424916 333 269875 154458 42 208 269458 167 0 0 0 0 0 0 0 +GCREC 1 33554432 8969488 10329384 8 525458 3125 360333 161542 125 333 359792 458 0 0 0 0 0 0 0 +GCREC 1 33554408 10329384 10525992 773 263666 500 107166 154375 125 1500 106666 334 0 0 0 0 0 0 0 +GCREC 1 33554400 10525992 11803944 210 585250 625 429417 154417 125 666 428500 292 0 0 0 0 0 0 0 +GCREC 1 33554400 11803944 12819752 7 531250 625 372208 158375 42 0 371791 250 0 0 0 0 0 0 0 +GCREC 1 33554432 12819752 13884736 7 480917 292 326000 154333 84 208 325167 250 0 0 0 0 0 0 0 +GCREC 1 33554424 13884736 15424832 8 679750 500 524542 154375 83 250 523875 250 0 0 0 0 0 0 0 +GCREC 1 33554384 15424832 15539544 580 265500 459 106958 156375 458 1250 106125 375 0 0 0 0 0 0 0 +GCREC 1 33553832 15539544 16686424 28 537209 500 381917 154542 0 250 381042 291 0 0 0 0 0 0 0 +GCREC 1 33554432 16686424 17718640 5 445709 334 290666 154500 42 167 290209 250 0 0 0 0 0 0 0 +GCREC 1 33554432 17718640 18898288 8 506833 2958 346084 157458 83 250 345667 333 0 0 0 0 0 0 0 +GCREC 1 33554424 18898288 19062128 979 301167 417 144000 154958 42 1750 142083 84 0 0 0 0 0 0 0 +GCREC 1 33554416 19062128 20569456 409 612333 417 456291 154542 125 958 455458 333 0 0 0 0 0 0 0 diff --git a/docs/results/worker-runtime-collection-costs-v1-timing.patch b/docs/results/worker-runtime-collection-costs-v1-timing.patch new file mode 100644 index 000000000..867d31e0b --- /dev/null +++ b/docs/results/worker-runtime-collection-costs-v1-timing.patch @@ -0,0 +1,284 @@ +diff --git a/packages/compiler/src/build.ts b/packages/compiler/src/build.ts +index c4e847f27..94d1999c5 100644 +--- a/packages/compiler/src/build.ts ++++ b/packages/compiler/src/build.ts +@@ -1082,6 +1082,12 @@ export function compileMacOSProgram( + request.report("runtime"); + const collectorBindings = parallelCollectorBindings(request); + const runtimeBindings: NativeHostBinding[] = [ ++ // SCRATCH TIMING (not for landing). ++ { ++ module: "packages/native-runtime/src/intrinsics/clock.ts", ++ name: "monotonicMilliseconds", ++ operation: "monotonic-time", ++ }, + { + module: request.memoryModule, + name: "configureExternalMemory", +@@ -1140,7 +1146,14 @@ export function compileMacOSProgram( + entry: request.runtimeModule, + exports: [request.runtimeEntry ?? "initializeRuntime"], + start: request.runtimeEntry ?? "initializeRuntime", +- runtimes: [{ module: request.memoryModule, namespace: "native-memory" }], ++ runtimes: [ ++ { module: request.memoryModule, namespace: "native-memory" }, ++ // SCRATCH TIMING (not for landing). ++ { ++ module: "packages/native-runtime/src/intrinsics/clock.ts", ++ namespace: "native-collector-clock", ++ }, ++ ], + hostBindings: runtimeBindings, + }, + target, +diff --git a/packages/native-runtime/src/generational.ts b/packages/native-runtime/src/generational.ts +index 00a047b55..3e415d5c5 100644 +--- a/packages/native-runtime/src/generational.ts ++++ b/packages/native-runtime/src/generational.ts +@@ -7,8 +7,19 @@ import { + createCheneyCollector, + createCompactor, + noSharedStrings, ++ cheneyTimes, ++ compactTimes, + } from "./tracing/cheney.ts"; +-import { createParallelEvacuator } from "./tracing/parallel.ts"; ++import { ++ createParallelEvacuator, ++ parallelTimes, ++} from "./tracing/parallel.ts"; ++import { monotonicMilliseconds } from "./intrinsics/clock.ts"; ++import { ++ mapMemory, ++ writeMemoryWord, ++ currentContextDomain, ++} from "./intrinsics/memory.ts"; + import { cgroupProcessorLimit } from "./processors.ts"; + import { + currentRootHead, +@@ -234,14 +245,39 @@ export function initializeGenerationalRuntime( + configureGenerationalAllocation(allocation, youngBits, dirty, roots) !== 0 + ) + throw "INVALID_GENERATIONAL_STATE"; ++ // SCRATCH TIMING (not for landing): per-collection records of 24 words ++ // (nanoseconds), published at identity page words 5 (log) and 6 (count). ++ const logRecords = 512, ++ logBytes = logRecords * 24 * 8, ++ log = mapMemory(logBytes), ++ identity = currentContextDomain(); ++ let logged = 0; ++ if (log > 0 && identity > 0) writeMemoryWord(identity, 56, 40, log); ++ const stamp = new Float64Array(8); ++ function ns(from: number, to: number): number { ++ return Math.max(0, Math.round((to - from) * 1000000)); ++ } ++ function record(fields: Float64Array, count: number): void { ++ if (log <= 0 || identity <= 0 || logged >= logRecords) return; ++ for (let index = 0; index < count; index++) ++ writeMemoryWord(log, logBytes, (logged * 24 + index) * 8, fields[index] ?? 0); ++ logged++; ++ writeMemoryWord(identity, 56, 48, logged); ++ } ++ const fields = new Float64Array(24); + function collect(forceMajor: boolean): boolean { + if (exhausted) return false; + const youngTop = allocation[0] ?? 0, + oldTop = allocation[3] ?? 0; + const minor = + !forceMajor && youngTop > 0 && youngTop <= oldCapacity - oldTop; ++ const rememberedCount = ++ (allocation[14] ?? 0) !== 0 ? -1 : (allocation[12] ?? 0); ++ stamp[0] = monotonicMilliseconds(); + collecting = true; + holds?.begin(); ++ stamp[1] = monotonicMilliseconds(); ++ let usedParallel = false; + const base = activeBase(); + if (minor) { + // The nursery is evacuated into the old prefix, which has room for all +@@ -262,6 +298,7 @@ export function initializeGenerationalRuntime( + oldTop, + ) + : -1; ++ usedParallel = top >= 0; + const bytes = + top >= 0 + ? top - oldTop +@@ -276,8 +313,10 @@ export function initializeGenerationalRuntime( + base, + rememberedObjects, + ); ++ stamp[2] = monotonicMilliseconds(); + youngBits.fill(0, 0, Math.ceil(youngTop / 256)); + allocation[3] = oldTop + bytes; ++ stamp[3] = monotonicMilliseconds(); + } else { + // The whole active space compacts in place: live old and young + // objects slide down into a packed old prefix (survivors that no +@@ -293,6 +332,7 @@ export function initializeGenerationalRuntime( + chunkOffsets, + pending, + ); ++ stamp[2] = monotonicMilliseconds(); + allocation[3] = bytes; + if (bytes > oldCapacity) exhausted = true; + // Nothing above the packed prefix is live: return those pages. The +@@ -301,10 +341,37 @@ export function initializeGenerationalRuntime( + end = Math.floor((base + used) / releasePage) * releasePage; + if (end > start && releaseMemory(start, end - start) !== 0) + throw "GENERATIONAL_RELEASE"; ++ stamp[3] = monotonicMilliseconds(); + } + holds?.finish(minor); ++ stamp[4] = monotonicMilliseconds(); + allocation[0] = exhausted ? youngCapacity : 0; + clearRemembered(); ++ stamp[5] = monotonicMilliseconds(); ++ fields.fill(0); ++ fields[0] = minor ? (usedParallel ? 1 : 0) : (compactTimes[9] ?? 0) + 2; ++ fields[1] = youngTop; ++ fields[2] = oldTop; ++ fields[3] = allocation[3] ?? 0; ++ fields[4] = rememberedCount < 0 ? 999999999 : rememberedCount; ++ fields[5] = ns(stamp[0] ?? 0, stamp[5] ?? 0); ++ fields[6] = ns(stamp[0] ?? 0, stamp[1] ?? 0); ++ fields[7] = ns(stamp[1] ?? 0, stamp[2] ?? 0); ++ fields[8] = ns(stamp[2] ?? 0, stamp[3] ?? 0); ++ fields[9] = ns(stamp[3] ?? 0, stamp[4] ?? 0); ++ fields[10] = ns(stamp[4] ?? 0, stamp[5] ?? 0); ++ if (minor && !usedParallel) ++ for (let index = 0; index < 4; index++) ++ fields[11 + index] = ns(cheneyTimes[index] ?? 0, cheneyTimes[index + 1] ?? 0); ++ else if (minor) { ++ fields[11] = ns(parallelTimes[0] ?? 0, parallelTimes[1] ?? 0); ++ fields[12] = ns(parallelTimes[1] ?? 0, parallelTimes[2] ?? 0); ++ } else { ++ for (let index = 0; index < 8; index++) ++ fields[11 + index] = ns(compactTimes[index] ?? 0, compactTimes[index + 1] ?? 0); ++ fields[19] = compactTimes[10] ?? 0; ++ } ++ record(fields, 20); + collecting = false; + return !exhausted; + } +diff --git a/packages/native-runtime/src/tracing/cheney.ts b/packages/native-runtime/src/tracing/cheney.ts +index 539c71af2..cd562b064 100644 +--- a/packages/native-runtime/src/tracing/cheney.ts ++++ b/packages/native-runtime/src/tracing/cheney.ts +@@ -31,6 +31,10 @@ import { + headerFlags, + type ReadWord, + } from "./scan.ts"; ++import { monotonicMilliseconds } from "../intrinsics/clock.ts"; ++// SCRATCH TIMING (not for landing): phase stamps in milliseconds. ++export const cheneyTimes = new Float64Array(8); ++export const compactTimes = new Float64Array(12); + + /** Scratch kind flag on a destination weak slot whose ephemeron value was + * traced (live weak slots use flags 0 and 1). */ +@@ -505,6 +509,7 @@ export function createCheneyCollector( + state.sharedShift = shared.shift; + currentStarts = startBits; + const start = destinationStart; ++ cheneyTimes[0] = monotonicMilliseconds(); + if ( + walkNativeStack( + readWord, +@@ -515,12 +520,16 @@ export function createCheneyCollector( + ) !== "complete" + ) + throw "CHENEY_ROOT"; ++ cheneyTimes[1] = monotonicMilliseconds(); + if (remembered !== undefined) remembered(scanRemembered); ++ cheneyTimes[2] = monotonicMilliseconds(); + let scanned = drain(state, startBits, start); ++ cheneyTimes[3] = monotonicMilliseconds(); + if (state.weakCopied !== 0) { + scanned = traceEphemerons(state, startBits, start, scanned); + finishWeakSlots(state, start); + } ++ cheneyTimes[4] = monotonicMilliseconds(); + return state.free - start; + }, + }; +@@ -984,7 +993,9 @@ export function createCompactor( + state.liveBytes = 0; + state.sawView = false; + state.sawWeak = false; ++ compactTimes[0] = monotonicMilliseconds(); + live.fill(0, 0, words); ++ compactTimes[1] = monotonicMilliseconds(); + // 1. Mark (in parallel for large regions). + const delegate = used >= parallelMinimum ? helpers : undefined; + let parallel = false; +@@ -1013,6 +1024,7 @@ export function createCompactor( + ) + throw "COMPACT_ROOT"; + closeMarks(state); ++ compactTimes[2] = monotonicMilliseconds(); + if (state.sawWeak) { + // Ephemerons whose keys are live keep their values, to a fixpoint + // (re-marking is idempotent: a pass that marks nothing ends it); +@@ -1025,6 +1037,7 @@ export function createCompactor( + } + forEachLive(state, finishCompactWeak); + } ++ compactTimes[3] = monotonicMilliseconds(); + // 2. Offsets: live granules before each chunk. + let granules = 0; + for (let word = 0; word < words; word++) { +@@ -1032,8 +1045,10 @@ export function createCompactor( + granules += popcount(live[word] ?? 0); + } + if (granules * 8 !== state.liveBytes) throw "COMPACT_LIVE"; ++ compactTimes[4] = monotonicMilliseconds(); + // 3. Views' data pointers against their unmodified owner chains. + if (state.sawView) forEachLive(state, relocateView); ++ compactTimes[5] = monotonicMilliseconds(); + // 4 and 5. Roots, then every live object's fields and position. + if ( + walkNativeStack( +@@ -1045,8 +1060,13 @@ export function createCompactor( + ) !== "complete" + ) + throw "COMPACT_ROOT"; ++ compactTimes[6] = monotonicMilliseconds(); + if (delegate !== undefined && parallel) delegate.forward(state, words); ++ compactTimes[7] = monotonicMilliseconds(); + const liveBytes = compactLive(state, words, !parallel); ++ compactTimes[8] = monotonicMilliseconds(); ++ compactTimes[9] = parallel ? 1 : 0; ++ compactTimes[10] = words; + if (liveBytes !== state.liveBytes) throw "COMPACT_PACK"; + return liveBytes; + }, +diff --git a/packages/native-runtime/src/tracing/parallel.ts b/packages/native-runtime/src/tracing/parallel.ts +index f0423fd09..970dbbd8f 100644 +--- a/packages/native-runtime/src/tracing/parallel.ts ++++ b/packages/native-runtime/src/tracing/parallel.ts +@@ -52,6 +52,9 @@ import { + type ReadWord, + type VisitWord, + } from "./scan.ts"; ++import { monotonicMilliseconds } from "../intrinsics/clock.ts"; ++// SCRATCH TIMING (not for landing). ++export const parallelTimes = new Float64Array(4); + + /** Parallel stop-the-world evacuation (see docs/parallel-gc.md). + * +@@ -1419,12 +1422,15 @@ export function createParallelEvacuator( + state.sharedEnd = sharedStrings.end; + state.sharedShift = sharedStrings.shift; + } ++ parallelTimes[0] = monotonicMilliseconds(); + shared.rootHead = rootHead; + shared.rememberedCount = rememberedObjects ? rememberedCount : -2; + shared.oldBase = toBase; + shared.oldTop = oldTop; + runCommand(commandEvacuate, first, rootHead); ++ parallelTimes[1] = monotonicMilliseconds(); + finishCopiedWeak(); ++ parallelTimes[2] = monotonicMilliseconds(); + return read(record + recordCursor); + }, + }; diff --git a/packages/native-runtime/src/copying.ts b/packages/native-runtime/src/copying.ts index 1b944a088..d22896678 100644 --- a/packages/native-runtime/src/copying.ts +++ b/packages/native-runtime/src/copying.ts @@ -1,5 +1,5 @@ import { initializeExternalMemory } from "./external-memory.ts"; -import { createCheneyCollector } from "./tracing/cheney.ts"; +import { clearWords, createCheneyCollector } from "./tracing/cheney.ts"; import { currentRootHead, configureLinearAllocation, @@ -77,7 +77,7 @@ export function initializeCopyingRuntime( destination = managedBase + (reverse ? 0 : capacity); // Start bits are relative to the active space; clear the used prefix and // let the evacuation describe the packed destination. - bits.fill(0, 0, Math.ceil(top / 256)); + clearWords(bits, 0, Math.ceil(top / 256)); const bytes = heap.collect( source, top, diff --git a/packages/native-runtime/src/generational.ts b/packages/native-runtime/src/generational.ts index 924c7b869..4a6f24614 100644 --- a/packages/native-runtime/src/generational.ts +++ b/packages/native-runtime/src/generational.ts @@ -1,6 +1,11 @@ import { nativeAllocatorShutdownRequest } from "@tint/abi/host"; import { initializeExternalMemory } from "./external-memory.ts"; -import { createCheneyCollector, createCompactor } from "./tracing/cheney.ts"; +import { + clearBytes, + clearWords, + createCheneyCollector, + createCompactor, +} from "./tracing/cheney.ts"; import { createParallelEvacuator } from "./tracing/parallel.ts"; import { cgroupProcessorLimit } from "./processors.ts"; import { @@ -145,7 +150,7 @@ export function initializeGenerationalRuntime( } } function clearRemembered(): void { - if ((allocation[14] ?? 0) !== 0) dirty.fill(0); + if ((allocation[14] ?? 0) !== 0) clearBytes(dirty, 0, dirty.length); else for (let i = 0; i < (allocation[12] ?? 0); i++) { const offset = roots[i]; @@ -211,73 +216,88 @@ export function initializeGenerationalRuntime( configureGenerationalAllocation(allocation, youngBits, dirty, roots) !== 0 ) throw "INVALID_GENERATIONAL_STATE"; + /** A minor collection: the nursery is evacuated into the old prefix, which + * has room for all of it; remembered old objects are the only old-to-young + * edges. */ + function evacuateNursery(youngTop: number, oldTop: number): void { + const base = activeBase(); + const top = + parallel !== undefined && youngTop >= parallelMinimum + ? parallel.collect( + base + oldCapacity, + youngTop, + base, + oldTop, + oldCapacity, + youngTop, + currentRootHead(), + base, + true, + (allocation[14] ?? 0) !== 0 ? -1 : (allocation[12] ?? 0), + oldTop, + ) + : -1; + const bytes = + top >= 0 + ? top - oldTop + : heap.collect( + base + oldCapacity, + youngTop, + base, + oldTop, + oldCapacity, + currentRootHead(), + bits, + base, + rememberedObjects, + ); + clearWords(youngBits, 0, Math.ceil(youngTop / 256)); + allocation[3] = oldTop + bytes; + allocation[0] = 0; + clearRemembered(); + } function collect(forceMajor: boolean): boolean { if (exhausted) return false; const youngTop = allocation[0] ?? 0, - oldTop = allocation[3] ?? 0; - const minor = - !forceMajor && youngTop > 0 && youngTop <= oldCapacity - oldTop; + oldTop = allocation[3] ?? 0, + // What the space has used: the old fill, and the nursery's when it is + // not empty (the gap between them is untouched). + reach = youngTop > 0 ? oldCapacity + youngTop : oldTop; collecting = true; - const base = activeBase(); - if (minor) { - // The nursery is evacuated into the old prefix, which has room for all - // of it; remembered old objects are the only old-to-young edges. - const top = - parallel !== undefined && youngTop >= parallelMinimum - ? parallel.collect( - base + oldCapacity, - youngTop, - base, - oldTop, - oldCapacity, - youngTop, - currentRootHead(), - base, - true, - (allocation[14] ?? 0) !== 0 ? -1 : (allocation[12] ?? 0), - oldTop, - ) - : -1; - const bytes = - top >= 0 - ? top - oldTop - : heap.collect( - base + oldCapacity, - youngTop, - base, - oldTop, - oldCapacity, - currentRootHead(), - bits, - base, - rememberedObjects, - ); - youngBits.fill(0, 0, Math.ceil(youngTop / 256)); - allocation[3] = oldTop + bytes; - } else { - // The whole active space compacts in place: live old and young - // objects slide down into a packed old prefix (survivors that no - // longer fit exhaust the heap for good), so no second space is - // touched and peak residency is the space's fill plus metadata. - const used = youngTop > 0 ? oldCapacity + youngTop : oldTop, - bytes = compactor.collect( - base, - used, - currentRootHead(), - bits, - live, - chunkOffsets, - pending, - ); - allocation[3] = bytes; - if (bytes > oldCapacity) exhausted = true; - // Nothing above the packed prefix is live: return those pages. The - // nursery and the old fill fault back in as zero pages when reused. - const start = Math.ceil((base + bytes) / releasePage) * releasePage, - end = Math.floor((base + used) / releasePage) * releasePage; - if (end > start && releaseMemory(start, end - start) !== 0) - throw "GENERATIONAL_RELEASE"; + if (youngTop > 0 && youngTop <= oldCapacity - oldTop) { + evacuateNursery(youngTop, oldTop); + if (!forceMajor) { + collecting = false; + return true; + } + // A major collection then compacts only the packed old prefix, not + // the whole capacity up to the nursery's top. } + // The whole occupied space compacts in place: live old and young + // objects slide down into a packed old prefix (survivors that no longer + // fit exhaust the heap for good), so no second space is touched and peak + // residency is the space's fill plus metadata. + const base = activeBase(), + young = allocation[0] ?? 0, + used = young > 0 ? oldCapacity + young : (allocation[3] ?? 0), + bytes = compactor.collect( + base, + used, + currentRootHead(), + bits, + live, + chunkOffsets, + pending, + ); + allocation[3] = bytes; + if (bytes > oldCapacity) exhausted = true; + // Nothing above the packed prefix is live: return those pages, up to + // what the space had used (the nursery included). The nursery and the + // old fill fault back in as zero pages when reused. + const start = Math.ceil((base + bytes) / releasePage) * releasePage, + end = Math.floor((base + reach) / releasePage) * releasePage; + if (end > start && releaseMemory(start, end - start) !== 0) + throw "GENERATIONAL_RELEASE"; allocation[0] = exhausted ? youngCapacity : 0; clearRemembered(); collecting = false; diff --git a/packages/native-runtime/src/tracing/cheney.ts b/packages/native-runtime/src/tracing/cheney.ts index a494675a1..9e3006b1c 100644 --- a/packages/native-runtime/src/tracing/cheney.ts +++ b/packages/native-runtime/src/tracing/cheney.ts @@ -83,6 +83,38 @@ interface CheneyState { readonly managedLength: number; } +// Zeroes for bulk clears. `set` copies a block with one memmove where `fill` +// stores element by element (about ten times slower), and a collection +// must not allocate, so the blocks are permanent. +const zeroWords = new Uint32Array(512); +const zeroBytes = new Uint8Array(2048); +/** Zeroes elements [start, end) of `array`. */ +export function clearWords( + array: Uint32Array, + start: number, + end: number, +): void { + let at = start; + while (end - at >= 512) { + array.set(zeroWords, at); + at += 512; + } + if (at < end) array.fill(0, at, end); +} +/** Zeroes elements [start, end) of `array`. */ +export function clearBytes( + array: Uint8Array, + start: number, + end: number, +): void { + let at = start; + while (end - at >= 2048) { + array.set(zeroBytes, at); + at += 2048; + } + if (at < end) array.fill(0, at, end); +} + // Single host operations, so the inliner folds them into their callers. // Headers, descriptors and references are trusted (see the contract below). function read(address: number): number { @@ -925,7 +957,7 @@ export function createCompactor( state.liveBytes = 0; state.sawView = false; state.sawWeak = false; - live.fill(0, 0, words); + clearWords(live, 0, words); // 1. Mark (in parallel for large regions). const delegate = used >= parallelMinimum ? helpers : undefined; let parallel = false; diff --git a/tools/verify-heap-tools.ts b/tools/verify-heap-tools.ts index 20274d161..a1cd59ce1 100644 --- a/tools/verify-heap-tools.ts +++ b/tools/verify-heap-tools.ts @@ -117,15 +117,23 @@ function stopInInspect(): string[] { try { if (run("descriptor")) { - // A line inside the collector where youngTop and oldTop are live (one - // with its own line-table entry), found rather than hard-coded. - const collectLine = - readFileSync("packages/native-runtime/src/generational.ts", "utf8") - .split("\n") - .findIndex((line) => line.trim() === "if (minor) {") + 1; - assert.ok(collectLine > 0, "collector line"); - const inCollect = debug.python( - "assert 'native-runtime/src/generational.ts:collect ' in " + + // A line in the nursery evacuation where youngTop and oldTop are homed + // (live across its first call) and nothing has moved yet, found rather + // than hard-coded. It has its own line-table entry. (In collect itself + // they are dead after the evacuation call, so they stay in registers + // the debug information does not describe.) + const evacuatorText = + "parallel !== undefined && youngTop >= parallelMinimum", + evacuatorFunction = "native-runtime/src/generational.ts:evacuateNursery ", + evacuatorLine = + readFileSync("packages/native-runtime/src/generational.ts", "utf8") + .split("\n") + .findIndex((line) => line.trim() === evacuatorText) + 1; + assert.ok(evacuatorLine > 0, "evacuator line"); + const inEvacuator = debug.python( + "assert '" + + evacuatorFunction + + "' in " + debug.functionExpression + ", " + debug.functionExpression, @@ -141,24 +149,26 @@ try { const output = debug.run(image, [ debug.breakAtLine( "packages/native-runtime/src/generational.ts", - collectLine, + evacuatorLine, ), ...(debug.name === "lldb" ? [ - // The line must resolve inside the collector function (found + // The line must resolve inside the evacuation function (found // by its symbol), never silently into a neighbour after an // edit. (Under GDB each stop's frame is checked instead.) - "script bp = lldb.target.GetBreakpointAtIndex(0); names = [bp.GetLocationAtIndex(i).GetAddress().GetFunction().GetName() or '' for i in range(bp.GetNumLocations())]; assert len(names) == 1 and 'native-runtime/src/generational.ts:collect ' in names[0], names", + "script bp = lldb.target.GetBreakpointAtIndex(0); names = [bp.GetLocationAtIndex(i).GetAddress().GetFunction().GetName() or '' for i in range(bp.GetNumLocations())]; assert len(names) == 1 and '" + + evacuatorFunction + + "' in names[0], names", ] : []), debug.launch, - inCollect, + inEvacuator, locals, "tint-heap info --json", "tint-heap verify --json", debug.resume, debug.resume, - inCollect, + inEvacuator, locals, "tint-heap info --json", "tint-heap verify --json",