remedy-lua 0.1.1

A Lua 5.4 superset in safe Rust for running untrusted scripts: exact fuel, memory and depth limits, an incremental collector, an optional typed dialect and a capability-based host API.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
//! The heap: typed slabs of objects addressed by index and generation, the string interner,
//! the memory account, and the incremental mark-and-sweep collector.
//!
//! Objects are never moved; a freed slot is reset to an empty object and its generation
//! bumps, so a stale reference reads an empty object (never another type, never memory that
//! is gone) and the host API can tell it is stale.
//!
//! The collector is incremental: marking and sweeping run in small steps between
//! instructions, paid for by allocation (each byte allocated buys a fixed amount of
//! collector work) and charged to the script's fuel. New objects made while marking are
//! gray; writes into black tables and upvalues turn them gray again (a backward barrier);
//! thread stacks are marked again in the atomic step that ends marking. Weak tables
//! (`__mode`) are cleared in the atomic step, with ephemeron semantics for weak keys.

use std::any::Any;
use std::sync::Arc;

use crate::bytecode::Proto;
use crate::table::{SLOT_SIZE, Table};
use crate::value::{FuncRef, StrRef, TableRef, ThreadRef, UdRef, Value};
use crate::vm::{Frame, NativeFn};

pub(crate) const WHITE: u8 = 0;
pub(crate) const GRAY: u8 = 1;
pub(crate) const BLACK: u8 = 2;
pub(crate) const FREE: u8 = 3;
/// Units of the collector's work between looks at the host's interrupt and the deadline,
/// within a step: about 0.2 ms of ephemeron marking alone, and some 1 ms on a loaded
/// machine (16,384 of them, 4 to 7 ms there: S6GC-2e-R).
const ATOMIC_CHECK: usize = 1 << 12;

/// Bytes of a slab's room given back at a time (`Slab::shrink_piece`): about half a
/// millisecond of giving memory back to the system.
const SHRINK_BYTES: usize = 8 << 20;

/// Bytes of a slot's generation (counted while `trim` keeps one for a cut slot).
const GEN_BYTES: usize = 4;
/// Distance from a slab's first generation past which `trim` keeps cut generations
/// instead of raising the floor over them.
const FLOOR_LIMIT: u32 = 1 << 31;

/// The memory account: bytes the script's objects hold, against the cap.
pub(crate) struct Mem {
    /// The collector's trace, when the host turned it on (`Lua::gc_trace`, SEC7-G1).
    pub trace: Option<Box<crate::gctrace::Trace>>,
    /// Bytes held: the objects' and their slabs' free slots (`slack` of them), what the
    /// cap limits.
    pub used: usize,
    pub cap: usize,
    /// Bytes allocated since the collector last did work (negative: credit).
    pub debt: isize,
    /// Set when an allocation was refused.
    pub refused: bool,
    /// Extra room the host gets while it builds values to pass in.
    pub headroom: usize,
    /// Test mode: every allocation runs a whole collection.
    pub stress: bool,
    /// Buffers of swept tables, reused by new tables.
    pub pool: Pool,
    /// Bytes of the slabs' free slots (`Slab::slot_bytes` each), from the sweep that
    /// frees a slot until an allocation reuses it or `trim` cuts it off. Resident, so
    /// counted in `used` (SEC-M5: one survivor at a slab's end kept every kind's slab at
    /// its peak, uncounted, about the cap per kind). The collector paces on `live` (used
    /// less this), so the fuel a program costs does not follow its free slots.
    pub slack: usize,
    /// The collection cycle (`Heap::cycle`), for `Table::gc_snap`.
    pub cycle: u32,
    /// Marking is under way in `cycle` (from its start to the end of the atomic step), and
    /// what reaching an object costs in it (`Heap::obj_work`): what a table grown past its
    /// size at the cycle's start pays for the marking of its growth (`Table::take_resized`).
    pub marking: bool,
    pub obj_work: usize,
    /// What the table being traversed in chunks (`Table::gc_watch`) did besides the writes
    /// themselves (`Heap::part`).
    pub watch: Watch,
    /// The running call's watch (the host's interrupt and deadline), for the long table
    /// operations to look at between pieces of their work (`stopped`); set at each entry
    /// from the host.
    pub deadline: Option<crate::stdlib::pattern::Watch>,
    /// The fuel the running call has left, set before a write that may rehash
    /// (`Table::rehash`): unlimited outside a call.
    pub fuel: usize,
    /// Under a fuel watch (a growth the call's fuel cannot pay for whole, SEC8-F4): the most
    /// `Table::resized` may reach before the growth stops for the call's fuel, a piece at a
    /// time, keeping what it made (`table::look_w`). `None`: no watch.
    pub fuel_watch: Option<usize>,
    /// Whether `fuel` is the call's own (the full path's write): only then may a growth it
    /// cannot pay for whole be made under a fuel watch. The fast loop's `fuel` is its slice:
    /// a growth past it is left to the full path, as before.
    pub fuel_watchable: bool,
    /// What resizes given up on tables made (`Table::partial` holds the index past one):
    /// their bytes are in `used` until they go.
    pub partials: Vec<Option<Box<crate::table::Partial>>>,
    /// Big buffers being freed a piece at a time between looks (`park`), oldest first:
    /// their bytes are in `used` (as slack) until their pages go.
    pub freeing: Vec<Parked>,
}

/// Changes to the table traversed in chunks while marking is incremental that move
/// entries, noted by the table for the collector (`Heap::part_written`, `Heap::part_step`).
pub(crate) struct Watch {
    /// Entries (key, value) an insertion moved to another node: from a node not traversed
    /// yet to one traversed already, nothing else would mark them.
    pub moved: Vec<Value>,
    /// The table's parts were rebuilt or resized (entries moved wholesale): where the
    /// traversal stands means nothing any more, but below `alen_floor`.
    pub restructured: bool,
    /// The least length its array part had while it was rebuilt: a rebuild moves no entry
    /// of the array below its old and new lengths, and puts every entry it moves past both
    /// (into the array's new slots, or the hash parts, which come after it).
    pub alen_floor: usize,
}

impl Default for Watch {
    fn default() -> Self {
        Watch { moved: Vec::new(), restructured: false, alen_floor: usize::MAX }
    }
}

/// Array and hash buffers of swept tables, kept to make new tables without the system
/// allocator. Invisible to the account (a buffer is reused only at the exact size it is
/// charged for); bounded per size, and emptied by every full collection, so the process
/// gives the memory back too. Its bytes count as used (as slack: resident, not live, so
/// the collector's pacing ignores them) and it gives them back before any charge or fit
/// would be refused, so a limit refuses exactly what it refused without it. It holds at
/// most `POOL_BYTES` or 1/16 of the limit, whichever is less.
#[derive(Default)]
pub(crate) struct Pool {
    /// By capacity.
    arrs: Vec<Vec<Vec<Value>>>,
    /// By length (a power of two).
    nodes: Vec<Vec<Vec<crate::table::Node>>>,
    /// Bytes of buffers held.
    pub(crate) bytes: usize,
}

/// A buffer this big is freed a piece at a time (`Mem::park`).
pub(crate) const PARK_BYTES: usize = 32 << 20;

/// What `Mem::unpark` frees between two looks.
const PARK_PIECE: usize = 512 << 10;

/// A buffer being freed a piece at a time (`Mem::park`).
pub(crate) enum Parked {
    Nodes(Vec<crate::table::Node>),
    Slots(Vec<Value>),
    Bytes(Vec<u8>),
}

impl Parked {
    fn bytes(&self) -> usize {
        match self {
            Parked::Nodes(v) => v.capacity() * size_of::<crate::table::Node>(),
            Parked::Slots(v) => v.capacity() * size_of::<Value>(),
            Parked::Bytes(v) => v.capacity(),
        }
    }

    /// A piece freed: whether none is left.
    fn shrink(&mut self) -> bool {
        fn piece<T>(v: &mut Vec<T>) -> bool {
            let keep = v.len().saturating_sub(PARK_PIECE / size_of::<T>().max(1));
            v.truncate(keep);
            v.shrink_to(keep);
            v.capacity() == 0
        }
        match self {
            Parked::Nodes(v) => piece(v),
            Parked::Slots(v) => piece(v),
            Parked::Bytes(v) => piece(v),
        }
    }
}

/// Largest array capacity and hash size kept, and how many buffers of one size.
pub(crate) const POOL_MAX: usize = 64;
const POOL_PER_SIZE: usize = 1 << 16;
/// Most bytes the pool holds: 16 MiB per state (a burst of dead tables reused, not freed
/// and allocated again: about 4% of binary_trees, 7% of gc_churn and table_churn).
pub(crate) const POOL_BYTES: usize = 16 << 20;

impl Pool {
    /// Buffers held (for tests).
    #[cfg(test)]
    pub fn held(&self) -> usize {
        self.arrs.iter().map(Vec::len).sum::<usize>() + self.nodes.iter().map(Vec::len).sum::<usize>()
    }

    /// An empty array buffer of capacity exactly `cap`.
    pub(crate) fn arr(&mut self, cap: usize) -> Vec<Value> {
        match self.arrs.get_mut(cap).and_then(Vec::pop) {
            Some(v) => {
                self.bytes -= cap * std::mem::size_of::<Value>();
                v
            }
            None => Vec::with_capacity(cap),
        }
    }

    /// A hash part of `slots` empty nodes.
    pub(crate) fn nodes(&mut self, slots: usize) -> Vec<crate::table::Node> {
        match self.nodes.get_mut(slots).and_then(Vec::pop) {
            Some(mut v) => {
                self.bytes -= slots * std::mem::size_of::<crate::table::Node>();
                v.resize(slots, crate::table::Node::default());
                v
            }
            None => vec![crate::table::Node::default(); slots],
        }
    }

    /// Keeps the buffers of a table that died, while the pool holds under `limit` bytes.
    pub(crate) fn give(&mut self, t: &mut Table, limit: usize) {
        let limit = limit.min(POOL_BYTES);
        let mut arr = t.arr.take_backing();
        let cap = arr.capacity();
        let size = cap * std::mem::size_of::<Value>();
        if (1..=POOL_MAX).contains(&cap) && self.bytes + size <= limit {
            if self.arrs.len() <= cap {
                self.arrs.resize_with(cap + 1, Vec::new);
            }
            if let Some(class) = self.arrs.get_mut(cap)
                && class.len() < POOL_PER_SIZE
            {
                arr.clear();
                class.push(arr);
                self.bytes += size;
            }
        }
        let mut nodes = std::mem::take(&mut t.nodes);
        let len = nodes.len();
        let size = len * std::mem::size_of::<crate::table::Node>();
        if (1..=POOL_MAX).contains(&len) && nodes.capacity() == len && self.bytes + size <= limit {
            if self.nodes.len() <= len {
                self.nodes.resize_with(len + 1, Vec::new);
            }
            if let Some(class) = self.nodes.get_mut(len)
                && class.len() < POOL_PER_SIZE
            {
                nodes.clear();
                class.push(nodes);
                self.bytes += size;
            }
        }
    }
}

impl Mem {
    /// The limit that stops the running call now (its interrupt or deadline), if any: for
    /// a table resize to give up between pieces, the table left as it was.
    #[cold]
    pub(crate) fn stopped(&self) -> Option<crate::LimitKind> {
        self.deadline.as_ref().and_then(crate::stdlib::pattern::Watch::stop)
    }

    /// A buffer of `PARK_BYTES` or more freed a piece at a time between looks at the stop,
    /// in the account until its pages go: freeing one is the kernel's unmapping, about 5 ms
    /// per 100 MiB, with no look (LR-29b: a resize's old part of 2^21 nodes, 2 to 3 ms past
    /// its last look, 26 ms at 512 MiB). As much as goes before a stop is freed now; the
    /// rest at the next looks (`Lua::refuel`). No fuel (a free never was), and not live
    /// (slack): the collector's pacing is as when it was freed whole.
    pub(crate) fn park(&mut self, p: Parked) {
        self.slack_in(p.bytes());
        self.freeing.push(p);
        self.unpark();
    }

    /// The parked buffers freed `PARK_PIECE` bytes at a time (shrunk in place, their tail
    /// pages unmapped: no copy), oldest first, a look at the stop after each piece.
    pub(crate) fn unpark(&mut self) {
        while let Some(p) = self.freeing.first_mut() {
            let (before, gone) = (p.bytes(), p.shrink());
            let freed = before - p.bytes();
            if gone {
                self.freeing.remove(0);
            }
            self.slack_out(freed);
            if self.stopped().is_some() {
                return;
            }
        }
    }

    /// When an operation to trace starts (None: no trace).
    #[inline]
    pub(crate) fn trace_begin(&self) -> Option<std::time::Duration> {
        self.trace.as_ref().map(|t| t.now())
    }

    /// Records an operation that started at `start` (`trace_begin`), in the step under way.
    #[cold]
    pub(crate) fn trace_note(&mut self, op: crate::gctrace::TraceOp, slots: u64, bytes: u64, start: Option<std::time::Duration>) {
        if let (Some(t), Some(start)) = (self.trace.as_mut(), start) {
            let step = t.step;
            t.note(op, step, slots, bytes, 0, start);
        }
    }

    /// A new account under `cap`.
    pub(crate) fn new(cap: usize, debt: isize) -> Mem {
        Mem { used: 0, cap, debt, refused: false, headroom: 0, stress: false, pool: Pool::default(), slack: 0, cycle: 0, marking: false, obj_work: 0, watch: Watch::default(), deadline: None, fuel: usize::MAX, fuel_watch: None, fuel_watchable: false, partials: Vec::new(), freeing: Vec::new(), trace: None }
    }

    /// Bytes the live objects hold (what the collector paces on).
    #[inline]
    pub fn live(&self) -> usize {
        self.used.saturating_sub(self.slack)
    }

    /// Whether `n` more bytes fit under the cap (the pool given back first if they would
    /// not).
    #[inline]
    pub fn fits(&mut self, n: usize) -> bool {
        self.used.saturating_add(n) <= self.cap.saturating_add(self.headroom) || self.fits_drained(n)
    }

    #[cold]
    fn fits_drained(&mut self, n: usize) -> bool {
        self.drain_pool();
        self.used.saturating_add(n) <= self.cap.saturating_add(self.headroom)
    }

    /// The pool's buffers freed and its bytes out of the account.
    #[cold]
    pub(crate) fn drain_pool(&mut self) {
        self.slack_out(self.pool.bytes);
        self.pool = Pool::default();
    }

    /// `Pool::arr`, the buffer's bytes out of the slack.
    #[inline]
    pub(crate) fn pool_arr(&mut self, cap: usize) -> Vec<Value> {
        let held = self.pool.bytes;
        let v = self.pool.arr(cap);
        self.slack_out(held - self.pool.bytes);
        v
    }

    /// `Pool::nodes`, the buffer's bytes out of the slack.
    #[inline]
    pub(crate) fn pool_nodes(&mut self, slots: usize) -> Vec<crate::table::Node> {
        let held = self.pool.bytes;
        let v = self.pool.nodes(slots);
        self.slack_out(held - self.pool.bytes);
        v
    }

    /// `Pool::give`, the buffers kept counted as slack.
    pub(crate) fn pool_give(&mut self, t: &mut Table) {
        let held = self.pool.bytes;
        self.pool.give(t, self.cap / 16);
        self.slack_in(self.pool.bytes - held);
    }

    /// A slot of `n` bytes freed (into the slack) or reused or cut (out of it).
    #[inline]
    pub(crate) fn slack_in(&mut self, n: usize) {
        // (Bounded by the slabs' slots: no overflow.)
        self.slack += n;
        self.used += n;
    }

    #[inline]
    pub(crate) fn slack_out(&mut self, n: usize) {
        self.slack -= n;
        self.used = self.used.saturating_sub(n);
    }

    /// Accounts for `n` more bytes, or refuses when they would pass the cap.
    #[inline]
    pub fn charge(&mut self, n: usize) -> Result<(), ()> {
        let after = self.used.saturating_add(n);
        if after > self.cap.saturating_add(self.headroom) {
            return self.charge_drained(n);
        }
        self.used = after;
        self.debt = self.debt.saturating_add(isize::try_from(n).unwrap_or(isize::MAX));
        if self.stress {
            self.debt = self.debt.max(1);
        }
        Ok(())
    }

    /// `charge` past the cap: the pool given back, then the charge or the refusal.
    #[cold]
    #[inline(never)]
    fn charge_drained(&mut self, n: usize) -> Result<(), ()> {
        if self.pool.bytes == 0 || !self.fits_drained(n) {
            self.refused = true;
            return Err(());
        }
        self.charge(n)
    }

    /// Accounts for `n` fewer bytes.
    #[inline]
    pub fn release(&mut self, n: usize) {
        self.used = self.used.saturating_sub(n);
    }

    /// Settles the account of an object charged `charged` that now holds `actual`.
    pub fn settle(&mut self, charged: usize, actual: usize) {
        if actual > charged {
            self.used = self.used.saturating_add(actual - charged);
        } else {
            self.used = self.used.saturating_sub(charged - actual);
        }
    }
}

/// A string: its bytes, their hash and whether it is the interner's copy. Short strings
/// (at most [`SHORT_LEN`] bytes) are always interned, so two of them are equal exactly when
/// they are the same object. A long string is made without hashing or interning, as in C
/// Lua; it is hashed and interned only when used as a table key (tables hold interned keys
/// only), so equal long strings may be different objects: compare them by content.
#[derive(Default)]
pub(crate) struct LStr {
    pub bytes: Bytes,
    /// The seeded hash of the bytes; 0 until computed.
    pub(crate) hash: std::cell::Cell<u64>,
    pub interned: bool,
    /// The interned copy last found for a long string that is not interned itself, so a
    /// key lookup compares its bytes once, not once per lookup (checked live before use).
    twin: std::cell::Cell<Option<StrRef>>,
}

pub(crate) const SHORT_LEN: usize = 40;

/// Bytes a string keeps inside its object, without an allocation of their own.
const INLINE: usize = 22;

/// A string's bytes: short ones (most strings a script makes) inside the object, longer
/// ones in their own allocation.
pub(crate) enum Bytes {
    Inline { len: u8, buf: [u8; INLINE] },
    Heap(Box<[u8]>),
}

impl Default for Bytes {
    fn default() -> Self {
        Bytes::Inline { len: 0, buf: [0; INLINE] }
    }
}

impl std::ops::Deref for Bytes {
    type Target = [u8];
    #[inline]
    fn deref(&self) -> &[u8] {
        match self {
            Bytes::Inline { len, buf } => buf.get(..usize::from(*len)).unwrap_or_default(),
            Bytes::Heap(b) => b,
        }
    }
}

impl From<&[u8]> for Bytes {
    fn from(b: &[u8]) -> Self {
        let mut buf = [0; INLINE];
        match buf.get_mut(..b.len()) {
            Some(dst) => {
                dst.copy_from_slice(b);
                Bytes::Inline { len: b.len() as u8, buf }
            }
            None => Bytes::Heap(b.into()),
        }
    }
}

impl From<Box<[u8]>> for Bytes {
    fn from(b: Box<[u8]>) -> Self {
        if b.len() <= INLINE { Bytes::from(&*b) } else { Bytes::Heap(b) }
    }
}
/// A string object's own memory: its slab entry (`LStr`, generation, color, occupancy) and
/// its interner slot, measured resident at about 64 bytes (sweep 6: the old 40 charged a
/// 7-byte string 47 bytes against about 70 resident).
pub(crate) const STR_BASE: usize = 64;

/// The memory a string of `len` bytes is charged (and released when swept): the object,
/// plus, past the bytes kept inline, the allocation's header and rounding (16 bytes).
#[inline]
pub(crate) const fn str_mem(len: usize) -> usize {
    STR_BASE + len + if len > INLINE { 16 } else { 0 }
}
pub(crate) const FUNC_BASE: usize = 48;

/// What a boxed slice of `bytes` bytes takes (SEC-M9: a closure's upvalue list was charged
/// its 8 bytes a reference, against the allocation's header and rounding: a closure over
/// one upvalue held 32 bytes for 8 charged). Empty boxes allocate nothing.
#[inline]
pub(crate) const fn boxed(bytes: usize) -> usize {
    if bytes == 0 { 0 } else { bytes.div_ceil(16) * 16 + ALLOC_OVERHEAD }
}
pub(crate) const UPVAL_SIZE: usize = 40;
/// A userdata's own record (LR-17: it was a flat 48 against about 90 bytes): its slab slot
/// (the record, its generation and color) and the box holding the host's value (the
/// allocation's header and least block; the value's own bytes are `size`).
pub(crate) const UD_BASE: usize = std::mem::size_of::<UserData>() + 5 + 32;
/// What one allocation costs beyond its payload: the allocator's header and rounding.
pub(crate) const ALLOC_OVERHEAD: usize = 16;
/// A host handle's root (LR-17): its `Arc<()>` (counts and the allocation's header) and
/// its entry in `Lua::roots`.
pub(crate) const ROOT_BYTES: usize = 16 + ALLOC_OVERHEAD + std::mem::size_of::<(std::sync::Weak<()>, crate::value::Value)>();
/// A thread's own record: its state (boxed: the slab slot holds the pointer) and the box's
/// allocation, at least 256. It was a flat 256 while the state had grown to 272 bytes
/// (304 now): the charge follows the type.
pub(crate) const THREAD_BASE: usize = {
    let need = std::mem::size_of::<ThreadState>() + std::mem::size_of::<Box<ThreadState>>() + 16;
    if need > 256 { need } else { 256 }
};

/// A host function: reentrant, so it may be called again from script code it calls.
pub(crate) type HostFn = Arc<dyn Fn(&mut crate::vm::Vm, crate::vm::Args) -> Result<crate::vm::Flow, crate::vm::LuaError> + Send + Sync>;

/// A function object.
pub(crate) enum Func {
    Lua { proto: u32, upvals: Box<[crate::value::FuncRef]> },
    /// `in_place`: 1 + its index in `stdlib::string::IN_PLACE` for a library function a
    /// call from Lua code runs in place (`Lua::call_in_place`), else 0.
    Native { f: NativeFn, upvals: Box<[Value]>, name: &'static str, in_place: u8 },
    /// `bytes`: what it is charged beyond `FUNC_BASE` (its closure's and name's allocations,
    /// LR-17), given back when it is swept.
    Host { f: HostFn, name: Arc<str>, bytes: usize },
}

impl Default for Func {
    fn default() -> Self {
        Func::Native { f: crate::vm::native_dead, upvals: Box::new([]), name: "?", in_place: 0 }
    }
}

/// An upvalue: open (a live register of a thread) or closed (its own value). Upvalues are
/// kept in the function slab's sibling, `upvals`; a Lua closure lists them by index.
#[derive(Clone, Copy)]
pub(crate) enum Upval {
    Open { thread: ThreadRef, idx: usize },
    Closed(Value),
}

impl Default for Upval {
    fn default() -> Self {
        Upval::Closed(Value::Nil)
    }
}

/// A full userdata.
#[derive(Default)]
pub(crate) struct UserData {
    pub data: Option<Box<dyn Any + Send>>,
    pub meta: Option<TableRef>,
    pub size: usize,
    /// The host's key to report when this userdata is collected (opt-in).
    pub notify: Option<u64>,
}

/// A coroutine's status.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub(crate) enum CoStatus {
    /// Made, or yielded: can be resumed.
    #[default]
    Suspended,
    /// Running now.
    Running,
    /// Resumed another coroutine and waits for it.
    Normal,
    /// Finished or failed.
    Dead,
}

/// Where the results of a pending call go when a thread continues: the stack slot of the
/// called function and how many results the caller wants (-1: all).
#[derive(Clone, Copy, Debug)]
pub(crate) struct Pending {
    /// The resume (or wrap) call came from Lua code (for error positions).
    pub from_lua: bool,
    pub func: usize,
    pub nresults: i32,
    /// `coroutine.wrap`: errors propagate instead of returning `false, err`.
    pub wrap: bool,
    /// How the call's results are placed (a `pcall` of `resume` or a wrap function adds
    /// `true` and catches).
    pub kind: crate::vm::FrameKind,
}

/// A thread's state: its stack, frames and open upvalues.
#[derive(Default)]
pub(crate) struct ThreadState {
    pub stack: Vec<Value>,
    pub frames: Vec<Frame>,
    /// Open upvalues, sorted by stack index.
    pub open_upvals: Vec<(usize, u32)>,
    /// Stack slots of to-be-closed variables, in order.
    pub tbc: Vec<usize>,
    pub status: CoStatus,
    /// Who resumed it.
    pub resumer: Option<ThreadRef>,
    /// An upper bound of the C levels (C Lua's `nCcalls`) the threads that resumed it hold,
    /// set by each resume (`Lua::c_bound_here`): below C Lua's limit, a resume need not count
    /// them exactly. A resumer cannot run while this thread does, so it stays valid until
    /// the next resume.
    pub c_bound: usize,
    /// The `coroutine.yield` call waiting for the next resume's values.
    pub yield_pending: Option<Pending>,
    /// The `coroutine.resume` call waiting for this thread's next yield or end.
    pub resume_pending: Option<Pending>,
    /// Execution loops entered from Rust while this thread ran: it cannot yield past them.
    pub nny: u32,
    /// Not started: the body sits in stack[0].
    pub fresh: bool,
    /// The error that killed it.
    pub error: Option<Value>,
    /// Died by an error raised in a library function: that function's depth, stack slot
    /// and value (a level of its traceback, as in C Lua; LIBS-4).
    pub died_in: Option<(usize, usize, Value)>,
    /// Frames below this index were there when it was last resumed after a yield: C Lua
    /// "unrolls" their C levels then (a protected call's C function is gone from the C
    /// stack; its continuation runs from the resume), so they no longer count towards
    /// `Lua::c_levels` (S5B-OPEN-3). Lowered when a protected or continued frame is pushed
    /// below it (`push_frame`: only such frames count, and only the slow path makes them).
    pub unrolled: usize,
}

/// Bytes charged per call frame record as the frame list grows (S5GC-7): the record's
/// size when this was set. The record is smaller now (128 bytes); the charge stays, so
/// memory accounting, the collector's pacing and with them fuel are unchanged.
pub(crate) const FRAME_BYTES: usize = 136;
const _: () = assert!(std::mem::size_of::<Frame>() <= FRAME_BYTES);

/// Gives back a frame list's room past twice what it holds (after deep recursion), as
/// the stack's is (S5GC-7).
pub(crate) fn shrink_frames(frames: &mut Vec<Frame>, mem: &mut Mem) {
    let cap = frames.capacity();
    let want = frames.len() * 2;
    if cap <= want * 2 {
        return;
    }
    frames.shrink_to(want);
    mem.release(cap.saturating_sub(frames.capacity()) * FRAME_BYTES);
}

impl ThreadState {
    pub fn mem_size(&self) -> usize {
        THREAD_BASE + self.stack.capacity() * SLOT_SIZE + self.frames.capacity() * FRAME_BYTES
    }
}

/// A per-slot vector of a slab that grows without copying all of itself in one push: from
/// 3/4 full it is copied to one of twice its capacity a stretch at a time (writes to what is
/// copied go to both), and that one takes its place when it is full. (Grown by `Vec`'s
/// doubling, the generations and free-list positions of a slab of 1M strings were copied
/// in the push that made the 1,048,577th, 2.4 ms each and 5 ms together, past the
/// host's interrupt; `items` grows in place, its buffer being the system's.)
pub(crate) struct Ahead<T> {
    cur: Vec<T>,
    /// The copy of `cur`'s first `next.len()` elements, with room for twice `cur`'s.
    next: Vec<T>,
    /// The length at which a push next copies a stretch, swaps, or lets `cur` grow by
    /// itself (`push_ahead`): one comparison for every other push.
    mark: usize,
}

/// Elements of a per-slot vector past which it grows ahead (`Ahead`), the pushes between
/// copied stretches, and a stretch: all of it is copied within `capacity / 16` pushes, so a
/// full vector's copy lacks at most the last `AHEAD_EVERY - 1` elements. (A stretch at
/// every push past 3/4 full cost string_build 4% more instructions; one every 64 behind a
/// single comparison, 0.0 to 0.25% across string_ops, string_build, json and gc_churn.)
const AHEAD_FROM: usize = 1 << 16;
const AHEAD_EVERY: usize = 64;
const AHEAD_STRETCH: usize = 16 * AHEAD_EVERY;

impl<T: Copy> Ahead<T> {
    const fn new() -> Self {
        Ahead { cur: Vec::new(), next: Vec::new(), mark: 0 }
    }

    #[inline]
    pub(crate) fn push(&mut self, v: T) {
        if self.cur.len() < self.mark {
            self.cur.push(v);
        } else {
            self.push_ahead(v);
        }
    }

    /// `push` past 3/4 full (every `AHEAD_EVERY`th): with the next stretch copied, or the
    /// copy taking `cur`'s place.
    #[inline(never)]
    fn push_ahead(&mut self, v: T) {
        let cap = self.cur.capacity();
        if self.cur.len() == cap && cap >= AHEAD_FROM {
            // Full: the copy takes its place (finished first if it is not, as after a
            // `shrink_to`).
            if self.next.capacity() <= cap {
                self.next = Vec::with_capacity(2 * cap);
            }
            let from = self.next.len();
            self.next.extend_from_slice(self.cur.get(from..).unwrap_or_default());
            self.cur = std::mem::take(&mut self.next);
        }
        self.cur.push(v);
        let cap = self.cur.capacity();
        if self.cur.len() * 4 >= cap * 3 && cap >= AHEAD_FROM {
            if self.next.capacity() <= cap {
                self.next = Vec::with_capacity(2 * cap);
            }
            let from = self.next.len();
            let to = (from + AHEAD_STRETCH).min(self.cur.len());
            self.next.extend_from_slice(self.cur.get(from..to).unwrap_or_default());
        }
        let len = self.cur.len();
        self.mark = if cap < AHEAD_FROM {
            // Small: grows by itself, when full.
            cap
        } else if len * 4 < cap * 3 {
            (cap * 3).div_ceil(4)
        } else {
            (len + AHEAD_EVERY).min(cap)
        };
    }

    /// Sets element `i` (if there is one), in the copy too.
    pub(crate) fn set(&mut self, i: usize, v: T) {
        if let Some(x) = self.cur.get_mut(i) {
            *x = v;
        }
        if let Some(x) = self.next.get_mut(i) {
            *x = v;
        }
    }

    pub(crate) fn truncate(&mut self, n: usize) {
        self.cur.truncate(n);
        self.next.truncate(n);
        self.mark = self.mark.min(n);
    }

    pub(crate) fn capacity(&self) -> usize {
        self.cur.capacity()
    }

    /// Gives back room past `n` elements (and the copy, which starts again later).
    pub(crate) fn shrink_to(&mut self, n: usize) {
        self.next = Vec::new();
        self.cur.shrink_to(n);
        self.mark = 0;
    }
}

impl<T> std::ops::Deref for Ahead<T> {
    type Target = [T];
    fn deref(&self) -> &[T] {
        &self.cur
    }
}

/// A slab's objects: one `Vec` (its growth is the system's `realloc`, which moves a big
/// one's pages without a copy), or on wasm, where `realloc` copies (19 ms for 32 MiB),
/// chunks that never move (`Chunked`; natively under the `segmented-arrays` test build).
#[cfg(not(any(target_family = "wasm", feature = "segmented-arrays")))]
type Items<T> = Vec<T>;
#[cfg(any(target_family = "wasm", feature = "segmented-arrays"))]
type Items<T> = Chunked<T>;

/// Objects in chunks of `CHUNK` that never move once full: the first grows as a `Vec`
/// does up to `CHUNK`, the rest are made at `CHUNK` and filled (SEC8-S1: a slab of 2M
/// tables doubled from 144 to 288 MiB in one `realloc`, a copy on wasm).
#[cfg(any(target_family = "wasm", feature = "segmented-arrays"))]
pub(crate) struct Chunked<T> {
    chunks: Vec<Vec<T>>,
    len: usize,
}

#[cfg(feature = "segmented-arrays")]
const CHUNK_SHIFT: u32 = 10;
#[cfg(all(target_family = "wasm", not(feature = "segmented-arrays")))]
const CHUNK_SHIFT: u32 = 16;
#[cfg(any(target_family = "wasm", feature = "segmented-arrays"))]
const CHUNK: usize = 1 << CHUNK_SHIFT;

#[cfg(any(target_family = "wasm", feature = "segmented-arrays"))]
thread_local! {
    /// Objects a slab's storage moved on this thread (only its first chunk's growth can),
    /// for `Lua::slab_objects_moved`.
    static MOVED: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}

/// The objects slabs' storage moved since the last call (a test probe).
#[cfg(any(target_family = "wasm", feature = "segmented-arrays"))]
pub(crate) fn take_moved() -> u64 {
    MOVED.with(std::cell::Cell::take)
}

#[cfg(any(target_family = "wasm", feature = "segmented-arrays"))]
impl<T> Chunked<T> {
    const fn new() -> Self {
        Chunked { chunks: Vec::new(), len: 0 }
    }

    #[inline]
    fn len(&self) -> usize {
        self.len
    }

    fn capacity(&self) -> usize {
        match self.chunks.split_first() {
            Some((first, rest)) => first.capacity() + rest.len() * CHUNK,
            None => 0,
        }
    }

    #[inline]
    fn get(&self, i: usize) -> Option<&T> {
        self.chunks.get(i >> CHUNK_SHIFT)?.get(i & (CHUNK - 1))
    }

    #[inline]
    fn get_mut(&mut self, i: usize) -> Option<&mut T> {
        self.chunks.get_mut(i >> CHUNK_SHIFT)?.get_mut(i & (CHUNK - 1))
    }

    fn push(&mut self, v: T) {
        let k = self.len >> CHUNK_SHIFT;
        if k == self.chunks.len() {
            // (A new chunk made whole past the first: nothing in it moves again.)
            self.chunks.push(if k == 0 { Vec::new() } else { Vec::with_capacity(CHUNK) });
        }
        if let Some(c) = self.chunks.get_mut(k) {
            if c.len() == c.capacity() {
                MOVED.with(|m| m.set(m.get() + c.len() as u64));
            }
            c.push(v);
            self.len += 1;
        }
    }

    fn truncate(&mut self, n: usize) {
        if n >= self.len {
            return;
        }
        self.chunks.truncate(n.div_ceil(CHUNK).max(1));
        let base = (self.chunks.len() - 1) << CHUNK_SHIFT;
        if let Some(c) = self.chunks.last_mut() {
            c.truncate(n - base);
        }
        self.len = n;
    }

    /// Room past `n` slots given back: the chunks past them, and the first's own.
    fn shrink_to(&mut self, n: usize) {
        let keep = n.max(self.len).div_ceil(CHUNK).max(1);
        self.chunks.truncate(keep);
        if let [first] = &mut self.chunks[..] {
            first.shrink_to(n);
        }
    }

    fn swap(&mut self, a: usize, b: usize) {
        let (ka, kb) = (a >> CHUNK_SHIFT, b >> CHUNK_SHIFT);
        let (ia, ib) = (a & (CHUNK - 1), b & (CHUNK - 1));
        if ka == kb {
            if let Some(c) = self.chunks.get_mut(ka) {
                c.swap(ia, ib);
            }
            return;
        }
        let (lo, hi, il, ih) = if ka < kb { (ka, kb, ia, ib) } else { (kb, ka, ib, ia) };
        let (left, right) = self.chunks.split_at_mut(hi);
        if let (Some(x), Some(y)) = (left.get_mut(lo).and_then(|c| c.get_mut(il)), right.first_mut().and_then(|c| c.get_mut(ih))) {
            std::mem::swap(x, y);
        }
    }
}

/// A slab of one object kind.
pub(crate) struct Slab<T> {
    items: Items<T>,
    pub gens: Ahead<u32>,
    pub color: Vec<u8>,
    pub free: Vec<u32>,
    /// The generation new slots start at: per state (so another state's handles do not
    /// match), raised by `trim` past every generation it cuts.
    floor: u32,
    /// The first `floor`: generations are compared by their distance from it.
    origin: u32,
    /// A bit per slot holding an object, and a bit per word of those that is not zero:
    /// the collector's walks skip free slots 4,096 at a time (`next_used`, S5GC-2).
    used: Vec<u64>,
    used_words: Vec<u64>,
    /// Each free slot's position in `free`, so `trim` takes the slots it cuts out of the
    /// free list one by one. As long as `items`; read only for free slots (an allocation
    /// leaves it stale).
    free_at: Ahead<u32>,
    /// Bytes a free slot holds, counted in `Mem::slack` (SEC-M5).
    pub(crate) slot_bytes: usize,
}


impl<T> Slab<T> {
    /// The object in slot `i` (a slot index, not a handle: no generation checked).
    #[inline]
    pub(crate) fn at(&self, i: usize) -> Option<&T> {
        self.items.get(i)
    }

    /// The object in slot `i`, to change in place.
    #[inline]
    pub(crate) fn at_mut(&mut self, i: usize) -> Option<&mut T> {
        self.items.get_mut(i)
    }

    /// The slots: the objects and the free slots below the last.
    #[inline]
    pub(crate) fn slots(&self) -> usize {
        self.items.len()
    }

    /// Slots there is room for before the storage grows.
    pub(crate) fn slot_capacity(&self) -> usize {
        self.items.capacity()
    }

    /// The objects in slots `a` and `b` swapped (the running thread's state and another's).
    pub(crate) fn swap_slots(&mut self, a: usize, b: usize) {
        self.items.swap(a, b);
    }
}

impl<T: Default> Slab<T> {
    /// Bytes a free slot holds: its item, generation and color, its free-list entry and
    /// position, and its bits in the bitmaps (rounded up to a byte).
    pub(crate) const SLOT_BYTES: usize = std::mem::size_of::<T>() + 4 + 1 + 4 + 4 + 1;

    fn new(first_gen: u32) -> Self {
        Self::with_slot_extra(first_gen, 0)
    }

    /// A slab whose free slot holds `extra` bytes more than `SLOT_BYTES` (an empty item
    /// that allocates: a free thread slot holds a fresh `Box<ThreadState>`).
    fn with_slot_extra(first_gen: u32, extra: usize) -> Self {
        Slab {
            slot_bytes: Self::SLOT_BYTES + extra,
            free_at: Ahead::new(),
            items: Items::new(),
            gens: Ahead::new(),
            color: Vec::new(),
            free: Vec::new(),
            floor: first_gen,
            origin: first_gen,
            used: Vec::new(),
            used_words: Vec::new(),
        }
    }

    fn set_used(&mut self, i: usize) {
        let (w, b) = (i / 64, i % 64);
        if self.used.len() <= w {
            self.used.resize(w + 1, 0);
            self.used_words.resize(w / 64 + 1, 0);
        }
        if let Some(x) = self.used.get_mut(w) {
            *x |= 1 << b;
        }
        if let Some(x) = self.used_words.get_mut(w / 64) {
            *x |= 1 << (w % 64);
        }
    }

    fn clear_used(&mut self, i: usize) {
        let (w, b) = (i / 64, i % 64);
        if let Some(x) = self.used.get_mut(w) {
            *x &= !(1 << b);
            if *x == 0
                && let Some(y) = self.used_words.get_mut(w / 64)
            {
                *y &= !(1 << (w % 64));
            }
        }
    }

    /// The first slot at or after `from` that holds an object (`items.len()` if none).
    /// Free slots used to be walked one by one at no cost (S5GC-2): a slab left with one
    /// live object in 1,000 after a spike made every collection cycle walk all of it, 15x
    /// to 30x plain code per unit of fuel under a 256 MB limit and more under a larger one
    /// (charging the walk instead would make a program's fuel depend on what the state
    /// did before).
    pub(crate) fn next_used(&self, from: usize) -> usize {
        let end = self.slots();
        let mut w = from / 64;
        if let Some(&x) = self.used.get(w) {
            let m = x & (!0u64).checked_shl((from % 64) as u32).unwrap_or(0);
            if m != 0 {
                return (w * 64 + m.trailing_zeros() as usize).min(end);
            }
        }
        w += 1;
        loop {
            let Some(&s) = self.used_words.get(w / 64) else { return end };
            let m = s & (!0u64).checked_shl((w % 64) as u32).unwrap_or(0);
            if m != 0 {
                let w2 = (w / 64) * 64 + m.trailing_zeros() as usize;
                let x = self.used.get(w2).copied().unwrap_or(0);
                return (w2 * 64 + x.trailing_zeros() as usize).min(end);
            }
            w = (w / 64 + 1) * 64;
        }
    }

    /// One past the last slot that holds an object (0 if none). S5B-WALK-2: `trim` walked
    /// the free slots at the end one by one after every sweep, and gave nothing back while
    /// they were under a quarter of the slab: a slab of 2 million with its last live object
    /// at 80% walked 400,000 free slots a cycle for no work, 5x plain code per unit of fuel
    /// at a short pause and more under a larger memory limit. The bitmap skips them 4,096
    /// at a time, as `next_used` does.
    fn end_of_used(&self) -> usize {
        for (wi, &s) in self.used_words.iter().enumerate().rev() {
            if s != 0 {
                let w = wi * 64 + 63 - s.leading_zeros() as usize;
                let x = self.used.get(w).copied().unwrap_or(0);
                return (w * 64 + 64 - x.leading_zeros() as usize).min(self.slots());
            }
        }
        0
    }

    /// Gives back the free slots at the end (after a spike of objects died), so the
    /// process's memory returns to what is live instead of staying at its peak.
    ///
    /// After an incremental cycle only a tail of a quarter of the slab (or 1,024 slots)
    /// is cut, so a slab that shrinks and grows each cycle reuses its slots instead of
    /// giving them back and making them again. `all` (a full collection) cuts every free
    /// slot at the end, however few: they are counted against the cap while they stay
    /// (SEC-M5), so a full collection leaves the account where it was before the objects
    /// were made. The work is the slots cut, each of which an allocation paid for: each
    /// leaves the free list through its position in `free_at` (a `retain` walked the whole
    /// free list, the cut slots or not).
    ///
    /// `trim_piece` until nothing is left to cut: the whole cut in one go, with no look
    /// at a stop. The unit tests drive a slab this way; the collector cuts piece by
    /// piece through `Heap::trim_slabs` (SEC7-H3).
    #[cfg(test)]
    fn trim(&mut self, mem: &mut Mem, all: bool) {
        while self.trim_piece(mem, all, usize::MAX).1 {}
    }

    /// At most `most` slots off the end: the slots cut, and whether more are to be cut
    /// (`all` then, the cut decided). (S6GC: 2,000,000 slots cut in one go
    /// took 60 ms without a look at the deadline.)
    fn trim_piece(&mut self, mem: &mut Mem, all: bool, most: usize) -> (usize, bool) {
        let len = self.slots();
        let end = self.end_of_used();
        if end >= len {
            return if all { self.shrink_piece() } else { (0, false) };
        }
        if !all && len - end < (len / 4).max(1024) {
            return (0, false);
        }
        let keep = end.max(len.saturating_sub(most));
        for j in keep..len {
            let Some(&pos) = self.free_at.get(j) else { continue };
            let pos = pos as usize;
            if pos >= self.free.len() {
                continue;
            }
            self.free.swap_remove(pos);
            if let Some(&moved) = self.free.get(pos)
                && self.free_at.get(moved as usize).is_some()
            {
                self.free_at.set(moved as usize, pos as u32);
            }
        }
        // A slot made again past `keep` must not get back a generation a stale handle to
        // one of its objects holds (SEC4-10). The floor goes past every cut generation (by
        // distance from `origin`, so a state whose generations start near `u32::MAX` wraps
        // nothing), and the cut generations go. A generation moves at most once a cycle,
        // so the floor reaches `FLOOR_LIMIT` only after some 2^31 cycles that cut slots;
        // from there `gens` keeps the cut slots' generations instead (each made again
        // goes on from its own, as before SEC-M5), 4 bytes a slot, counted. (Raising the
        // floor saturating at `u32::MAX` once gave every slot made again the same
        // generation, so stale handles matched again.)
        let dist = |g: u32| g.wrapping_sub(self.origin);
        let most = self.gens.get(keep..len).unwrap_or_default().iter().map(|&g| dist(g)).max().unwrap_or(0);
        mem.slack_out((len - keep) * self.slot_bytes);
        if self.gens.len() == len && most < FLOOR_LIMIT {
            if most >= dist(self.floor) {
                self.floor = self.origin.wrapping_add(most + 1);
            }
            self.gens.truncate(keep);
            if self.gens.capacity() > keep * 2 {
                self.gens.shrink_to(keep + keep / 2);
            }
        } else {
            mem.slack_in((len - keep) * GEN_BYTES);
        }
        self.items.truncate(keep);
        self.color.truncate(keep);
        self.free_at.truncate(keep);
        // (The cut slots are free: their bits are clear already.)
        self.used.truncate(keep.div_ceil(64));
        self.used_words.truncate(self.used.len().div_ceil(64));
        if keep > end {
            return (len - keep, true);
        }
        let (w, more) = self.shrink_piece();
        (len - keep + w, more)
    }

    /// Gives back the slots' room past twice those used, down to 1.5 times, at most
    /// `SHRINK_BYTES` of it at a time: the slots' worth given back (a unit a page), and
    /// whether more is to be. (S6GC: the 2,097,152-slot room of a table slab, 300 MB, given back in
    /// one go took 15 to 22 ms.)
    fn shrink_piece(&mut self) -> (usize, bool) {
        let keep = self.slots();
        let cap = self.slot_capacity();
        if cap <= keep * 2 {
            if self.free.capacity() > self.free.len() * 2 + 64 {
                self.free.shrink_to_fit();
            }
            return (0, false);
        }
        let size = std::mem::size_of::<T>().max(1);
        let to = (keep + keep / 2).max(cap - (SHRINK_BYTES / size).min(cap));
        self.free_at.shrink_to(to);
        self.items.shrink_to(to);
        self.color.shrink_to(to);
        ((cap - to) * size / 4096, true)
    }

    fn alloc(&mut self, item: T, mem: &mut Mem, color_for: impl Fn(usize) -> u8) -> (u32, u32) {
        if let Some((i, generation, slot)) = self.take_free(mem, &color_for) {
            *slot = item;
            return (i, generation);
        }
        let i = self.slots();
        self.set_used(i);
        self.items.push(item);
        self.free_at.push(0);
        let generation = match self.gens.get(i) {
            Some(&g) => {
                // A cut slot's kept generation: the object's now (no longer slack).
                mem.slack_out(GEN_BYTES);
                g
            }
            None => {
                self.gens.push(self.floor);
                self.floor
            }
        };
        self.color.push(color_for(i));
        (i as u32, generation)
    }

    /// A freed slot taken for a new object (it holds `T::default()`, for the caller to fill
    /// in place: a table built elsewhere and moved in was two copies and a drop), with its
    /// index and generation.
    fn take_free(&mut self, mem: &mut Mem, color_for: impl Fn(usize) -> u8) -> Option<(u32, u32, &mut T)> {
        let i = self.free.pop()?;
        let iu = i as usize;
        *self.color.get_mut(iu)? = color_for(iu);
        mem.slack_out(self.slot_bytes);
        self.set_used(iu);
        let generation = self.gens.get(iu).copied().unwrap_or(0);
        Some((i, generation, self.at_mut(iu)?))
    }

    #[inline]
    pub fn get(&self, idx: u32) -> Option<&T> {
        self.at(idx as usize)
    }

    #[inline]
    pub fn get_mut(&mut self, idx: u32) -> Option<&mut T> {
        self.at_mut(idx as usize)
    }


    pub fn live(&self, idx: u32, generation: u32) -> bool {
        self.gens.get(idx as usize) == Some(&generation)
            && self.color.get(idx as usize).is_some_and(|&c| c != FREE)
    }

    /// Frees slot `i`, which holds an object.
    fn free_slot(&mut self, i: usize, mem: &mut Mem) {
        mem.slack_in(self.slot_bytes);
        if let Some(slot) = self.at_mut(i) {
            *slot = T::default();
        }
        if let Some(&g) = self.gens.get(i) {
            self.gens.set(i, g.wrapping_add(1));
        }
        if let Some(c) = self.color.get_mut(i) {
            *c = FREE;
        }
        self.clear_used(i);
        let at = self.free.len() as u32;
        self.free_at.set(i, at);
        self.free.push(i as u32);
    }
}

/// The collector's phase.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum Phase {
    Pause,
    Propagate,
    Sweep,
}

/// Objects made while marking is incremental (gray, as marked), and the work they owe.
///
/// COV-5: each was traversed as collector work, and how many are made while marking lasts
/// follows when it ends, which follows the order objects are reached in (the hash seed):
/// 259 closures and upvalues more under one seed than another, 1,570 units of fuel. Every
/// allocation pays its traversal as made instead, in any phase, and that traversal is not
/// counted.
#[derive(Default)]
pub(crate) struct Born {
    pub list: Vec<Gray>,
    /// The work of every object made, and how much of it was charged (`take_work`) and
    /// left to the collector's steps (`take_step`): a marking step leaves the latter of
    /// its budget to the other objects, as gray objects took their part before (their
    /// actual traversals follow when marking runs, so the seed; this, the allocations).
    total: usize,
    charged: usize,
    stepped: usize,
    /// `Heap::obj_work`, for what a traversal costs.
    pub obj_work: usize,
}

/// Born work charged as it comes, not only by collector steps (none run in a pause, or
/// with the collector stopped): a call would make objects its fuel does not pay for.
const BORN_DUE: usize = 1 << 12;

impl Born {
    /// Object `g` made, its traversal walking `walked` slots, upvalues or constants.
    #[inline]
    pub(crate) fn add(&mut self, phase: Phase, g: Gray, walked: usize) {
        let per = if matches!(g, Gray::Table(_)) { 1 + self.obj_work / 4 } else { 1 };
        self.total += self.obj_work + 1 + walked * per;
        if phase == Phase::Propagate {
            self.list.push(g);
        }
    }

    /// The work not charged yet.
    pub(crate) fn pending(&self) -> usize {
        self.total - self.charged
    }

    /// The work not charged yet, now charged.
    pub(crate) fn take_work(&mut self) -> usize {
        self.total - std::mem::replace(&mut self.charged, self.total)
    }

    /// The work since the last collector step.
    pub(crate) fn take_step(&mut self) -> usize {
        self.total - std::mem::replace(&mut self.stepped, self.total)
    }
}

#[derive(Clone, Copy, Debug)]
pub(crate) enum Gray {
    Proto(u32),
    Table(u32),
    /// The rest of a big table's traversal, from slot `.1` (`TABLE_CHUNK`, S6GC-2e).
    TablePart(u32, u32),
    Func(u32),
    Upval(u32),
    Ud(u32),
    Thread(u32),
}

/// The string interner: an open-addressing set of string slots keyed by content.
///
/// A regrow does not place every string again at once: it sets the slots aside (`old`)
/// and each new string moves a bounded number of them to the new slots, while lookups and
/// removals walk both (S6T-REGROW: a regrow placed every string in one insert, with no
/// look at the host's interrupt or deadline in a script or a host `load`: 15 to 17 ms at
/// 600,000 strings, 52 to 58 ms at 1.6 million, where making 2M strings went 51 to 82 ms
/// between looks at the deadline; now the longest insert is under a millisecond, giving
/// back the old slots, and the interner keeps nothing waiting).
pub(crate) struct Interner {
    /// Each slot: a string's index, inverted (the low 32 bits: `EMPTY` is 0, so empty
    /// slots are zeroes, written ahead as `spare` or zeroed by the system; `TOMB`), and
    /// the high 32 bits of its hash, so a walk compares without reading the strings.
    pub(crate) slots: Vec<u64>,
    /// The slots before the last regrow while their strings move to `slots` (empty when
    /// all have moved), in order: a moved one is left removed (`TOMB`), so walks go on past
    /// it. (In order, as a whole regrow placed them: their homes in `slots` follow theirs
    /// in `old`, and so the new slots fill a page at a time; moved from the end down, so
    /// the end could be cut off as it went, they took twice as long.)
    pub(crate) old: Vec<u64>,
    /// `old`'s salt, and the next of its slots to move.
    old_salt: u64,
    old_next: usize,
    /// Slots of `old` each new string moves: all of it moves within `slots.len() / 16`
    /// new strings, well before the next regrow (at least `slots.len() / 8` away). (Within
    /// `slots.len() / 64`, the moves crowded into the fuel slices of the move: 2.7 to 3.2 ms
    /// between looks at the deadline making 2M strings; 1.9 to 2.1 ms now. The fuel is the
    /// count's, `regrow_share`, either way.)
    moves: usize,
    /// The next regrow's slots, written a stretch at a time as the slots fill up from 5/8
    /// toward 3/4 (`write_spare`). Slots zeroed by the system are mapped a page at a
    /// time, but a page read before it is first written is mapped twice (once to read,
    /// again to write), and walks read the slots all over: making 2M strings took 16,000
    /// more page faults and 75 ms more than with slots written at once; written at once,
    /// 32 MB of slots kept the interrupt waiting 12 ms.
    spare: Vec<u64>,
    /// The strings in both.
    pub(crate) count: usize,
    /// The most there were since the last sweep ended, as the atomic step found them (the
    /// cycle's dead strings not yet swept: a cycle's most): slots are given back only when
    /// that is an eighth of them (a count that falls at each sweep and climbs again before
    /// the next, strings made and dropped, would give them back and grow them again every
    /// cycle: concat's 600,000 short strings, +2% instructions). (Not kept at each insert:
    /// +0.4% on string_build.)
    pub(crate) peak: usize,
    /// The bytes of the slots a shrink set aside, counted until they are given back.
    shrunk: usize,
    /// The removed slots in `slots`.
    tombs: usize,
    /// Mixed into each hash's home slot: a new one (and the slots placed again) when an
    /// insert walks past `walk_bound`, as a table's hash part takes a new seed (S5GC-3).
    salt: u64,
    /// The length of `find`'s last walk (in `slots`), for `insert`.
    walked: std::cell::Cell<usize>,
    /// Strings inserted since the slots were last placed.
    since_salt: usize,
    /// The interned strings of `GROUP_FROM` bytes or more by whole hash and length, where
    /// two or more have one (`Crit`).
    groups: std::collections::HashMap<(u64, usize), Crit>,
    /// Where the bytes of the last string not found (by whole hash and length) first
    /// differ from the member they could have equaled, and that member's byte there: for
    /// `insert` of that string just after, which so compares nothing again.
    last_diff: std::cell::Cell<Option<(u64, usize, u32, usize, u8)>>,
}

/// S5GC-3: interner walks were free, and a script that knows the hash seed
/// (`Limits::hash_seed`, or recovered from `math.random`: S5GC-4) makes strings with any
/// hash it likes: the byte chunks of a string fold into its hash one after another, so a
/// chunk can cancel the one before and any number of strings share one hash. With 4,000
/// of them, each `..` that made one again walked them all (290x plain code per unit of
/// fuel), and interning them was quadratic (in `load` too). Now:
/// - strings aimed at one run by their low bits only spread again under a new salt when
///   an insert walks past `walk_bound` (charged as the rebuild it is);
/// - a walk is fuel for its steps past `walk_bound`, and for every step over a string
///   with the same hash bits (which random strings never meet, so neither depends on the
///   seed in practice);
///
/// Both are charged with the table probe steps (and, in a compile, as its scan work).
#[cold]
fn intern_excess(n: usize) {
    crate::table::add_summed(n as u64);
    crate::diag::compile_work(n);
}

/// The farthest an interner walk goes before it is fuel or makes a new salt: past what
/// random strings reach at the interner's 3/4 load (open addressing: some 270 slots at
/// 2^20).
#[inline]
fn walk_bound(len: usize) -> usize {
    64 + 16 * len.max(2).ilog2() as usize
}

/// The fuel each new string pays toward placing the interned strings again (`regrow`) in
/// an interner holding `count` strings. A regrow places every string at a random slot,
/// reading its hash from the string (30 to 40 ns a string at a million: cache misses), and
/// regrows come at most every `len / 4` new strings (a regrow leaves at most half the
/// slots used, the next comes when they are 3/4 used, and only a new string uses a slot),
/// so a new string costs at most two placements, beside its own walk's misses. Regrows
/// were free but for their excess walks: making strings that die beside two million live
/// ones ran at 2.7x plain code per unit of fuel with the fast loop and 3.2x without (2.2
/// to 2.7x beside a million, 3.2 to 3.9x beside three million elsewhere); 1.8x and 2.1x
/// with a share of 4 past a million, but 2.5 to 2.6x again with the rest of the tests
/// running beside it (the walks and placements miss caches the other processes share).
/// The share now grows with the interner's size, as a table's `place_cost` does: 0.8 to
/// 1.0x alone, 0.4 to 1.2x beside the whole suite (the regrow moving a stretch at a time
/// with the spare slots written ahead, `Interner`). Charging a regrow when it happens
/// would follow the seed (whether a new string takes a removed one's slot depends on the
/// layout, and with it when the slots fill up: one program's regrows came at 538,068 to
/// 538,793 strings across seeds), so each new string pays its share by the count, which
/// is the program's; so does the stretch it moves, by the same count.
#[inline]
fn regrow_share(count: usize) -> usize {
    match count {
        c if c >= 1 << 21 => 10,
        c if c >= 1 << 20 => 8,
        c if c >= 1 << 19 => 5,
        c if c >= 1 << 18 => 3,
        _ => 0,
    }
}

/// The fuel a walk of `steps`, `same` of them over strings with the same hash bits, costs.
#[inline]
fn walk_cost(steps: usize, same: usize, len: usize) -> usize {
    steps.saturating_sub(walk_bound(len)).max(same)
}

/// A slot's string index.
#[inline]
fn slot_idx(s: u64) -> u32 {
    !(s as u32)
}

/// The slot holding string `idx`, whose hash is `hash`.
#[inline]
fn slot_of(hash: u64, idx: u32) -> u64 {
    slot_bits(hash) | u64::from(!idx)
}

/// The bits of a hash a slot keeps.
#[inline]
fn slot_bits(hash: u64) -> u64 {
    hash & !0xffff_ffff
}

/// The slot a hash's walk starts at, in slots of `mask + 1` under `salt`.
#[inline]
fn home_in(hash: u64, salt: u64, mask: usize) -> usize {
    (fold_mul(hash ^ salt, 0x9E37_79B9_7F4A_7C15) as usize) & mask
}

/// Walks `slots` from `i` for the string with these bytes: its index, or the slot to put it
/// in (the first removed one on the way, else the empty one that ends the walk); with the
/// steps walked and how many were over strings with the same hash bits. For bytes of
/// `GROUP_FROM` or more it stops at the first string of the same whole hash and length
/// without comparing them: which of those it is, if any, is the group's to say (`Crit`).
#[inline(always)]
fn walk(
    slots: &[u64],
    mut i: usize,
    mask: usize,
    strings: &Slab<LStr>,
    hash: u64,
    bytes: &[u8],
) -> (Result<u32, usize>, usize, usize) {
    let bits = slot_bits(hash);
    let mut first_tomb = None;
    let (mut steps, mut same) = (0usize, 0usize);
    let r = loop {
        let s = slots.get(i).copied().unwrap_or(EMPTY);
        if s == EMPTY {
            break Err(first_tomb.unwrap_or(i));
        }
        if s == TOMB {
            first_tomb.get_or_insert(i);
        } else if slot_bits(s) == bits {
            if let Some(st) = strings.get(slot_idx(s))
                && st.hash.get() == hash
                && (if bytes.len() >= GROUP_FROM { st.bytes.len() == bytes.len() } else { *st.bytes == *bytes })
            {
                break Ok(slot_idx(s));
            }
            same += 1;
        }
        i = (i + 1) & mask;
        steps += 1;
    };
    (r, steps, same)
}

/// Walks `slots` from `i` to the slot a new string with this hash goes in (the first
/// removed one, else the empty one that ends the walk), past every string: the slot, the
/// steps and how many were over strings with the same hash bits.
fn walk_free(slots: &[u64], mut i: usize, mask: usize, hash: u64) -> (usize, usize, usize) {
    let mut first_tomb = None;
    let (mut steps, mut same) = (0usize, 0usize);
    loop {
        let s = slots.get(i).copied().unwrap_or(EMPTY);
        if s == EMPTY {
            return (first_tomb.unwrap_or(i), steps, same);
        }
        if s == TOMB {
            first_tomb.get_or_insert(i);
        } else if slot_bits(s) == slot_bits(hash) {
            same += 1;
        }
        i = (i + 1) & mask;
        steps += 1;
    }
}

/// Bytes from which interned strings of one whole hash and length are told apart by a
/// crit-bit tree (`Crit`) instead of by comparing each one's bytes in turn (S6T-EQ): under
/// a known seed, a program can make any number of strings with one hash and length, and
/// a lookup compared its bytes with each (any number of keys' worth of bytes, free). With
/// the tree a lookup compares them with one string at most. (Below, a comparison costs
/// about what a walk's step does, and each is charged as one: `walk_cost`.)
pub(crate) const GROUP_FROM: usize = 128;

/// Fuel for comparing or copying `n` bytes (`Lua::charge_bytes`, and the interner's
/// comparisons, SEC-M11).
#[inline]
pub(crate) fn bytes_fuel(n: usize) -> u64 {
    if n > 256 { n as u64 / 64 } else { 0 }
}

/// The interned strings of one whole hash and length (`GROUP_FROM` bytes or more), when
/// there are two or more: a crit-bit tree over their bytes, each node the first byte and
/// bit where its two sides differ. Looking a string up reads one byte a node and ends at
/// the one member it can equal. A program not aimed at the seed has none (a lone string
/// of its hash and length needs none: kept for each, they made 200-byte keys 21% slower).
enum Crit {
    Leaf(u32),
    Node { at: usize, bit: u8, kids: Box<[Crit; 2]> },
}

/// The side of a crit-bit node (byte `at`, bit `bit`) bytes `b` go to.
#[inline]
fn crit_side(b: &[u8], at: usize, bit: u8) -> bool {
    b.get(at).is_some_and(|&x| x & bit != 0)
}

/// The first byte where `a` and `b` differ (None: equal), comparing `from..to` only.
pub(crate) fn first_diff(a: &[u8], b: &[u8], from: usize, to: usize) -> Option<usize> {
    #[cfg(test)]
    COMPARED.with(|c| c.set(c.get() + to.saturating_sub(from)));
    // (Either out of range: they differ, at `from`.)
    let (Some(x), Some(y)) = (a.get(from..to), b.get(from..to)) else { return Some(from) };
    // (Slices compared whole, then by chunks to the byte: a byte at a time made looking
    // up 200-byte keys 23% slower.)
    if x == y {
        return None;
    }
    let mut off = from;
    for (p, q) in x.chunks(256).zip(y.chunks(256)) {
        if p != q {
            return p.iter().zip(q).position(|(m, n)| m != n).map(|k| off + k);
        }
        off += p.len();
    }
    Some(off)
}

#[cfg(test)]
thread_local! {
    /// Bytes `first_diff` was asked to compare (for the tests).
    static COMPARED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}

/// Memory a crit-bit node takes (its two sides' box) and a group's map entry: charged as
/// they are made (sweep 6 review: under a collision attack, about 48 bytes per string
/// were outside the account).
const CRIT_NODE: usize = 2 * std::mem::size_of::<Crit>() + 16;
const GROUP_ENTRY: usize = std::mem::size_of::<((u64, usize), Crit)>() + 8;

impl Crit {
    /// The member bytes `b` can equal. Each node walked is a walk's step, charged: with the
    /// seed known a tree can be a chain as deep as its strings (S6R-5), and the slots'
    /// walk, which charged each string of the same hash bits, now stops at the first.
    fn best(&self, b: &[u8]) -> u32 {
        let mut n = self;
        let mut depth = 0;
        loop {
            match n {
                Crit::Leaf(i) => {
                    intern_excess(depth);
                    return *i;
                }
                Crit::Node { at, bit, kids } => {
                    depth += 1;
                    let [k0, k1] = &**kids;
                    n = if crit_side(b, *at, *bit) { k1 } else { k0 };
                }
            }
        }
    }

    /// Adds member `idx` (bytes `b`), which first differs from `best(b)` at byte `at`,
    /// where that member has byte `other`.
    fn insert(&mut self, idx: u32, b: &[u8], at: usize, other: u8) {
        let x = b.get(at).copied().unwrap_or(0) ^ other;
        let bit = 0x80u8 >> x.leading_zeros().min(7);
        let mut n = self;
        let mut depth = 0;
        loop {
            let side = match &*n {
                Crit::Node { at: a, bit: c, .. } if *a < at || (*a == at && *c > bit) => crit_side(b, *a, *c),
                _ => break,
            };
            depth += 1;
            let Crit::Node { kids, .. } = n else { break };
            let [k0, k1] = &mut **kids;
            n = if side { k1 } else { k0 };
        }
        intern_excess(depth);
        let old = std::mem::replace(n, Crit::Leaf(u32::MAX));
        let kids = if crit_side(b, at, bit) { [old, Crit::Leaf(idx)] } else { [Crit::Leaf(idx), old] };
        *n = Crit::Node { at, bit, kids: Box::new(kids) };
    }

    /// Takes member `idx` (bytes `b`) out of a tree of two or more: the nodes walked, and
    /// whether it was there (a node went).
    fn remove(&mut self, idx: u32, b: &[u8]) -> (usize, bool) {
        let mut n = self;
        let mut depth = 0;
        loop {
            depth += 1;
            let (side, hit) = match &*n {
                Crit::Node { at, bit, kids } => {
                    let side = crit_side(b, *at, *bit);
                    let [k0, k1] = &**kids;
                    (side, matches!(if side { k1 } else { k0 }, Crit::Leaf(i) if *i == idx))
                }
                Crit::Leaf(_) => return (depth, false),
            };
            if hit {
                // The node goes, its other side in its place.
                if let Crit::Node { kids, .. } = n {
                    let [k0, k1] = &mut **kids;
                    *n = std::mem::replace(if side { k0 } else { k1 }, Crit::Leaf(u32::MAX));
                }
                return (depth, true);
            }
            let Crit::Node { kids, .. } = n else { return (depth, false) };
            let [k0, k1] = &mut **kids;
            n = if side { k1 } else { k0 };
        }
    }
}

/// Freed a node at a time: a script that knows the seed can make its tree a chain as deep
/// as it has strings, and dropped a level per call it overflowed the native stack (S6R-5).
impl Drop for Crit {
    fn drop(&mut self) {
        let Crit::Node { kids, .. } = self else { return };
        let mut todo: Vec<Crit> = kids.iter_mut().map(|k| std::mem::replace(k, Crit::Leaf(0))).collect();
        while let Some(mut n) = todo.pop() {
            if let Crit::Node { kids, .. } = &mut n {
                todo.extend(kids.iter_mut().map(|k| std::mem::replace(k, Crit::Leaf(0))));
            }
            // (`n` goes here, its sides leaves now.)
        }
    }
}

/// Walks `slots` from `i` for string `idx` (hash `hash`) and marks its slot removed:
/// whether it was there, the steps walked and how many were over the same hash bits.
fn remove_in(slots: &mut [u64], mut i: usize, mask: usize, hash: u64, idx: u32) -> (bool, usize, usize) {
    let (mut steps, mut same) = (0, 0);
    loop {
        match slots.get(i).copied() {
            None | Some(EMPTY) => return (false, steps, same),
            Some(s) if s != TOMB && slot_idx(s) == idx => {
                if let Some(slot) = slots.get_mut(i) {
                    *slot = TOMB;
                }
                return (true, steps, same);
            }
            Some(s) => {
                if s != TOMB && slot_bits(s) == slot_bits(hash) {
                    same += 1;
                }
                i = (i + 1) & mask;
                steps += 1;
            }
        }
    }
}

impl Interner {
    fn new() -> Interner {
        Interner {
            slots: vec![EMPTY; 256],
            old: Vec::new(),
            old_salt: 0,
            old_next: 0,
            moves: 0,
            spare: Vec::new(),
            count: 0,
            peak: 0,
            shrunk: 0,
            tombs: 0,
            salt: 0,
            walked: std::cell::Cell::new(0),
            since_salt: 0,
            groups: std::collections::HashMap::new(),
            last_diff: std::cell::Cell::new(None),
        }
    }

    /// The slot a hash's walk starts at.
    #[inline]
    fn home(&self, hash: u64) -> usize {
        home_in(hash, self.salt, self.slots.len() - 1)
    }

    /// Where a walk in `old` for this hash starts, while strings are left there.
    #[inline]
    fn old_home(&self, hash: u64) -> Option<usize> {
        (!self.old.is_empty()).then(|| home_in(hash, self.old_salt, self.old.len() - 1))
    }

    /// The interned string with these bytes (its index), or the slot to put it in.
    #[inline(always)]
    fn find(&self, strings: &Slab<LStr>, hash: u64, bytes: &[u8]) -> Result<u32, usize> {
        let len = self.slots.len();
        let (r, steps, same) = walk(&self.slots, self.home(hash), len - 1, strings, hash, bytes);
        self.walked.set(steps - same);
        let mut cost = walk_cost(steps, same, len);
        let r = match r {
            Err(slot) if !self.old.is_empty() => match self.old_home(hash) {
                Some(i) => {
                    let (o, steps, same) = walk(&self.old, i, self.old.len() - 1, strings, hash, bytes);
                    cost += walk_cost(steps, same, self.old.len());
                    o.map_err(|_| slot)
                }
                None => Err(slot),
            },
            r => r,
        };
        let r = match r {
            Ok(c) if bytes.len() >= GROUP_FROM => {
                // A string of the same whole hash and length: the one member these bytes can
                // equal, compared once.
                let best = if self.groups.is_empty() { c } else { self.group_best(strings, hash, bytes).unwrap_or(c) };
                let other = strings.get(best).map_or(&[][..], |st| &st.bytes[..]);
                let diff = first_diff(bytes, other, 0, bytes.len());
                // SEC-M11: the bytes compared are paid for (with the probe steps). A miss
                // records nothing, so a key that differs from an interned one of its whole
                // hash and length only at its end (made with a seed the script knows) was
                // compared to its end at every lookup, free. Only strings of one whole hash
                // and length are compared, so fuel follows no seed for a program not aimed
                // at one.
                cost += bytes_fuel(diff.unwrap_or(bytes.len())) as usize;
                match diff {
                    None => Ok(best),
                    Some(at) => {
                        let ob = other.get(at).copied().unwrap_or(0);
                        self.last_diff.set(Some((hash, bytes.len(), best, at, ob)));
                        let (slot, c) = self.free_slot(hash);
                        cost += c;
                        Err(slot)
                    }
                }
            }
            r => r,
        };
        if cost > 0 {
            intern_excess(cost);
        }
        r
    }

    /// The interned string `bytes` (`GROUP_FROM` or more, of this whole hash) can equal: the
    /// member of their group its bytes lead to, else the lone one of that hash and length.
    fn group_best(&self, strings: &Slab<LStr>, hash: u64, bytes: &[u8]) -> Option<u32> {
        if let Some(g) = self.groups.get(&(hash, bytes.len())) {
            return Some(g.best(bytes));
        }
        // (Uncharged: the lookup that follows walks the same slots, charged as before.)
        let (r, _, _) = walk(&self.slots, self.home(hash), self.slots.len() - 1, strings, hash, bytes);
        match r {
            Ok(c) => Some(c),
            Err(_) => {
                let i = self.old_home(hash)?;
                walk(&self.old, i, self.old.len() - 1, strings, hash, bytes).0.ok()
            }
        }
    }

    /// The slot a new string with this hash goes in, past the strings of its hash: the slot
    /// and the walk's cost.
    fn free_slot(&self, hash: u64) -> (usize, usize) {
        let len = self.slots.len();
        let (slot, steps, same) = walk_free(&self.slots, self.home(hash), len - 1, hash);
        (slot, walk_cost(steps, same, len))
    }

    /// The slot for a string of `GROUP_FROM` bytes or more known to be in no slots (its bytes
    /// compared with the one member they could equal: first differing at `at`, where that
    /// member has `other`), for `insert` just after.
    fn slot_for_absent(&self, hash: u64, len: usize, best: Option<(u32, usize, u8)>) -> usize {
        if let Some((m, at, ob)) = best {
            self.last_diff.set(Some((hash, len, m, at, ob)));
        }
        let (slot, cost) = self.free_slot(hash);
        if cost > 0 {
            intern_excess(cost);
        }
        slot
    }

    /// Puts string `idx` in `slot` (from `find`, just before): the bytes its group took.
    fn insert(&mut self, strings: &Slab<LStr>, slot: usize, idx: u32) -> usize {
        let mut held = 0;
        let hash = strings.get(idx).map_or(0, |s| s.hash.get());
        if let Some(st) = strings.get(idx)
            && st.bytes.len() >= GROUP_FROM
        {
            let b = &st.bytes[..];
            let hint = self.last_diff.take();
            match if self.groups.is_empty() { None } else { self.groups.get_mut(&(hash, b.len())) } {
                None => {
                    // A second of this hash and length (the lookup just before compared it
                    // with the first): their group.
                    if let Some((h, l, m, at, ob)) = hint
                        && h == hash
                        && l == b.len()
                    {
                        let mut g = Crit::Leaf(m);
                        g.insert(idx, b, at, ob);
                        self.groups.insert((hash, b.len()), g);
                        held = GROUP_ENTRY + CRIT_NODE;
                    }
                }
                Some(g) => {
                    let best = g.best(b);
                    // Where these bytes first differ from the member they could equal: from
                    // the lookup just before (none compares them again), else compared now.
                    let diff = match hint {
                        Some((h, l, m, at, ob)) if h == hash && l == b.len() && m == best => Some((at, ob)),
                        _ => strings.get(best).and_then(|o| first_diff(b, &o.bytes, 0, b.len()).map(|at| (at, o.bytes.get(at).copied().unwrap_or(0)))),
                    };
                    if let Some((at, ob)) = diff {
                        g.insert(idx, b, at, ob);
                        held = CRIT_NODE;
                    }
                }
            }
        }
        if let Some(s) = self.slots.get_mut(slot) {
            if *s == TOMB {
                self.tombs -= 1;
            }
            *s = slot_of(hash, idx);
        }
        self.count += 1;
        let share = regrow_share(self.count);
        if share > 0 {
            intern_excess(share);
        }
        if !self.old.is_empty() {
            self.move_some(strings, self.moves);
        }
        let used = self.count + self.tombs;
        if used * 8 > self.slots.len() * 5 {
            self.write_spare(used);
        }
        if used * 4 > self.slots.len() * 3 {
            self.regrow(strings, ((self.count + 1) * 2).next_power_of_two().max(256), self.salt);
        } else if self.walked.get() > walk_bound(self.slots.len()) && self.since_salt > self.slots.len() / 8 {
            // Strings aimed at one run (by the low bits of their hashes): a new salt, from
            // this one so runs replay, and every string placed again, charged as the work
            // it is. At most once per `len / 8` strings: strings with one whole hash stay
            // together under any salt, and walks past the bound are charged anyway.
            intern_excess(self.slots.len() / 16);
            self.regrow(strings, self.slots.len(), mix_seed(self.salt, 0x5a17));
        }
        self.since_salt += 1;
        held
    }

    /// Takes string `idx` (hash `hash`) out: the work its walk costs past a plain one, and
    /// the bytes its group gave back.
    fn remove(&mut self, strings: &Slab<LStr>, hash: u64, idx: u32) -> (usize, usize) {
        let mut crit = 0;
        let mut freed = 0;
        if !self.groups.is_empty()
            && let Some(st) = strings.get(idx)
            && st.bytes.len() >= GROUP_FROM
            && let Some(g) = self.groups.get_mut(&(hash, st.bytes.len()))
        {
            let (walked, went) = g.remove(idx, &st.bytes);
            crit = walked;
            freed = if went { CRIT_NODE } else { 0 };
            // One left: alone of its hash and length again.
            if matches!(g, Crit::Leaf(_)) {
                self.groups.remove(&(hash, st.bytes.len()));
                freed += GROUP_ENTRY;
            }
        }
        let len = self.slots.len();
        let home = self.home(hash);
        let (found, steps, same) = remove_in(&mut self.slots, home, len - 1, hash, idx);
        let mut cost = walk_cost(steps, same, len) + crit;
        if found {
            self.count -= 1;
            self.tombs += 1;
        } else if let Some(i) = self.old_home(hash) {
            let mask = self.old.len() - 1;
            let (found, steps, same) = remove_in(&mut self.old, i, mask, hash, idx);
            cost += walk_cost(steps, same, mask + 1);
            if found {
                self.count -= 1;
            }
        }
        (cost, freed)
    }

    /// Sets the slots aside for new ones of `size` under `salt`: their strings move a few
    /// at a time (`move_some`).
    fn regrow(&mut self, strings: &Slab<LStr>, size: usize, salt: u64) {
        if !self.old.is_empty() {
            // (Not reached: the slots set aside before are moved well before this.)
            self.move_some(strings, usize::MAX);
        }
        let new = if self.spare.len() >= size {
            let mut v = std::mem::take(&mut self.spare);
            if v.len() > size {
                // (Fewer strings than the spare was written for: its end goes back.)
                v.truncate(size);
                v.shrink_to_fit();
            }
            v
        } else {
            vec![EMPTY; size]
        };
        let old = std::mem::replace(&mut self.slots, new);
        self.moves = (16 * old.len()).div_ceil(size) + 1;
        self.old_salt = self.salt;
        self.old = old;
        self.old_next = 0;
        self.salt = salt;
        self.tombs = 0;
        self.since_salt = 0;
    }

    /// Writes the next stretch of the spare slots, `used` of the slots being used: as many
    /// as the next regrow can want (the most strings there can be by then, when every new
    /// string until then is kept), all of them within `slots.len() / 8` new strings, before
    /// the slots are 3/4 used.
    fn write_spare(&mut self, used: usize) {
        let want = self.spare_want(used);
        if self.spare.len() >= want {
            return;
        }
        if self.spare.capacity() < want {
            self.spare = Vec::with_capacity(want);
        }
        let n = (self.spare.len() + 32).min(want);
        self.spare.resize(n, EMPTY);
    }

    /// The most slots the next regrow can want, `used` of the slots being used.
    fn spare_want(&self, used: usize) -> usize {
        let len = self.slots.len();
        let most = self.count + (len * 3 / 4 + 1).saturating_sub(used);
        ((most + 1) * 2).next_power_of_two().clamp(256, 2 * len)
    }

    /// Moves up to `n` of `old`'s slots to `slots` (`n` units of work).
    fn move_some(&mut self, strings: &Slab<LStr>, n: usize) {
        let mask = self.slots.len() - 1;
        let mut excess = 0usize;
        let end = self.old_next.saturating_add(n).min(self.old.len());
        while self.old_next < end {
            let Some(x) = self.old.get_mut(self.old_next) else { break };
            self.old_next += 1;
            let s = *x;
            if s == EMPTY || s == TOMB {
                continue;
            }
            // (Empty ones stay empty, so walks there end where they did.)
            *x = TOMB;
            let h = strings.get(slot_idx(s)).map_or(0, |x| x.hash.get());
            let mut i = self.home(h);
            let (mut steps, mut same) = (0usize, 0usize);
            while let Some(&x) = self.slots.get(i)
                && x != EMPTY
                && x != TOMB
            {
                if slot_bits(x) == slot_bits(s) {
                    same += 1;
                }
                i = (i + 1) & mask;
                steps += 1;
            }
            excess += walk_cost(steps, same, mask + 1);
            if let Some(slot) = self.slots.get_mut(i) {
                if *slot == TOMB {
                    self.tombs -= 1;
                }
                *slot = s;
            }
        }
        if self.old_next >= self.old.len() {
            // All moved: kept to be written again as the spare when the next regrow can
            // use it (strings dying as fast as new ones come regrow at one size), else
            // given back. (Given back and taken again at every regrow, gc_churn's large
            // frees and the allocator's merging of small blocks at each nearly doubled.)
            let mut v = std::mem::take(&mut self.old);
            // (Not after a shrink: the next regrow wants at most twice the slots.)
            if v.capacity() >= self.spare_want(self.count + self.tombs) && v.capacity() > self.spare.capacity() && v.capacity() <= 2 * self.slots.len() {
                v.clear();
                self.spare = v;
            }
        }
        if excess > 0 {
            intern_excess(excess);
        }
    }
}

/// An empty interner slot, and one whose string was removed (by their index part).
const EMPTY: u64 = 0;
const TOMB: u64 = 1;

#[cfg(test)]
mod interner_tests {
    use super::{EMPTY, Heap, TOMB, slot_idx, tail_word};

    /// A hash's tail word is the bytes' little-endian word for every length (as the loop
    /// it replaced made it): the same hashes, so the same layouts and fuel.
    #[test]
    fn tail_words_are_the_bytes_little_endian() {
        let bytes: Vec<u8> = (0u8..12).map(|i| i.wrapping_mul(37) ^ 0xa5).collect();
        for n in 0..=bytes.len() {
            let t = bytes.get(..n).unwrap_or_default();
            let want = t.iter().take(8).enumerate().fold(0u64, |w, (i, &x)| w | u64::from(x) << (8 * i));
            assert_eq!(tail_word(t), want, "{n} bytes");
        }
    }

    /// S6T-EQ (security): strings of one whole hash and length, made with the seed known
    /// (each's last two words cancel: the second undoes what the first did to the hash),
    /// any number of them interned. Interning one, looking one up (found or not) and
    /// taking one out compares at most one string's worth of bytes (a lookup compared
    /// them with each in turn), and every lookup finds just the one it should.
    #[test]
    fn strings_of_one_hash_compare_one_string_at_most() {
        let mut h = Heap::new(1 << 30, 0x5eed);
        let len = 4096usize;
        let seed = h.seed;
        let fill = vec![b'x'; len - 16];
        let hp = super::hash_words(seed, super::hash_start(seed, len), &fill);
        let (k1, k2) = (seed ^ 0xa076_1d64_78bd_642f, seed.rotate_left(32) ^ 0xe703_7ed1_a0b4_28db);
        let make = |i: u64| {
            let w1 = 0x4141_4141_0000_0000 | i;
            let h1 = super::fold_mul(hp ^ w1 ^ k1, k2);
            let w2 = h1 ^ 0x1234_5678_9abc_def0;
            let mut b = fill.clone();
            b.extend_from_slice(&w1.to_le_bytes());
            b.extend_from_slice(&w2.to_le_bytes());
            b
        };
        let all: Vec<Vec<u8>> = (0..65).map(make).collect();
        let hash = super::hash_bytes(seed, &all[0]);
        assert!(all.iter().all(|b| super::hash_bytes(seed, b) == hash && b.len() == len), "one hash");
        let compared = || super::COMPARED.with(std::cell::Cell::get);
        let mut refs = Vec::new();
        for b in &all[..64] {
            let before = compared();
            refs.push(h.intern(b).unwrap());
            assert!(compared() - before <= len, "interning compared {} bytes", compared() - before);
        }
        for (i, b) in all[..64].iter().enumerate() {
            let before = compared();
            let hb = super::hash_bytes(seed, b);
            assert_eq!(h.interner.find(&h.strings, hb, b), Ok(refs[i].idx), "string {i}");
            assert!(compared() - before <= len, "a lookup compared {} bytes", compared() - before);
        }
        let before = compared();
        assert!(h.interner.find(&h.strings, hash, &all[64]).is_err(), "the one not interned");
        assert!(compared() - before <= len, "a lookup compared {} bytes", compared() - before);
        // Some taken out (nothing compared), the rest still found, those gone not.
        let before = compared();
        for r in refs.iter().step_by(3) {
            h.unintern(r.idx);
        }
        assert_eq!(compared(), before, "taking out compares nothing");
        for (i, b) in all[..64].iter().enumerate() {
            let found = h.interner.find(&h.strings, hash, b);
            if i % 3 == 0 {
                assert!(found.is_err(), "string {i} taken out");
            } else {
                assert_eq!(found, Ok(refs[i].idx), "string {i}");
            }
        }
    }

    /// S6T-REGROW: while a regrow's slots move, every string is found in whichever slots
    /// hold it, a string taken out of the old slots is gone (and counted out once), each
    /// new string moves a bounded stretch, and the move ends well before the next regrow.
    #[test]
    fn strings_move_a_stretch_at_a_time_and_stay_found() {
        let mut h = Heap::new(1 << 30, 0x5eed);
        let base = h.interner.count;
        let name = |i: usize| format!("s{i}").into_bytes();
        let mut refs = Vec::new();
        let (mut moves_seen, mut removed) = (0usize, Vec::new());
        for i in 0..200_000 {
            let was = h.interner.old.len();
            refs.push(h.intern(&name(i)).unwrap());
            let i_old = &h.interner;
            if !i_old.old.is_empty() {
                moves_seen += 1;
                // Bounded: at most `moves` old slots per new string.
                assert!(
                    i_old.old_next <= i_old.old.len()
                        && i_old.moves <= 16 * i_old.old.len() / i_old.slots.len() + 1
                );
                if was == 0 {
                    assert_eq!(i_old.old.len() * 2, i_old.slots.len(), "a regrow at {i} doubled");
                }
                // Takes out a string still in the old slots, now and then.
                if i % 97 == 0
                    && let Some(&s) =
                        i_old.old.iter().skip(i_old.old_next).find(|&&s| s != EMPTY && s != TOMB)
                {
                    let before = h.interner.count;
                    h.unintern(slot_idx(s));
                    assert_eq!(h.interner.count, before - 1);
                    removed.push(slot_idx(s));
                }
            }
        }
        assert!(moves_seen > 100, "moves seen in {moves_seen} inserts");
        assert!(!removed.is_empty());
        for (i, r) in refs.iter().enumerate() {
            let hash = h.strings.get(r.idx).unwrap().hash.get();
            let found = h.interner.find(&h.strings, hash, &name(i));
            if removed.contains(&r.idx) {
                assert!(found.is_err(), "string {i} found after its removal");
            } else {
                assert_eq!(found, Ok(r.idx), "string {i}");
            }
        }
        assert_eq!(h.interner.count, base + refs.len() - removed.len());
        let live =
            h.interner.slots.iter().chain(&h.interner.old).filter(|&&s| s != EMPTY && s != TOMB).count();
        assert_eq!(live, h.interner.count, "the count is the strings in the slots");
    }
}

/// The objects and the collector.
pub(crate) struct Heap {
    pub strings: Slab<LStr>,
    pub tables: Slab<Table>,
    pub funcs: Slab<Func>,
    pub upvals: Slab<Upval>,
    pub uds: Slab<UserData>,
    pub threads: Slab<Box<ThreadState>>,
    /// Function prototypes (code, constants, nested prototypes).
    pub protos: Slab<Proto>,
    pub(crate) interner: Interner,
    pub mem: Mem,
    pub(crate) seed: u64,
    /// The seed of table key hashing (see `value::hash_key`), given to each table made.
    pub(crate) table_seed: u64,
    /// Keys of host-tracked userdata collected since the host last drained them.
    pub collected: Vec<u64>,
    pub phase: Phase,
    pub gray: Vec<Gray>,
    pub born: Born,
    pub grayagain: Vec<Gray>,
    /// Weak tables met while marking: (table, weak keys, weak values).
    weak: Vec<(u32, bool, bool)>,
    /// Sweep position: (slab number, index).
    pub(crate) sweep: (u8, usize),
    /// Cycle number, for prototypes' marks.
    pub cycle: u32,
    /// Bytes live after the last cycle.
    pub estimate: usize,
    /// No collection may run (the compiler holds unrooted constants).
    pub blocked: u32,
    /// `collectgarbage("stop")`: no automatic steps.
    pub stopped: bool,
    /// `setpause`: the next cycle starts when the heap reaches this percentage of what
    /// was live after the last one.
    pub pause: usize,
    /// `setstepmul`: collector work per byte allocated, in percent.
    pub stepmul: usize,
    /// Interned `__mode` key for weak-table checks.
    pub mode_key: Option<StrRef>,
    /// While the atomic step settles ephemerons: values waiting for their (unmarked) key,
    /// by key object, so marking a key marks its values at once (linear, not quadratic).
    /// Ephemeron values waiting for their (unmarked) key: the head of each key's list in
    /// `eph_links` (value, next; `u32::MAX` ends a list). Flat, so clearing it after a
    /// cycle frees nothing per entry (a map of a `Vec` per key took 30 ms to drop for
    /// 600k keys, past the interrupt).
    eph_waiting: std::collections::HashMap<(u8, u32), u32, EphHash>,
    eph_links: Vec<(Value, u32)>,
    eph_active: bool,
    /// Where a suspended atomic phase stands (see `atomic`).
    atomic_stage: Option<AtomicStage>,
    /// Work per object reached in this cycle (`obj_work`, set when it starts).
    obj_work: usize,
    /// Buffers a traversal collects into, kept so marking allocates nothing.
    trav_vals: Vec<Value>,
    trav_ups: Vec<u32>,
    /// The table traversed `TABLE_CHUNK` slots a step while marking is incremental
    /// (`NO_PART`: none; see `part_step`), its next slot, its weak mode as it was when its
    /// traversal started, and whether that traversal started over once already.
    pub(crate) part: u32,
    /// Big tables reached while another was traversed in chunks, waiting their turn.
    part_queue: Vec<u32>,
    part_at: usize,
    /// Slots of its snapshot (`Table::slots_at`) its chunks have paid for.
    part_paid: usize,
    part_mode: (bool, bool),
    /// The most slots of a table one traversal scanned in one go (a whole table, or one
    /// chunk of one) since the host last read it (`Lua::gc_most_slots_at_once`).
    pub(crate) most_at_once: usize,
    /// Slots the atomic phase's traversals have scanned (`traverse_table`): its looks at
    /// the interrupt and the deadline go by them as well as by its work.
    pub(crate) scanned: usize,
    /// The most slots it scanned between two looks since the host last read it
    /// (`Lua::gc_most_slots_between_looks`).
    pub(crate) most_between_looks: usize,
    /// The slots a host poll may scan before a marking step that may pause does
    /// (`pause_ok`, set by the machine where the call can be resumed as it stands), and
    /// `scanned` when the poll started. None: steps run to their end. A paused step's loop
    /// state, and that it paused (SEC7-G1).
    pub(crate) poll_allow: Option<usize>,
    pub(crate) poll_start: usize,
    /// Objects traversed (and the clearing's work) since the poll started: a poll is
    /// bounded by them with its slots (`poll_over`).
    pub(crate) poll_objs: usize,
    /// The table slots a marking step scans at most (`STEP_SLOTS` for the steps of an
    /// incremental cycle; none for a full collection): a step's work leaves out the slots a
    /// table grew by while marking (their marking was paid as it grew), so a step bounded by
    /// its work alone scanned a weak array's 2^21 slots in one go, 10 ms (SEC7-G1).
    pub(crate) step_slots: usize,
    /// The atomic step paused at the end of the host's poll: its counters (work, its next
    /// looks, where its scan count started, its last look), so it goes on as it stood.
    pub(crate) atomic_saved: Option<[usize; 5]>,
    pub(crate) pause_ok: bool,
    pub(crate) paused: bool,
    prop_saved: Option<(usize, Prop)>,
    /// What a marking step's prepaid slots (`part_step`) took past its budget, taken from
    /// the next step's: they pace the steps as work does, so a call scans no more of them
    /// than its steps' budgets cover (SEC7-G1, the plain call's pause).
    pace_carry: usize,
    /// Test probe (`Lua::gc_pace_probe`): the grown slots scanned, and the marking steps'
    /// budgets (work units), since the state was made.
    pub(crate) probe_prepaid: u64,
    pub(crate) probe_budget: u64,
}

/// Where a marking step's loop stands (`Heap::propagate_from`).
#[derive(Clone, Copy, Debug)]
struct Prop {
    done: usize,
    next: usize,
    redone: usize,
    slots: usize,
    /// The work units the slots it scanned past what it pays for stand for (`pre_paid`).
    pre: usize,
    /// The step's budget as given, and the pacing carried into it (`pace_carry`).
    given: usize,
    carry: usize,
}

/// The work units a slot grown in the cycle spends of a marking step's budget when a
/// chunked traversal scans it (uncharged: its marking was paid as the table grew). What
/// scanning one costs, against what a unit of fuel costs, measured (SEC7-G1, the plain
/// call's pause; release build, 5dc4ba3, `s6gc_2e_a_big_table_is_marked_a_chunk_a_step`'s
/// workload, thread CPU, 3 runs a loop): 14.2 to 16.3 ns a grown slot (the 16 steps over the
/// 1,048,766 slots T grew by) against 27.4 to 35.8 ns a fuel unit (the median call's CPU
/// over its 200,000 units), so 1.82 to 2.30 units a slot (`WORK_PER_FUEL` 4). Paced at a
/// charged slot's rate instead (6 units there), the cycle took longer, more garbage floated
/// and the tables slab doubled once more (302 to 604 MB) within the growth report.
pub(crate) const GROWN_SLOT_WORK: usize = 2;

/// `Heap::part` when no table is traversed in chunks.
pub(crate) const NO_PART: u32 = u32::MAX;

/// Whether marking an entry (key `k`, value `v`) of the table traversed in chunks, of weak
/// mode (`wk`, `wv`), marks its key and its value. Decided by the entry alone, never by the
/// part it sits in, so the same wherever a write or a rebuild put it: a weak part is not
/// marked (strings always are), and in an ephemeron table (weak keys only) a value is
/// marked when its key is a string or no object, as the atomic step would mark it (whose
/// keys are other objects is left to that step: their marks then follow the order).
fn entry_marks(k: Value, v: Value, (wk, wv): (bool, bool)) -> (bool, bool) {
    let str_k = matches!(k, Value::Str(_));
    let mk = !wk || str_k;
    let mv = matches!(v, Value::Str(_)) || (!wv && (!wk || str_k || !k.is_collectable()));
    (mk, mv)
}

/// Collector work units per byte allocated.
/// Bytes allocated between steps.
pub(crate) const STEP_SIZE: isize = 8 * 1024;

/// The 64-bit halves of `a * b`, folded (xor): a multiply whose every output bit depends on
/// every input bit.
#[inline]
fn fold_mul(a: u64, b: u64) -> u64 {
    let r = u128::from(a) * u128::from(b);
    (r as u64) ^ ((r >> 64) as u64)
}

/// A string's hash for the interner, keyed by the heap's seed: each 8 bytes go through a
/// folded multiply by a seed-derived factor. (The FxHash-style `(rotl(h, 5) ^ w) * K` it
/// replaces had collisions that hold for every seed: flipping the top bit of one word and
/// bit 4 of the next leaves the hash unchanged, so a script could make any number of
/// colliding strings without knowing the seed.)
#[inline]
fn hash_bytes(seed: u64, b: &[u8]) -> u64 {
    let whole = b.len() & !7;
    let h = hash_words(seed, hash_start(seed, b.len()), b.get(..whole).unwrap_or_default());
    hash_finish(seed, h, b.get(whole..).unwrap_or_default())
}

/// `hash_bytes` in parts (for a long string hashed a piece at a time, `Heap::hash_piece`):
/// the state before any bytes of a string of `len`,
#[inline]
fn hash_start(seed: u64, len: usize) -> u64 {
    seed ^ (len as u64).wrapping_mul(0x9E37_79B9_7F4A_7C15)
}

/// the state after `words` (whole 8-byte words: the bytes before them so too),
#[inline]
fn hash_words(seed: u64, mut h: u64, words: &[u8]) -> u64 {
    let (k1, k2) = (seed ^ 0xa076_1d64_78bd_642f, seed.rotate_left(32) ^ 0xe703_7ed1_a0b4_28db);
    for c in words.chunks_exact(8) {
        let mut w = [0u8; 8];
        w.copy_from_slice(c);
        h = fold_mul(h ^ u64::from_le_bytes(w) ^ k1, k2);
    }
    h
}

/// Up to 8 bytes as a little-endian word (zero above them). (Shifted in a byte at a time
/// in a loop, the tail of a short string's hash cost more than its words; copied into a
/// word, it became a `memcpy` call.)
#[inline]
fn tail_word(t: &[u8]) -> u64 {
    let w = |b: &[u8]| b.iter().rev().fold(0u64, |w, &x| w << 8 | u64::from(x));
    match *t {
        [] => 0,
        [a] => u64::from(a),
        [a, b] => w(&[a, b]),
        [a, b, c] => w(&[a, b, c]),
        [a, b, c, d] => w(&[a, b, c, d]),
        [a, b, c, d, e] => w(&[a, b, c, d, e]),
        [a, b, c, d, e, f] => w(&[a, b, c, d, e, f]),
        [a, b, c, d, e, f, g] => w(&[a, b, c, d, e, f, g]),
        [a, b, c, d, e, f, g, h, ..] => u64::from_le_bytes([a, b, c, d, e, f, g, h]),
    }
}

/// and the hash, from the state and the last (under 8) bytes.
#[inline]
fn hash_finish(seed: u64, h: u64, tail: &[u8]) -> u64 {
    let (k1, k2) = (seed ^ 0xa076_1d64_78bd_642f, seed.rotate_left(32) ^ 0xe703_7ed1_a0b4_28db);
    let h = fold_mul(h ^ tail_word(tail) ^ k1, k2);
    // Never 0, which a string's `hash` keeps for "not computed yet": a script that knows
    // the seed can make a string hash to 0, which was then hashed again at every use (and
    // forever by `settle_long_key`, S6R-7).
    fold_mul(h ^ k2, k1 | 1).max(1)
}


impl Heap {
    pub fn new(cap: usize, seed: u64) -> Heap {
        // Each state's generations start elsewhere, so a handle from another state almost
        // never passes as one of this state's objects (`is_live`). Never script-visible.
        static STATES: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(1);
        let g = STATES.fetch_add(1, std::sync::atomic::Ordering::Relaxed).wrapping_mul(0x9E37_79B9);
        Heap {
            strings: Slab::new(g),
            tables: Slab::new(g),
            funcs: Slab::new(g),
            upvals: Slab::new(g),
            uds: Slab::new(g),
            threads: Slab::with_slot_extra(g, THREAD_BASE),
            protos: Slab::new(g),
            interner: Interner::new(),
            mem: Mem::new(cap, -STEP_SIZE),
            seed,
            table_seed: seed.rotate_left(29) ^ 0x9E37_79B9_7F4A_7C15,
            collected: Vec::new(),
            phase: Phase::Pause,
            gray: Vec::new(),
            born: Born::default(),
            grayagain: Vec::new(),
            weak: Vec::new(),
            sweep: (0, 0),
            cycle: 1,
            estimate: 0,
            blocked: 0,
            mode_key: None,
            eph_waiting: std::collections::HashMap::with_hasher(EphHash(mix_seed(seed, 0xe9))),
            eph_links: Vec::new(),
            eph_active: false,
            atomic_stage: None,
            obj_work: 2,
            trav_vals: Vec::new(),
            trav_ups: Vec::new(),
            part: NO_PART,
            part_queue: Vec::new(),
            part_at: 0,
            part_paid: 0,
            part_mode: (false, false),
            most_at_once: 0,
            scanned: 0,
            most_between_looks: 0,
            poll_allow: None,
            poll_start: 0,
            poll_objs: 0,
            step_slots: usize::MAX,
            atomic_saved: None,
            pause_ok: false,
            paused: false,
            prop_saved: None,
            pace_carry: 0,
            probe_prepaid: 0,
            probe_budget: 0,
            stopped: false,
            pause: 200,
            stepmul: 100,
        }
    }

    /// The color a new object in slab `slab` at `idx` starts with: white between cycles,
    /// gray while marking (it is traversed before marking ends), and while sweeping black
    /// where the sweep has not reached yet (so it survives) or white where it has.
    fn new_color(phase: Phase, sweep: (u8, usize), slab: u8, idx: usize) -> u8 {
        match phase {
            Phase::Pause => WHITE,
            Phase::Propagate => GRAY,
            Phase::Sweep => {
                if slab < sweep.0 || (slab == sweep.0 && idx < sweep.1) {
                    WHITE
                } else {
                    BLACK
                }
            }
        }
    }

    // ---- strings ----

    /// The interned string with these bytes, made if new (of any length). Charges memory.
    pub fn intern(&mut self, bytes: &[u8]) -> Result<StrRef, ()> {
        self.intern_hashed(bytes, None)
    }

    /// `intern` of bytes whose hash may be known already (`hash_piece`).
    #[inline]
    pub fn intern_hashed(&mut self, bytes: &[u8], hash: Option<u64>) -> Result<StrRef, ()> {
        let (seed, phase, sweep) = (self.seed, self.phase, self.sweep);
        Self::intern_in(&mut self.strings, &mut self.interner, &mut self.mem, (seed, phase, sweep), bytes, hash)
    }

    /// `intern` on the parts of the heap it uses (the fast loop holds the others borrowed):
    /// the string slab, the interner, the memory account and (seed, phase, sweep).
    pub(crate) fn intern_in(
        strings: &mut Slab<LStr>,
        interner: &mut Interner,
        mem: &mut Mem,
        (seed, phase, sweep): (u64, Phase, (u8, usize)),
        bytes: &[u8],
        hash: Option<u64>,
    ) -> Result<StrRef, ()> {
        let hash = hash.unwrap_or_else(|| hash_bytes(seed, bytes));
        let slot = match interner.find(strings, hash, bytes) {
            Ok(s) => return Ok(Self::keep_in(strings, phase, sweep, s)),
            Err(slot) => slot,
        };
        mem.charge(str_mem(bytes.len()))?;
        let lstr = LStr { bytes: bytes.into(), hash: std::cell::Cell::new(hash), interned: true, twin: std::cell::Cell::new(None) };
        let r = Self::alloc_str_in(strings, mem, phase, sweep, lstr);
        mem.used = mem.used.saturating_add(interner.insert(strings, slot, r.idx));
        Ok(r)
    }

    /// A string: interned when short, else a new object (not hashed). Charges memory.
    #[inline]
    pub fn new_str(&mut self, bytes: &[u8]) -> Result<StrRef, ()> {
        if bytes.len() <= SHORT_LEN {
            return self.intern(bytes);
        }
        self.mem.charge(str_mem(bytes.len()))?;
        Ok(self.new_long_charged(bytes.into()))
    }

    /// A long string whose memory (`str_mem(len)`) is charged already: its bytes are
    /// moved in, not copied.
    pub fn new_long_charged(&mut self, bytes: Box<[u8]>) -> StrRef {
        self.alloc_str(LStr { bytes: bytes.into(), hash: std::cell::Cell::new(0), interned: false, twin: std::cell::Cell::new(None) })
    }

    fn alloc_str(&mut self, s: LStr) -> StrRef {
        Self::alloc_str_in(&mut self.strings, &mut self.mem, self.phase, self.sweep, s)
    }

    fn alloc_str_in(strings: &mut Slab<LStr>, mem: &mut Mem, phase: Phase, sweep: (u8, usize), s: LStr) -> StrRef {
        let (idx, generation) = strings.alloc(s, mem, |i| Self::new_color(phase, sweep, 0, i));
        // Strings have nothing to traverse: gray is black for them.
        if let Some(c) = strings.color.get_mut(idx as usize)
            && *c == GRAY
        {
            *c = BLACK;
        }
        StrRef { idx, generation }
    }

    /// The interned string of `bytes` (short), kept as `intern` keeps a string it finds,
    /// or None when there is none (nothing made; the walk's steps are counted as
    /// `intern`'s are).
    pub(crate) fn find_short(&mut self, bytes: &[u8]) -> Option<StrRef> {
        let s = self.interner.find(&self.strings, hash_bytes(self.seed, bytes), bytes).ok()?;
        Some(Self::keep_in(&mut self.strings, self.phase, self.sweep, s))
    }

    /// The interned string of `bytes` (any length), found without making it, keeping it
    /// or leaving anything a later interner call reads (`walked`, `last_diff`): for a host
    /// read that does not enter the state (SEC8-F3). Table keys are interned, so a key
    /// with these bytes is this string or there is none.
    pub(crate) fn find_interned_quiet(&self, bytes: &[u8]) -> Option<StrRef> {
        let (walked, diff) = (self.interner.walked.get(), self.interner.last_diff.get());
        let r = self.interner.find(&self.strings, hash_bytes(self.seed, bytes), bytes).ok();
        self.interner.walked.set(walked);
        self.interner.last_diff.set(diff);
        r.map(|s| StrRef { idx: s, generation: self.strings.gens.get(s as usize).copied().unwrap_or(0) })
    }

    fn find_interned(&self, hash: u64, bytes: &[u8]) -> Result<u32, usize> {
        self.interner.find(&self.strings, hash, bytes)
    }

    /// A reference to interned string `s`, found again: while the collector runs, it must
    /// not be swept now.
    pub(crate) fn keep(&mut self, s: u32) -> StrRef {
        Self::keep_in(&mut self.strings, self.phase, self.sweep, s)
    }

    /// `keep` on the string slab alone (for the fast loop, which holds other parts of the
    /// heap borrowed).
    pub(crate) fn keep_in(strings: &mut Slab<LStr>, phase: Phase, sweep: (u8, usize), s: u32) -> StrRef {
        let generation = strings.gens.get(s as usize).copied().unwrap_or(0);
        if phase == Phase::Sweep
            && let Some(c) = strings.color.get_mut(s as usize)
            && *c == WHITE
            && (sweep.0 == 0 && (s as usize) >= sweep.1)
        {
            *c = BLACK;
        }
        if phase == Phase::Propagate
            && let Some(c) = strings.color.get_mut(s as usize)
            && *c == WHITE
        {
            *c = BLACK;
        }
        StrRef { idx: s, generation }
    }

    fn insert_interned(&mut self, slot: usize, idx: u32) {
        let held = self.interner.insert(&self.strings, slot, idx);
        self.mem.used = self.mem.used.saturating_add(held);
    }

    /// A string's hash, computed on first use (at most once per string, so its cost is
    /// covered by what making the string cost).
    fn str_hash(&self, s: &LStr) -> u64 {
        let h = s.hash.get();
        if h != 0 {
            return h;
        }
        let h = hash_bytes(self.seed, &s.bytes);
        s.hash.set(h);
        h
    }

    /// Whether looking `r` up as a key could hash or compare a long run of bytes in one go: a
    /// long string, not interned, whose interned copy is not known (`Lua::settle_key`).
    #[inline]
    pub fn key_needs_settling(&self, r: StrRef) -> bool {
        self.strings.get(r.idx).is_some_and(|s| !s.interned && s.bytes.len() > crate::stdlib::COPY_PIECE) && self.known_canon(r).is_none()
    }

    /// Hashes the next `COPY_PIECE` bytes of `b` from `run` (the state and the bytes done,
    /// None at the start): the hash (as `hash_bytes` makes it) once all are done, for a
    /// long string hashed between looks at the host's interrupt and deadline.
    pub fn hash_piece(&self, b: &[u8], run: &mut Option<(u64, usize)>) -> Option<u64> {
        let (h, at) = run.unwrap_or((hash_start(self.seed, b.len()), 0));
        let whole = b.len() & !7;
        let end = at.saturating_add(crate::stdlib::COPY_PIECE).min(whole);
        let h = hash_words(self.seed, h, b.get(at..end).unwrap_or_default());
        if end < whole {
            *run = Some((h, end));
            return None;
        }
        Some(hash_finish(self.seed, h, b.get(whole..).unwrap_or_default()))
    }

    /// The one interned string that string `r` (its hash known: `hash_piece`) can equal:
    /// the member of its group (`Crit`) its bytes lead to.
    pub fn key_best(&self, r: StrRef) -> Option<u32> {
        let s = self.strings.get(r.idx)?;
        self.interner.group_best(&self.strings, s.hash.get(), &s.bytes)
    }

    /// The first byte in `from..to` where strings `a` and `b` differ (None: none there).
    pub fn diff_piece(&self, a: u32, b: u32, from: usize, to: usize) -> Option<usize> {
        let (Some(x), Some(y)) = (self.strings.get(a), self.strings.get(b)) else { return Some(from) };
        first_diff(&x.bytes, &y.bytes, from, to)
    }

    /// Interns string `r` (`GROUP_FROM` bytes or more, its hash known) that was compared with
    /// the one interned string it could equal and is not it (`best`: that string and the
    /// first byte where they differ; None: there was none), comparing nothing again.
    pub fn canon_absent(&mut self, r: StrRef, best: Option<(u32, usize)>) -> StrRef {
        let Some(s) = self.strings.get(r.idx) else { return r };
        let (hash, len) = (s.hash.get(), s.bytes.len());
        let best = best.map(|(m, at)| (m, at, self.strings.get(m).and_then(|o| o.bytes.get(at).copied()).unwrap_or(0)));
        let slot = self.interner.slot_for_absent(hash, len, best);
        if let Some(s) = self.strings.get_mut(r.idx) {
            s.interned = true;
        }
        self.insert_interned(slot, r.idx);
        r
    }

    /// Notes interned string `idx` as the copy of `r` (its bytes compared equal).
    pub fn set_twin(&self, r: StrRef, idx: u32) {
        if let Some(s) = self.strings.get(r.idx) {
            s.twin.set(Some(StrRef { idx, generation: self.strings.gens.get(idx as usize).copied().unwrap_or(0) }));
        }
    }

    /// The interned string equal to `r`, if any (`r` itself once interned). For reads
    /// only: a table holds interned keys, so a long string that has no interned copy is in
    /// no table.
    pub fn find_canon(&self, r: StrRef) -> Option<StrRef> {
        let s = self.strings.get(r.idx)?;
        if s.interned {
            return Some(r);
        }
        if let Some(t) = s.twin.get()
            && self.strings.live(t.idx, t.generation)
        {
            return Some(t);
        }
        let idx = self.find_interned(self.str_hash(s), &s.bytes).ok()?;
        let t = StrRef { idx, generation: self.strings.gens.get(idx as usize).copied().unwrap_or(0) };
        s.twin.set(Some(t));
        Some(t)
    }

    /// `find_canon` when it is known without walking the interner: `r` itself once
    /// interned, or the copy found before (for the fast loop, S5GC-3). None: not known.
    #[inline]
    pub fn known_canon(&self, r: StrRef) -> Option<StrRef> {
        let s = self.strings.get(r.idx)?;
        if s.interned {
            return Some(r);
        }
        s.twin.get().filter(|t| self.strings.live(t.idx, t.generation))
    }

    /// The interned string equal to `r`, interning `r` if there is none (for a table key).
    pub fn canon(&mut self, r: StrRef) -> StrRef {
        let Some(s) = self.strings.get(r.idx) else { return r };
        if s.interned {
            return r;
        }
        if let Some(t) = s.twin.get()
            && self.strings.live(t.idx, t.generation)
        {
            return self.keep(t.idx);
        }
        // (One lookup: a second compared a long string's bytes again, S6T-EQ.)
        match self.find_interned(self.str_hash(s), &s.bytes) {
            Ok(i) => {
                let t = StrRef { idx: i, generation: self.strings.gens.get(i as usize).copied().unwrap_or(0) };
                s.twin.set(Some(t));
                self.keep(i)
            }
            Err(slot) => {
                if let Some(s) = self.strings.get_mut(r.idx) {
                    s.interned = true;
                }
                self.insert_interned(slot, r.idx);
                r
            }
        }
    }

    /// The key to look up again after `key` missed in a table: the interned copy of a long
    /// string that is not interned itself.
    #[inline]
    pub fn retry_key(&self, key: Value) -> Option<Value> {
        let Value::Str(r) = key else { return None };
        let c = self.find_canon(r)?;
        (c != r).then_some(Value::Str(c))
    }

    /// `tab[key]`, raw, for a key that may be a long string not interned yet.
    #[inline]
    pub fn get_in(&self, tab: &Table, key: Value) -> Value {
        let v = tab.get(key);
        match v {
            Value::Nil => self.retry_key(key).map_or(v, |k| tab.get(k)),
            _ => v,
        }
    }

    /// Whether two different string objects are equal, when that is known without comparing
    /// their bytes (None: compare them). Interned strings equal only themselves.
    #[inline]
    pub fn str_eq_quick(&self, a: StrRef, b: StrRef) -> Option<bool> {
        if a == b {
            return Some(true);
        }
        let (Some(x), Some(y)) = (self.strings.get(a.idx), self.strings.get(b.idx)) else { return Some(false) };
        let (hx, hy) = (x.hash.get(), y.hash.get());
        if x.bytes.len() != y.bytes.len() || (x.interned && y.interned) || (hx != 0 && hy != 0 && hx != hy) {
            return Some(false);
        }
        None
    }

    /// Takes string `idx` out of the interner: the collector work its walk costs past a
    /// plain one (S5GC-3).
    fn unintern(&mut self, idx: u32) -> usize {
        let h = self.strings.get(idx).map(|x| x.hash.get()).unwrap_or(0);
        let (cost, freed) = self.interner.remove(&self.strings, h, idx);
        self.mem.release(freed);
        cost
    }

    #[inline]
    pub fn str_bytes(&self, r: StrRef) -> &[u8] {
        self.strings.get(r.idx).map(|s| &*s.bytes).unwrap_or_default()
    }

    // ---- objects ----

    /// A collector step is due, or the work of the objects made since the last.
    #[inline]
    pub fn gc_due(&self) -> bool {
        self.mem.debt > 0 || self.born.pending() >= BORN_DUE
    }

    pub fn new_table(&mut self, arr: usize, hash: usize) -> Result<TableRef, ()> {
        Self::new_table_in(&mut self.mem, &mut self.tables, &mut self.born, (self.phase, self.sweep, self.table_seed), arr, hash)
    }

    /// [`Heap::new_table`] on the fields it touches (for the fast loop, which holds the
    /// prototypes borrowed).
    pub(crate) fn new_table_in(
        mem: &mut Mem,
        tables: &mut Slab<Table>,
        born: &mut Born,
        (phase, sweep, seed): (Phase, (u8, usize), u64),
        arr: usize,
        hash: usize,
    ) -> Result<TableRef, ()> {
        mem.charge(crate::table::new_cost(arr, hash))?;
        let slots = crate::table::hash_slots(hash);
        let walked = arr + slots;
        let (arr, nodes, live) = (mem.pool_arr(arr), mem.pool_nodes(slots), crate::table::LiveBits::for_len(slots));
        let color = |i| Self::new_color(phase, sweep, 1, i);
        let (idx, generation) = match tables.take_free(mem, color) {
            Some((i, generation, t)) => {
                (t.seed, t.arr, t.nodes, t.live) = (seed, arr.into(), nodes, live);
                (i, generation)
            }
            None => tables.alloc(Table { seed, arr: arr.into(), nodes, live, ..Table::default() }, mem, color),
        };
        born.add(phase, Gray::Table(idx), walked);
        Ok(TableRef { idx, generation })
    }

    /// A function whose memory the caller charged already.
    pub fn new_func_charged(&mut self, f: Func) -> FuncRef {
        let (phase, sweep) = (self.phase, self.sweep);
        let n = match &f {
            Func::Lua { upvals, .. } => upvals.len(),
            Func::Native { upvals, .. } => upvals.len(),
            Func::Host { .. } => 0,
        };
        let (idx, generation) = self.funcs.alloc(f, &mut self.mem, |i| Self::new_color(phase, sweep, 2, i));
        self.born.add(phase, Gray::Func(idx), n);
        FuncRef { idx, generation }
    }

    /// A prototype whose memory the caller charged already.
    pub fn new_proto_charged(&mut self, p: Proto) -> u32 {
        let (phase, sweep) = (self.phase, self.sweep);
        let n = p.consts.len() + p.protos.len();
        let (idx, _) = self.protos.alloc(p, &mut self.mem, |i| Self::new_color(phase, sweep, 6, i));
        self.born.add(phase, Gray::Proto(idx), n);
        idx
    }

    /// An upvalue whose memory the caller charged already. Upvalue references reuse
    /// [`FuncRef`]'s shape (index and generation).
    pub fn new_upval_charged(&mut self, u: Upval) -> FuncRef {
        let (phase, sweep) = (self.phase, self.sweep);
        let (idx, generation) = self.upvals.alloc(u, &mut self.mem, |i| Self::new_color(phase, sweep, 3, i));
        self.born.add(phase, Gray::Upval(idx), 0);
        FuncRef { idx, generation }
    }

    pub fn new_userdata(&mut self, data: Box<dyn Any + Send>, size: usize, meta: Option<TableRef>) -> Result<UdRef, ()> {
        self.mem.charge(UD_BASE + size)?;
        let (phase, sweep) = (self.phase, self.sweep);
        let (idx, generation) =
            self.uds.alloc(UserData { data: Some(data), meta, size, notify: None }, &mut self.mem, |i| Self::new_color(phase, sweep, 4, i));
        self.born.add(phase, Gray::Ud(idx), 0);
        Ok(UdRef { idx, generation })
    }

    pub fn new_thread(&mut self, state: ThreadState) -> Result<ThreadRef, ()> {
        self.mem.charge(state.mem_size())?;
        Ok(self.place_thread(state))
    }

    /// A thread whose memory (`ThreadState::mem_size`) was charged already.
    pub fn place_thread(&mut self, state: ThreadState) -> ThreadRef {
        let (phase, sweep) = (self.phase, self.sweep);
        let n = state.stack.len() + state.frames.len() + state.open_upvals.len();
        let (idx, generation) = self.threads.alloc(Box::new(state), &mut self.mem, |i| Self::new_color(phase, sweep, 5, i));
        self.born.add(phase, Gray::Thread(idx), n);
        ThreadRef { idx, generation }
    }

    /// Whether `v` names an object that is still alive (for values the host passes in).
    pub fn is_live(&self, v: Value) -> bool {
        let (slab, idx, live, color) = match v {
            Value::Str(r) => (0, r.idx, self.strings.live(r.idx, r.generation), &self.strings.color),
            Value::Table(r) => (1, r.idx, self.tables.live(r.idx, r.generation), &self.tables.color),
            Value::Func(r) => (2, r.idx, self.funcs.live(r.idx, r.generation), &self.funcs.color),
            Value::UserData(r) => (4, r.idx, self.uds.live(r.idx, r.generation), &self.uds.color),
            Value::Thread(r) => (5, r.idx, self.threads.live(r.idx, r.generation), &self.threads.color),
            _ => return true,
        };
        // Found dead by the last marking and not swept yet: still in its slot, but the sweep
        // will free it, so a handle to it must not be stored anywhere (sweep slab order).
        let doomed = self.phase == Phase::Sweep
            && (slab, idx as usize) >= self.sweep
            && color.get(idx as usize) == Some(&WHITE);
        live && !doomed
    }

    // ---- barriers ----

    /// A table was written: if marking already passed it, it is marked again later.
    #[inline]
    pub fn barrier_table(&mut self, t: TableRef) {
        if self.phase == Phase::Propagate
            && let Some(c) = self.tables.color.get_mut(t.idx as usize)
            && *c == BLACK
        {
            if t.idx == self.part {
                // Traversed in chunks: its entries were marked as they were written
                // (`part_written`); a new metatable is marked here.
                self.part_meta();
                return;
            }
            *c = GRAY;
            self.grayagain.push(Gray::Table(t.idx));
        }
    }

    /// The metatable of the table traversed in chunks, which may have changed.
    #[cold]
    fn part_meta(&mut self) {
        if let Some(m) = self.tables.get(self.part).and_then(|t| t.meta) {
            self.mark(Value::Table(m));
        }
    }

    // ---- the table traversed in chunks (S6GC-2e) ----
    //
    // While marking is incremental, a table of more than `TABLE_CHUNK` slots is traversed
    // that many slots a step (`part_step`), so no step takes a big table's traversal whole
    // (42 to 45 ms for 800,000 entries). The script runs between its chunks, so each write
    // into it then goes through `part_writing` and `part_written`, which make what marking
    // keeps of it exactly what it held when its traversal started plus everything written
    // into it since (a snapshot-at-the-beginning barrier, as Yuasa's):
    // - the entry a write overwrites or removes is marked before it goes (`part_writing`):
    //   so every entry the table held at the start is marked, by the traversal or then;
    // - the entry written is marked as it comes (`part_written`), and so is any entry the
    //   write moved to another node (`Watch::moved`), wherever it lands;
    // - when the table's parts are rebuilt or resized (`Watch::restructured`) after the
    //   traversal passed the array entries no rebuild moves (`Watch::alen_floor`), it
    //   starts over: every entry the table holds then is in that set already, and every
    //   entry of the start no longer there was marked as it went, at every rebuild. (A
    //   script cannot keep it from ending: a rebuild allocates the table's new parts, and
    //   the steps that allocation owes traverse them again.)
    // Its chunks go one after another, before any other gray object, so when its last one
    // comes follows the work alone; writes after it go to `grayagain`, as for any table.
    // None of this depends on which slot an entry sits in, so what is marked, and the work
    // (charged by the slots each chunk spans, never by what they hold), follow neither the
    // layout nor the hash seed (as traversing it in chunks alone did: entries overwritten
    // between chunks were marked or not by which chunk their node fell in). No log is kept:
    // an object turns gray once a cycle at most, so the gray list stays bounded by the live
    // heap whatever the script writes (a log of overwritten values grew without bound,
    // uncharged, under `t[i] = t[j]`, which allocates nothing and so runs no step).

    /// A big table reached while marking is incremental: traversed in chunks, or waiting
    /// its turn while another is (gray, so writes into it need no barrier yet).
    #[cold]
    #[inline(never)]
    fn big_table(&mut self, i: u32) -> usize {
        if self.part == NO_PART {
            return self.begin_part(i);
        }
        self.part_queue.push(i);
        1
    }

    /// Objects that can turn gray (tables, functions, userdata, threads) the slabs hold.
    fn live_objects(&self) -> usize {
        let live = |items: usize, free: usize| items.saturating_sub(free);
        live(self.tables.slots(), self.tables.free.len())
            + live(self.funcs.slots(), self.funcs.free.len())
            + live(self.uds.slots(), self.uds.free.len())
            + live(self.threads.slots(), self.threads.free.len())
    }

    /// Starts traversing table `i` in chunks: the work.
    fn begin_part(&mut self, i: u32) -> usize {
        // Room in the gray list for what its chunks can mark (two objects a slot, at most
        // the objects there are), taken now: grown by doubling as they came, it copied 16
        // MiB in one step (5 ms). Pages not written cost nothing; trimmed when the cycle
        // ends, as the rest of the collector's scratch.
        let objects = self.live_objects();
        let slots = self.tables.at(i as usize).map_or(0, Table::slots);
        self.gray.reserve(slots.saturating_mul(2).min(objects));
        if let Some(c) = self.tables.color.get_mut(i as usize) {
            *c = BLACK;
        }
        let meta = self.tables.get(i).and_then(|t| t.meta);
        if let Some(m) = meta {
            self.mark(Value::Table(m));
        }
        // Its weak mode as it is now holds for the whole traversal: a mode changed between
        // chunks would mark the chunks before and after differently, by the layout.
        self.part_mode = self.weak_mode(meta);
        self.part = i;
        self.part_at = 0;
        self.part_paid = 0;
        self.mem.watch.moved.clear();
        self.mem.watch.restructured = false;
        self.mem.watch.alen_floor = usize::MAX;
        if let Some(t) = self.tables.at_mut(i as usize) {
            t.gc_watch = true;
        }
        1
    }

    /// No table is traversed in chunks any more.
    fn end_part(&mut self) {
        if let Some(t) = self.tables.at_mut(self.part as usize) {
            t.gc_watch = false;
        }
        self.part = NO_PART;
        self.mem.watch.moved.clear();
        self.mem.watch.restructured = false;
        self.mem.watch.alen_floor = usize::MAX;
    }

    /// The next chunk of the table traversed in chunks: the work, by the slots it spans.
    /// The next chunk of the table traversed in chunks: (the work it charged, the slots it
    /// scanned, the slots of those grown in the cycle). Past the slots paid for (a table
    /// grown in the cycle, or a traversal a rebuild started over) a chunk charges nothing
    /// and still scans its slots, so they are counted apart (SEC7-G1): the charge and the
    /// grown slots drive the step's end, the slots what a poll costs. (The grown ones spent
    /// no step's budget at first: a call took a step at 64 Ki slots, the cap, one after
    /// another, 16 ms in one call for a hash part grown from 2^20 to 2^21 nodes.)
    fn part_step(&mut self) -> (usize, usize, usize) {
        let i = self.part;
        let (wk, wv) = self.part_mode;
        let floor = std::mem::replace(&mut self.mem.watch.alen_floor, usize::MAX);
        let alen_now = self.tables.at(i as usize).map_or(0, |t| t.arr.live_len());
        // A rebuild moved entries behind the traversal: it starts again, in chunks as
        // before (S6R-2: after a second rebuild the rest was marked whole, 37 ms at 1.5
        // million keys). (Traversed no further than the array part's entries no rebuild
        // moved: nothing moved behind it, the common case of an array growing while it is
        // traversed.)
        if std::mem::take(&mut self.mem.watch.restructured) && self.part_at > floor.min(alen_now) {
            self.part_at = 0;
        }
        let total = self.tables.at(i as usize).map_or(0, Table::slots);
        let from = self.part_at.min(total);
        let to = from.saturating_add(TABLE_CHUNK).min(total);
        self.mark_chunk(i, (from, to), total, (wk, wv));
        self.most_at_once = self.most_at_once.max(to - from);
        self.scanned = self.scanned.wrapping_add(to - from);
        // Writes after its last chunk go to `grayagain`, as for any table traversed.
        self.part_at = to;
        // By its slots when the cycle started (`Table::slots_at`), each paid once: its size
        // when reached, its chunks and how often a rebuild started them over follow when
        // it was reached, so the hash seed (COV-5, as `traverse_table`). A traversal
        // started over is paid by the rebuild that did it.
        let snap = self.tables.get(i).map_or(0, |t| t.slots_at(self.cycle));
        let upto = if to >= total { snap } else { to.min(snap) };
        let paid = upto.saturating_sub(self.part_paid);
        let work = paid * (1 + self.obj_work / 4);
        self.part_paid = self.part_paid.max(upto);
        if to >= total {
            self.finish_part();
        }
        // (The slots it scanned past the table's size when the cycle started: grown in the
        // cycle, paid as they grew, `propagate_from`. Where those are follows the table's
        // sizes alone; a traversal a rebuild started over is not counted, since where it
        // had got to followed when it was reached, so the seed.)
        (work, to - from, to.saturating_sub(from.max(snap)))
    }

    /// A chunk of the table traversed in chunks marked (`mark_slots`), compiled on its own:
    /// inlined into `propagate`, its loop over the array part took twice the instructions.
    #[inline(never)]
    fn mark_chunk(&mut self, i: u32, range: (usize, usize), total: usize, mode: (bool, bool)) {
        let alen = self.tables.at(i as usize).map_or(0, |t| t.arr.live_len());
        self.mark_slots::<true>(i, range, (alen, total), mode);
    }

    /// Ends the traversal in chunks (its last chunk done).
    fn finish_part(&mut self) {
        let i = self.part;
        let (wk, wv) = self.part_mode;
        self.end_part();
        if wk || wv {
            // Weak tables are revisited in the atomic step (their ephemerons too).
            self.weak.push((i, wk, wv));
            if let Some(c) = self.tables.color.get_mut(i as usize) {
                *c = GRAY;
            }
        }
    }

    /// An entry (key, value) of the table traversed in chunks, marked as its traversal
    /// would mark it (weak parts not; strings always).
    fn part_mark(&mut self, k: Value, v: Value) {
        let (mk, mv) = entry_marks(k, v, self.part_mode);
        if mk {
            self.mark(k);
        }
        if mv {
            self.mark(v);
        }
    }

    /// Before any write of `key` into table `t` while it is traversed in chunks (a value,
    /// nil or a new key): the entry it overwrites or removes is marked. The key as the
    /// table holds it, or `key` when it holds none.
    #[cold]
    pub fn part_writing(&mut self, t: u32, key: Value) -> Value {
        // (Its lookup walks what the write's own does, and a lookup's probe steps count
        // the longest walk, not the sum: it costs the script nothing more.)
        match self.tables.get(t).and_then(|tab| tab.entry(key)) {
            Some((k, old)) => {
                if !old.is_nil() {
                    self.part_mark(k, old);
                }
                k
            }
            None => key,
        }
    }

    /// After that write of `val` at `key` (as `part_writing` gave it): the entry written is
    /// marked, and any entry the write moved to another node.
    #[cold]
    pub fn part_written(&mut self, t: u32, key: Value, val: Value) {
        if self.part != t {
            return;
        }
        if !val.is_nil() {
            self.part_mark(key, val);
        }
        let mut moved = std::mem::take(&mut self.mem.watch.moved);
        let mut it = moved.iter().copied();
        while let (Some(k), Some(v)) = (it.next(), it.next()) {
            self.part_mark(k, v);
        }
        moved.clear();
        self.mem.watch.moved = moved;
    }

    /// An upvalue was written.
    #[inline]
    pub fn barrier_upval(&mut self, u: u32) {
        if self.phase == Phase::Propagate
            && let Some(c) = self.upvals.color.get_mut(u as usize)
            && *c == BLACK
        {
            *c = GRAY;
            self.grayagain.push(Gray::Upval(u));
        }
    }

    // ---- marking ----

    /// Marks a value reachable.
    #[inline]
    pub fn mark(&mut self, v: Value) {
        match v {
            Value::Str(r) => {
                if let Some(c) = self.strings.color.get_mut(r.idx as usize)
                    && *c == WHITE
                {
                    *c = BLACK;
                }
            }
            Value::Table(r) => self.mark_gray(Gray::Table(r.idx)),
            Value::Func(r) => self.mark_gray(Gray::Func(r.idx)),
            Value::UserData(r) => self.mark_gray(Gray::Ud(r.idx)),
            Value::Thread(r) => self.mark_gray(Gray::Thread(r.idx)),
            _ => {}
        }
    }

    /// Marks the ephemeron values that waited for `g` as their key. Called when `g` is
    /// traversed, not when it is marked: marking them at once recursed through
    /// `mark`, one native frame per link, so a long ephemeron chain (key -> value that is
    /// the next key) overflowed the native stack and killed the process. Every marked key
    /// is traversed (it goes on the gray list), so none is missed.
    fn release_waiting(&mut self, g: Gray) {
        if !self.eph_active || self.eph_waiting.is_empty() {
            return;
        }
        let key = match g {
            Gray::Table(i) => (1, i),
            Gray::Func(i) => (2, i),
            Gray::Ud(i) => (4, i),
            Gray::Thread(i) => (5, i),
            Gray::Upval(_) | Gray::Proto(_) | Gray::TablePart(..) => return,
        };
        let mut i = self.eph_waiting.remove(&key).unwrap_or(u32::MAX);
        while let Some(&(v, next)) = self.eph_links.get(i as usize) {
            self.mark(v);
            i = next;
        }
    }

    #[inline]
    fn mark_gray(&mut self, g: Gray) {
        let color = match g {
            Gray::TablePart(..) => return,
            Gray::Proto(i) => self.protos.color.get_mut(i as usize),
            Gray::Table(i) => self.tables.color.get_mut(i as usize),
            Gray::Func(i) => self.funcs.color.get_mut(i as usize),
            Gray::Upval(i) => self.upvals.color.get_mut(i as usize),
            Gray::Ud(i) => self.uds.color.get_mut(i as usize),
            Gray::Thread(i) => self.threads.color.get_mut(i as usize),
        };
        if let Some(c) = color
            && *c == WHITE
        {
            *c = GRAY;
            self.gray.push(g);
        }
    }

    fn is_marked(&self, v: Value) -> bool {
        let c = match v {
            Value::Str(r) => self.strings.color.get(r.idx as usize),
            Value::Table(r) => self.tables.color.get(r.idx as usize),
            Value::Func(r) => self.funcs.color.get(r.idx as usize),
            Value::UserData(r) => self.uds.color.get(r.idx as usize),
            Value::Thread(r) => self.threads.color.get(r.idx as usize),
            _ => return true,
        };
        c.is_some_and(|&c| c == GRAY || c == BLACK)
    }

    /// The weak mode of a table with metatable `meta`: (weak keys, weak values).
    /// S5B-WALK-3: the `__mode` string was scanned whole for every table traversed, free:
    /// 200 tables under a 4 MiB `__mode` ran at 110x plain code per unit of fuel (far more
    /// at a short pause). C Lua reads a mode only from a short string, up to its first zero
    /// byte (`traversetable`: `ttisshrstring`, `strchr`); so does this, which bounds the
    /// scan at `SHORT_LEN` bytes and gives C Lua's answer for a long or zero-cut mode.
    #[inline(always)]
    fn weak_mode(&self, meta: Option<TableRef>) -> (bool, bool) {
        let (Some(m), Some(key)) = (meta, self.mode_key) else { return (false, false) };
        let Some(mt) = self.tables.get(m.idx) else { return (false, false) };
        match mt.get_str(Value::Str(key)) {
            Value::Str(s) if self.str_bytes(s).len() <= SHORT_LEN => {
                let b = self.str_bytes(s);
                let b = b.split(|&c| c == 0).next().unwrap_or_default();
                (b.contains(&b'k'), b.contains(&b'v'))
            }
            _ => (false, false),
        }
    }

    /// Traverses one gray object: marks what it holds. Returns the work done.
    /// Traverses table `i`'s slots from `from` (array part, then hash nodes); in the atomic
    /// phase at most `TABLE_CHUNK` of them, the rest going back on the gray list
    /// (`Gray::TablePart`), so the phase can stop at a call's fuel inside a big table
    /// (S6GC-2e).
    fn traverse_table(&mut self, i: u32, from: usize) -> usize {
        let cycle = self.cycle;
        let Some((meta, alen, total, snap)) =
            self.tables.at(i as usize).map(|t| (t.meta, t.arr.live_len(), t.arr.live_len() + t.hash_len(), t.slots_at(cycle)))
        else {
            return 1;
        };
        if from == 0 && !self.eph_active && total > TABLE_CHUNK {
            return self.big_table(i);
        }
        if from == 0 {
            if let Some(c) = self.tables.color.get_mut(i as usize) {
                *c = BLACK;
            }
            if let Some(m) = meta {
                self.mark(Value::Table(m));
            }
        }
        let (wk, wv) = self.weak_mode(meta);
        // In chunks of `TABLE_CHUNK` inside the atomic phase, where no script runs between
        // them (`Gray::TablePart`); while marking is incremental a big table is traversed in
        // chunks by `part_step` instead, so here it is whole.
        // (An ephemeron table's entries cost `EPH_ENTRY_WORK + 4 * obj_work` each there: in
        // chunks of 256, one chunk is about as much work as the atomic phase does between
        // two looks at the interrupt and the deadline, not 20 times that.)
        let chunk = match (self.eph_active, wk && !wv) {
            (true, true) => EPH_CHUNK,
            (true, false) => TABLE_CHUNK,
            (false, _) => usize::MAX,
        };
        let to = from.saturating_add(chunk).min(total);
        let (nodes_from, nodes_to) = (from.saturating_sub(alen), to.saturating_sub(alen));
        self.mark_slots::<false>(i, (from, to), (alen, total), (wk, wv));
        self.most_at_once = self.most_at_once.max(to.saturating_sub(from));
        self.scanned = self.scanned.wrapping_add(to.saturating_sub(from));
        if to < total {
            self.gray.push(Gray::TablePart(i, u32::try_from(to).unwrap_or(u32::MAX)));
        } else if wk || wv {
            self.weak.push((i, wk, wv));
            // Weak tables are revisited in the atomic step.
            if let Some(c) = self.tables.color.get_mut(i as usize) {
                *c = GRAY;
            }
        }
        // By its slots when the cycle started or it was made (`Table::gc_snap`), never by
        // what it holds or its size when reached: when that is follows the order objects are
        // reached in, so the hash seed (S6GC-2e by what it held, COV-5 by its size, in the
        // atomic step too). Its growth is paid by the resizes. Charged a part at a time.
        let slots = to.min(snap) - from.min(snap) + if to >= total { snap.saturating_sub(total) } else { 0 };
        let mut work = usize::from(from == 0) + slots * (1 + self.obj_work / 4);
        if wk && !wv {
            // Ephemeron: a value is marked when its key is.
            work += self.mark_ephemeron(i, nodes_from, nodes_to);
        }
        work
    }

    /// Marks what table `i` holds in slots `from..to` (array part, then hash nodes) as a
    /// table of weak mode (`wk`, `wv`), entries as `entry_marks` decides when `PART` (the
    /// table traversed in chunks): the objects other than strings marked. (A constant, so
    /// the traversal of a whole table is the code it was before chunks.)
    #[inline(always)]
    fn mark_slots<const PART: bool>(
        &mut self,
        i: u32,
        (from, to): (usize, usize),
        (alen, total): (usize, usize),
        (wk, wv): (bool, bool),
    ) -> usize {
        // Collected first, so the table is not borrowed while marking. Strings are
        // values, never weak: they are always marked.
        let is_str = |v: &Value| matches!(v, Value::Str(_));
        let node = |n: &crate::table::Node, todo: &mut Vec<Value>| {
            if n.val.is_nil() {
                return;
            }
            if PART {
                let (mk, mv) = entry_marks(n.key(), n.val, (wk, wv));
                if mk {
                    todo.push(n.key());
                }
                if mv {
                    todo.push(n.val);
                }
                return;
            }
            if !wk || is_str(&n.key()) {
                todo.push(n.key());
            }
            if (!wv && !wk) || is_str(&n.val) {
                todo.push(n.val);
            }
        };
        // S6GC-M1: marked `TRAV_CHUNK` slots at a time, so the buffer stays small (a table
        // of 2^23 slots holding one shared table filled it with 128 MiB of copies,
        // uncharged and kept).
        let mut todo = std::mem::take(&mut self.trav_vals);
        let mut marked = 0;
        let mut at = from;
        while at < to {
            let end = (at + TRAV_CHUNK).min(to);
            if let Some(t) = self.tables.at(i as usize) {
                for (_, part) in t.arr.chunks(at..end) {
                    todo.extend(part.iter().copied().filter(|v| v.is_collectable() && (!wv || is_str(v))));
                }
                let (named, ints) = t.hash_parts();
                if at == 0 && end == total {
                    // The whole table at once (most tables): finding positions in the hash
                    // parts made marking small tables a quarter slower.
                    named.iter().for_each(|n| node(n, &mut todo));
                    ints.iter().for_each(|n| node(n, &mut todo));
                } else if end > alen {
                    let (nf, nt) = (at.saturating_sub(alen), end - alen);
                    // (The two parts' slices, not `hash_nodes().skip()`: a chain stepped
                    // node by node cost a chunk of a big table twice its marking.)
                    let k = named.len();
                    named.get(nf.min(k)..nt.min(k)).unwrap_or_default().iter().for_each(|n| node(n, &mut todo));
                    ints.get(nf.saturating_sub(k)..nt.saturating_sub(k)).unwrap_or_default().iter().for_each(|n| node(n, &mut todo));
                }
            }
            // Each object marked is a mark read at a random place (hash order follows no
            // allocation order): in a big heap a cache miss, paid by the live heap's size
            // (S6GC-2d: a strong table of 800,000 objects ran at 3x plain code per unit).
            marked += todo.iter().filter(|v| !matches!(v, Value::Str(_))).count();
            for v in todo.drain(..) {
                self.mark(v);
            }
            at = end;
        }
        self.trav_vals = todo;
        marked
    }

    fn traverse(&mut self, g: Gray, running: Option<&ThreadState>) -> usize {
        self.release_waiting(g);
        match g {
            Gray::Proto(i) => {
                if let Some(c) = self.protos.color.get_mut(i as usize) {
                    *c = BLACK;
                }
                let (consts, children, walked): (Vec<Value>, Vec<u32>, usize) = match self.protos.get(i) {
                    Some(p) => (
                        p.consts.iter().copied().chain(std::iter::once(p.chunk_name)).filter(|v| v.is_collectable()).collect(),
                        p.protos.clone(),
                        p.consts.len(),
                    ),
                    None => (Vec::new(), Vec::new(), 0),
                };
                // Every constant looked at counts, not only the objects among them
                // (S5B-WALK-1).
                let work = 1 + walked + children.len();
                for k in consts {
                    self.mark(k);
                }
                for c in children {
                    self.mark_gray(Gray::Proto(c));
                }
                work
            }
            Gray::Table(i) => self.traverse_table(i, 0),
            // Finished even when a write sent the table back to gray (it is traversed again
            // whole in the atomic step): skipping the rest would save work by where the
            // write fell, which follows the order objects are reached in, so the seed.
            Gray::TablePart(i, from) => self.traverse_table(i, from as usize),
            Gray::Func(i) => {
                if let Some(c) = self.funcs.color.get_mut(i as usize) {
                    *c = BLACK;
                }
                let mut todo_up = std::mem::take(&mut self.trav_ups);
                let mut todo_vals = std::mem::take(&mut self.trav_vals);
                let mut proto = None;
                match self.funcs.get(i) {
                    Some(Func::Lua { proto: p, upvals }) => {
                        proto = Some(*p);
                        todo_up.extend(upvals.iter().map(|u| u.idx));
                    }
                    Some(Func::Native { upvals, .. }) => todo_vals.extend(upvals.iter().copied()),
                    _ => {}
                }
                if let Some(p) = proto {
                    self.mark_gray(Gray::Proto(p));
                }
                let work = 1 + todo_up.len() + todo_vals.len();
                for u in todo_up.drain(..) {
                    self.mark_gray(Gray::Upval(u));
                }
                for v in todo_vals.drain(..) {
                    self.mark(v);
                }
                (self.trav_ups, self.trav_vals) = (todo_up, todo_vals);
                work
            }
            Gray::Upval(i) => {
                if let Some(c) = self.upvals.color.get_mut(i as usize) {
                    *c = BLACK;
                }
                match self.upvals.get(i).copied() {
                    Some(Upval::Closed(v)) => self.mark(v),
                    Some(Upval::Open { thread, .. }) => self.mark(Value::Thread(thread)),
                    None => {}
                }
                1
            }
            Gray::Ud(i) => {
                if let Some(c) = self.uds.color.get_mut(i as usize) {
                    *c = BLACK;
                }
                if let Some(m) = self.uds.get(i).and_then(|u| u.meta) {
                    self.mark(Value::Table(m));
                }
                1
            }
            Gray::Thread(i) => {
                // Threads stay gray: their stacks change without barriers, so they are
                // marked again in the atomic step.
                let mut todo: Vec<Value> = Vec::new();
                let mut protos: Vec<u32> = Vec::new();
                let mut ups: Vec<u32> = Vec::new();
                // S6GC-M2: of a suspended or dead coroutine only the live part of the stack
                // is marked (C Lua's `traversethread`): stale slots above it held garbage
                // (a weak table kept it; six parked coroutines each with a dead 16 MiB
                // string ran out of a 64 MiB cap). The atomic step clears them, so a value
                // left there is not found again when the coroutine runs.
                let mut walked = 0;
                if let Some(t) = self.threads.get(i) {
                    let live = parked_live(t, &self.protos);
                    walked = collect_thread(t, live.unwrap_or(usize::MAX), &mut todo, &mut protos, &mut ups);
                    if self.eph_active
                        && let Some(live) = live
                        && let Some(t) = self.threads.get_mut(i)
                    {
                        t.stack.get_mut(live..).unwrap_or_default().fill(Value::Nil);
                    }
                }
                let _ = running;
                let work = 1 + walked;
                for v in todo {
                    self.mark(v);
                }
                for p in protos {
                    self.mark_gray(Gray::Proto(p));
                }
                for u in ups {
                    self.mark_gray(Gray::Upval(u));
                }
                work
            }
        }
    }

    /// An ephemeron table: marks values whose keys are marked; while the atomic step
    /// settles ephemerons, the others wait for their key (`eph_waiting`). Returns the work:
    /// in the atomic step `EPH_ENTRY_WORK` an entry (S5B-COST-3).
    fn mark_ephemeron(&mut self, i: u32, from: usize, to: usize) -> usize {
        let Some(t) = self.tables.at(i as usize) else { return 0 };
        let mut todo = Vec::new();
        for n in t.hash_nodes().skip(from).take(to.saturating_sub(from)) {
            if !n.val.is_nil() && n.val.is_collectable() {
                todo.push((n.key(), n.val));
            }
        }
        // In the atomic step an entry is looked up in `eph_waiting`, linked, found again and
        // its value marked, each at a random place, and outside it its key's mark is read
        // at a random place: in a big heap these miss the caches as reaching an object
        // does, so they pay `obj_work` too (by the live heap's size, never its layout;
        // S6GC-2d: an 800,000-entry chain ran at 3x a strong one per unit of fuel).
        let work = if self.eph_active { todo.len() * (EPH_ENTRY_WORK + 4 * self.obj_work) } else { todo.len() * (1 + self.obj_work / 2) };
        // While marking is incremental, values are left to the atomic step, all of them:
        // marked here when their key happened to be marked already, whether a value was
        // reached then (and, a weak table or thread, traversed again there) or only there
        // followed the order objects are reached in, so the seed (SEC7-H1: 400 weak-table
        // values cost 5960 to 6191 by the seed).
        if !self.eph_active {
            return work;
        }
        for (k, v) in todo {
            if self.is_marked(k) {
                if !self.is_marked(v) {
                    self.mark(v);
                }
            } else {
                let key = match k {
                    Value::Table(r) => (1, r.idx),
                    Value::Func(r) => (2, r.idx),
                    Value::UserData(r) => (4, r.idx),
                    Value::Thread(r) => (5, r.idx),
                    _ => continue,
                };
                let head = self.eph_waiting.get(&key).copied().unwrap_or(u32::MAX);
                self.eph_links.push((v, head));
                self.eph_waiting.insert(key, (self.eph_links.len() - 1) as u32);
            }
        }
        work
    }

    /// Marks the running thread's state (kept outside the slab while it runs): the work,
    /// the slots and records walked (S5B-WALK-1).
    pub fn mark_thread_state(&mut self, t: &ThreadState) -> usize {
        let mut todo = Vec::new();
        let mut protos = Vec::new();
        let mut ups = Vec::new();
        let walked = collect_thread(t, usize::MAX, &mut todo, &mut protos, &mut ups);
        for v in todo {
            self.mark(v);
        }
        for p in protos {
            self.mark_gray(Gray::Proto(p));
        }
        for u in ups {
            self.mark_gray(Gray::Upval(u));
        }
        walked
    }

    /// Starts a cycle: everything white, roots to be marked by the caller.
    pub fn start_cycle(&mut self) {
        self.pace_carry = 0;
        self.cycle = self.cycle.wrapping_add(1).max(1);
        self.mem.cycle = self.cycle;
        let live = |items: usize, free: usize| items.saturating_sub(free);
        self.obj_work = obj_work(
            live(self.tables.slots(), self.tables.free.len())
                + live(self.funcs.slots(), self.funcs.free.len())
                + live(self.threads.slots(), self.threads.free.len())
                + live(self.protos.slots(), self.protos.free.len()),
        );
        self.born.obj_work = self.obj_work;
        (self.mem.marking, self.mem.obj_work) = (true, self.obj_work);
        self.end_part();
        self.part_queue.clear();
        self.gray.clear();
        self.born.list.clear();
        self.grayagain.clear();
        self.weak.clear();
        self.phase = Phase::Propagate;
    }

    /// Does up to `budget` units of marking; true when the gray list is empty.
    /// (Stopped early by `stop`: the third is true, the step's work done so far.) The
    /// fourth: the table slots it scanned, counted apart from its work (SEC7-G1).
    pub fn propagate(&mut self, budget: usize, stop: &Stop<'_>) -> (bool, usize, bool, usize) {
        self.traverse_born();
        let carry = std::mem::take(&mut self.pace_carry);
        self.probe_budget += budget as u64;
        let st = Prop { done: 0, next: ATOMIC_CHECK, redone: 0, slots: 0, pre: 0, given: budget, carry };
        self.propagate_paced(budget.saturating_sub(carry), stop, st)
    }

    /// `propagate_from`, then what its prepaid slots took past the budget carried to the
    /// next step (none once marking has nothing left). What it charges is its work alone:
    /// the prepaid slots were paid as the table grew (or by the rebuild that sent the
    /// traversal back), so they spend the budget without being charged again.
    fn propagate_paced(&mut self, limit: usize, stop: &Stop<'_>, st: Prop) -> (bool, usize, bool, usize) {
        let (given, carry) = (st.given, st.carry);
        let (empty, done, cut, slots, pre) = self.propagate_from(limit, stop, st);
        if !self.paused {
            // (Past the budget, what the work took is the caller's to carry, `gc_mark_over`;
            // what the prepaid slots took, this.)
            self.pace_carry = if empty { 0 } else { (done + pre + carry).saturating_sub(given).saturating_sub(done.saturating_sub(given)) };
        }
        (empty, done, cut, slots)
    }

    /// Goes on with the marking step that paused at the end of the host's poll (`paused`),
    /// inside its loop, as it stood: nothing it did is done again, its budget is the one it
    /// had. It looks at the interrupt and the deadline first (SEC7-G1).
    pub fn propagate_resume(&mut self, stop: &Stop<'_>) -> (bool, usize, bool, usize) {
        let Some((budget, mut st)) = self.prop_saved.take() else { return (false, 0, false, 0) };
        st.next = st.done + st.redone;
        self.propagate_paced(budget, stop, st)
    }

    fn propagate_from(&mut self, budget: usize, stop: &Stop<'_>, st: Prop) -> (bool, usize, bool, usize, usize) {
        // (A chunk a rebuild sent back over is not charged again: counted in `redone` for
        // the looks at the stop all the same; `slots`: the slots scanned, apart from the
        // work, `part_step`.)
        let Prop { mut done, mut next, mut redone, mut slots, mut pre, given, carry } = st;
        while done + pre < budget {
            if done + redone >= next {
                next = done + redone + ATOMIC_CHECK;
                if stop.interrupted() || stop.past_deadline() {
                    return (false, done, true, slots, pre);
                }
            }
            // The table traversed in chunks goes on first, chunk after chunk, as one
            // traversal of it whole did: when its last chunk comes then follows the work
            // alone. (Taking what its chunks mark first made that, and the order of the rest
            // among the script's writes, follow the layout, and with them the fuel.)
            if self.part != NO_PART {
                let (w, n, prepaid) = self.part_step();
                done += w;
                slots += n;
                pre += prepaid * GROWN_SLOT_WORK;
                self.probe_prepaid += prepaid as u64;
                if w == 0 {
                    redone += TABLE_CHUNK;
                }
                // Its slots too bound a step (`step_slots`): it ends here, its work done.
                if slots >= self.step_slots && self.part != NO_PART {
                    return (false, done, false, slots, pre);
                }
                // At the end of the host's poll, after a chunk (never inside one), a step
                // that may pause does: its state kept for `propagate_resume`.
                if self.pause_ok && done + pre < budget && self.poll_allow.is_some_and(|a| self.scanned.wrapping_sub(self.poll_start) >= a) {
                    self.prop_saved = Some((budget, Prop { done, next, redone, slots, pre, given, carry }));
                    self.paused = true;
                    return (false, done, false, slots, pre);
                }
            } else if let Some(g) = self.gray.pop() {
                let at = self.scanned;
                done += self.obj_work + self.traverse(g, None);
                slots += self.scanned.wrapping_sub(at);
                self.poll_objs += 1;
                // At the end of the host's poll, after an object too (SEC7-G1: a step of
                // 200,000 small objects took 9.5 ms between chunks).
                if done + pre < budget && self.poll_over() {
                    self.prop_saved = Some((budget, Prop { done, next, redone, slots, pre, given, carry }));
                    self.paused = true;
                    return (false, done, false, slots, pre);
                }
            } else if let Some(t) = self.part_queue.pop() {
                // (Its start was charged when it was reached, as for one started at once:
                // which it is follows the order tables are reached in.)
                self.begin_part(t);
            } else {
                return (true, done, false, slots, pre);
            }
        }
        (self.gray.is_empty() && self.part == NO_PART && self.part_queue.is_empty(), done, false, slots, pre)
    }

    /// Whether a step that may pause (`pause_ok`) is at the end of the host's poll: its
    /// slots scanned and objects traversed since the poll started reach `poll_allow`.
    #[inline]
    pub(crate) fn poll_over(&self) -> bool {
        self.pause_ok && self.poll_allow.is_some_and(|a| self.scanned.wrapping_sub(self.poll_start) + self.poll_objs >= a)
    }

    /// The objects made since the last step, traversed (paid for when they were made).
    fn traverse_born(&mut self) {
        while let Some(g) = self.born.list.pop() {
            self.traverse(g, None);
        }
    }

    /// Ends marking (roots already marked again by the caller): drains every gray object,
    /// settles ephemerons, clears weak tables, and starts the sweep.
    ///
    /// The atomic phase: the mutator must not run inside it, and its work grows with the
    /// heap (a weak table or ephemerons of a million entries take tens of milliseconds). It
    /// stops two ways:
    /// - after `allow` units of work (the fuel the running call has left): it is suspended
    ///   (`Atomic::Suspended`) where it stands, the call stops on its fuel limit, and the
    ///   next entry from the host resumes it before any script code runs (S6GC-2e: run in
    ///   one go, a script ran up to one whole atomic phase past its fuel, 460 ms over an
    ///   800,000-entry ephemeron chain, growing with the memory cap). Objects written while
    ///   it is suspended (by the host) are barriered as during marking and marked again
    ///   when it resumes.
    /// - on the host's interrupt, looked at every `ATOMIC_CHECK` units: the cycle is
    ///   abandoned (`abandon_cycle`), so a stuck host never keeps a half-done cycle.
    /// - at the time limit's deadline, looked at as often: suspended as at the allowance.
    pub fn atomic(&mut self, stop: &Stop<'_>, allow: usize) -> Atomic {
        // (Paused at the end of the host's poll: its counters as they stood.)
        let saved = self.atomic_saved.take();
        let [mut work, mut next, saved_next, saved_scanned, saved_looked] = saved.unwrap_or([0, ATOMIC_CHECK, 0, 0, 0]);
        let stage = match self.atomic_stage.take() {
            None => {
                let again = std::mem::take(&mut self.grayagain);
                for g in again {
                    // Barriered objects are gray; force a traversal.
                    self.force_gray(g);
                }
                // Weak tables are traversed again too.
                let weak = std::mem::take(&mut self.weak);
                for (t, _, _) in &weak {
                    self.force_gray(Gray::Table(*t));
                }
                // Room for the ephemerons waiting for their keys, taken now (at most an entry a
                // slot of the ephemeron tables known, at most the objects there are): grown
                // by doubling as they came, a step of 256 entries rehashed 300,000 of them,
                // 4.5 ms without a look at the deadline (S6GC-2e-R). Given back when the
                // cycle ends, as the rest of the collector's scratch.
                let eph: usize = weak
                    .iter()
                    .filter(|&&(_, wk, wv)| wk && !wv)
                    .map(|&(t, _, _)| self.tables.at(t as usize).map_or(0, Table::slots))
                    .sum();
                let room = eph.min(self.live_objects());
                self.eph_waiting.reserve(room);
                self.eph_links.reserve(room);
                // Threads are marked again (they were left gray).
                // (Skipping free slots: S5GC-2.)
                let mut threads: Vec<u32> = Vec::new();
                let mut i = self.threads.next_used(0);
                while i < self.threads.slots() {
                    if self.threads.color.get(i).is_some_and(|&c| c == GRAY || c == BLACK) {
                        threads.push(i as u32);
                    }
                    i = self.threads.next_used(i + 1);
                }
                work += threads.len();
                for t in threads {
                    self.gray.push(Gray::Thread(t));
                }
                // Ephemeron values wait for their keys from here on: marking a key marks them.
                self.eph_active = true;
                AtomicStage::Mark
            }
            Some(AtomicStage::Mark) => {
                // Resumed: what the host wrote while it was suspended is marked again.
                let again = std::mem::take(&mut self.grayagain);
                for g in again {
                    self.force_gray(g);
                }
                AtomicStage::Mark
            }
            Some(clear) => clear,
        };
        let (weak, at) = match stage {
            AtomicStage::Mark => {
                self.traverse_born();
                // By the slots scanned too: a table's slots past its size when the cycle
                // started add no work (their marking was paid as it grew), so a piece of
                // them ran on to the next look, 3.6 ms between two reads of the deadline,
                // 18 ms in a call (SEC7-G1).
                // (Its own count: the clearing below looks by its work alone.)
                let (scanned, mut looked, mut mark_next) = if saved.is_some() { (saved_scanned, saved_looked, saved_next) } else { (self.scanned, self.scanned, next) };
                while let Some(g) = self.gray.pop() {
                    work += self.obj_work + self.traverse(g, None);
                    self.poll_objs += 1;
                    let seen = self.scanned.wrapping_sub(scanned);
                    let mut look = false;
                    if work + seen >= mark_next {
                        look = true;
                        mark_next = work + seen + ATOMIC_CHECK;
                        next = work + ATOMIC_CHECK;
                        self.most_between_looks = self.most_between_looks.max(self.scanned.wrapping_sub(looked));
                        looked = self.scanned;
                        if stop.interrupted() {
                            self.abandon_cycle();
                            return Atomic::Interrupted(work);
                        }
                        if stop.past_deadline() {
                            self.atomic_stage = Some(AtomicStage::Mark);
                            return Atomic::TimedOut(work);
                        }
                    }
                    if work > allow {
                        self.atomic_stage = Some(AtomicStage::Mark);
                        return Atomic::Suspended(work);
                    }
                    // At the end of the host's poll, at a look, after the allowance's test
                    // (as it stood, the resume goes on with the next object: SEC7-G1).
                    if look && self.poll_over() && !self.gray.is_empty() {
                        self.atomic_stage = Some(AtomicStage::Mark);
                        self.atomic_saved = Some([work, next, mark_next, scanned, looked]);
                        return Atomic::Paused;
                    }
                }
                self.eph_active = false;
                self.eph_waiting.clear();
                self.eph_links.clear();
                (std::mem::take(&mut self.weak), (0, 0))
            }
            AtomicStage::Clear(weak, at, pos) => (weak, (at, pos)),
        };
        let (mut at, mut pos) = at;
        // Clear weak entries whose key or value was not marked.
        while let Some(&(t, wk, wv)) = weak.get(at) {
            let (w, more) = self.clear_weak(t, wk, wv, pos);
            work += w;
            self.poll_objs += w;
            match more {
                Some(p) => pos = p,
                None => {
                    at += 1;
                    pos = 0;
                }
            }
            let mut look = false;
            if work >= next {
                look = true;
                next = work + ATOMIC_CHECK;
                if stop.interrupted() {
                    // Tables not cleared yet must be before anything is swept: abandoning
                    // is the only safe stop here (clearing early is harmless: the entries
                    // cleared were unreachable).
                    self.abandon_cycle();
                    return Atomic::Interrupted(work);
                }
                if stop.past_deadline() && at < weak.len() {
                    self.atomic_stage = Some(AtomicStage::Clear(weak, at, pos));
                    return Atomic::TimedOut(work);
                }
            }
            if work > allow && at < weak.len() {
                self.atomic_stage = Some(AtomicStage::Clear(weak, at, pos));
                return Atomic::Suspended(work);
            }
            if look && self.poll_over() && at < weak.len() {
                self.atomic_saved = Some([work, next, 0, 0, 0]);
                self.atomic_stage = Some(AtomicStage::Clear(weak, at, pos));
                return Atomic::Paused;
            }
        }
        self.phase = Phase::Sweep;
        self.sweep = (0, 0);
        self.mem.marking = false;
        self.interner.peak = self.interner.peak.max(self.interner.count);
        self.trim_scratch();
        Atomic::Done(work)
    }

    /// The sweep's work units still to come in this cycle, at least (`Lua::collector_status`):
    /// a unit a used slot, for each slab the sweep has not reached (all of them while
    /// marking). The slab it is in, a dead interned string's walk and the interner's shrink
    /// are not counted: the floor, not the cost.
    pub(crate) fn sweep_floor(&self) -> usize {
        let from = match self.phase {
            Phase::Pause => return 0,
            Phase::Propagate => 0,
            Phase::Sweep => usize::from(self.sweep.0) + 1,
        };
        let used = |slots: usize, free: usize| slots.saturating_sub(free);
        let counts = [
            used(self.strings.slots(), self.strings.free.len()),
            used(self.tables.slots(), self.tables.free.len()),
            used(self.funcs.slots(), self.funcs.free.len()),
            used(self.upvals.slots(), self.upvals.free.len()),
            used(self.uds.slots(), self.uds.free.len()),
            used(self.threads.slots(), self.threads.free.len()),
            used(self.protos.slots(), self.protos.free.len()),
        ];
        counts.iter().skip(from).sum()
    }

    /// Whether an atomic phase is suspended (see `atomic`): it must be finished before any
    /// script code runs.
    #[inline]
    pub fn atomic_suspended(&self) -> bool {
        self.atomic_stage.is_some()
    }

    /// Gives up the cycle in progress: every object is white again (as after a sweep) and
    /// the next step starts a new cycle from the roots. Nothing is freed, so nothing a
    /// script or the host holds can go stale.
    pub fn abandon_cycle(&mut self) {
        fn whiten(color: &mut [u8]) {
            for c in color.iter_mut().filter(|c| **c != FREE) {
                *c = WHITE;
            }
        }
        whiten(&mut self.strings.color);
        whiten(&mut self.tables.color);
        whiten(&mut self.funcs.color);
        whiten(&mut self.upvals.color);
        whiten(&mut self.uds.color);
        whiten(&mut self.threads.color);
        whiten(&mut self.protos.color);
        self.end_part();
        self.part_queue.clear();
        self.gray.clear();
        self.born.list.clear();
        self.grayagain.clear();
        self.weak.clear();
        self.eph_waiting.clear();
        self.eph_links.clear();
        self.eph_active = false;
        self.atomic_stage = None;
        self.atomic_saved = None;
        self.phase = Phase::Pause;
        self.mem.marking = false;
        self.trim_scratch();
    }

    /// Gives back the collector's scratch buffers grown past `SCRATCH_KEEP` (S6GC-M1: kept
    /// at their peak, uncharged, they held 147 MB resident past the account after one
    /// cycle over a table of 2^23 slots).
    pub(crate) fn trim_scratch(&mut self) {
        trim_vec(&mut self.gray);
        trim_vec(&mut self.born.list);
        trim_vec(&mut self.grayagain);
        trim_vec(&mut self.weak);
        trim_vec(&mut self.eph_links);
        trim_vec(&mut self.trav_vals);
        trim_vec(&mut self.trav_ups);
        trim_vec(&mut self.mem.watch.moved);
        trim_vec(&mut self.part_queue);
        self.eph_waiting.clear();
        if self.eph_waiting.capacity() * 16 > SCRATCH_KEEP {
            self.eph_waiting = std::collections::HashMap::with_hasher(*self.eph_waiting.hasher());
        }
    }

    /// Bytes of capacity the collector's scratch buffers hold (a test probe).
    pub(crate) fn scratch_bytes(&self) -> usize {
        fn b<T>(v: &Vec<T>) -> usize {
            v.capacity() * std::mem::size_of::<T>()
        }
        b(&self.gray) + b(&self.grayagain) + b(&self.weak) + b(&self.eph_links) + b(&self.trav_vals) + b(&self.trav_ups) + b(&self.mem.watch.moved) + b(&self.part_queue) + self.eph_waiting.capacity() * 16
    }

    fn force_gray(&mut self, g: Gray) {
        let c = match g {
            Gray::TablePart(..) => return,
            Gray::Proto(i) => self.protos.color.get_mut(i as usize),
            Gray::Table(i) => self.tables.color.get_mut(i as usize),
            Gray::Func(i) => self.funcs.color.get_mut(i as usize),
            Gray::Upval(i) => self.upvals.color.get_mut(i as usize),
            Gray::Ud(i) => self.uds.color.get_mut(i as usize),
            Gray::Thread(i) => self.threads.color.get_mut(i as usize),
        };
        if let Some(c) = c
            && *c != FREE
        {
            *c = GRAY;
            self.gray.push(g);
        }
    }

    /// Clears weak table `t`'s dead entries from slot `from` (array part, then hash
    /// nodes), at most `TABLE_CHUNK` slots: the work, and where to go on (None when done).
    fn clear_weak(&mut self, t: u32, wk: bool, wv: bool, from: usize) -> (usize, Option<usize>) {
        // Strings are values, not objects that go away: they count as marked.
        let dead = |h: &Heap, v: Value| v.is_collectable() && !matches!(v, Value::Str(_)) && !h.is_marked(v);
        let Some(tab) = self.tables.at(t as usize) else { return (0, None) };
        let alen = tab.arr.live_len();
        let total = alen + tab.hash_len();
        let to = from.saturating_add(TABLE_CHUNK).min(total);
        let mut kill_arr = Vec::new();
        let mut kill_node = Vec::new();
        for (at, part) in tab.arr.chunks(from..to).filter(|_| wv) {
            for (j, v) in part.iter().enumerate() {
                if dead(self, *v) {
                    kill_arr.push(at + j);
                }
            }
        }
        let (nodes_from, nodes_to) = (from.saturating_sub(alen), to.saturating_sub(alen));
        for (j, n) in tab.hash_nodes().enumerate().skip(nodes_from).take(nodes_to - nodes_from) {
            if n.val.is_nil() {
                continue;
            }
            if (wk && dead(self, n.key())) || (wv && dead(self, n.val)) {
                kill_node.push(j);
            }
        }
        let work = to.saturating_sub(from);
        if let Some(tab) = self.tables.at_mut(t as usize) {
            // (A write: a resize given up on it cannot go on, `table::RESUMABLE`.)
            if !(kill_arr.is_empty() && kill_node.is_empty()) {
                tab.absent.set(0);
                tab.unkept(&mut self.mem);
            }
            for j in kill_arr {
                if let Some(s) = tab.arr.get_mut(j) {
                    *s = Value::Nil;
                }
            }
            for j in kill_node {
                if let Some(n) = tab.hash_node_mut(j) {
                    n.val = Value::Nil;
                }
            }
        }
        if to < total {
            return (work, Some(to));
        }
        if let Some(c) = self.tables.color.get_mut(t as usize) {
            *c = BLACK;
        }
        (work, None)
    }

    /// Sweeps up to `budget` slots; true when the sweep is over. Returns the work done.
    /// (Stopped early by `stop`: the third is true.)
    pub fn sweep_step(&mut self, budget: usize, stop: &Stop<'_>) -> (bool, usize, bool) {
        let mut done = 0;
        // Slots cut from slabs' ends: not work (free slots change neither the pacing nor
        // the fuel), but counted to the next look at the stop.
        let mut cut_all = 0;
        let mut next = ATOMIC_CHECK;
        while done < budget {
            if done + cut_all >= next {
                next = done + cut_all + ATOMIC_CHECK;
                if stop.interrupted() || stop.past_deadline() {
                    return (false, done, true);
                }
            }
            let (slab, idx) = self.sweep;
            let len = match slab {
                0 => self.strings.slots(),
                1 => self.tables.slots(),
                2 => self.funcs.slots(),
                3 => self.upvals.slots(),
                4 => self.uds.slots(),
                5 => self.threads.slots(),
                6 => self.protos.slots(),
                _ => {
                    // The interner's slots given back when most of its strings died (SEC7-H4:
                    // kept at their peak, 4M slots stayed resident, 32 MB outside the
                    // account). The strings move a piece at a time as the sweep's work (a
                    // quarter unit a slot), over as many steps as it takes, the slots set
                    // aside counted meanwhile (a script runs between steps). (Those a regrow
                    // set aside move as new strings come, paid for by them: `regrow_share`.)
                    let i = &mut self.interner;
                    if i.old.is_empty() && i.slots.len() > SHRINK_FROM && i.peak * 8 < i.slots.len() {
                        i.regrow(&self.strings, ((i.count + 1) * 2).next_power_of_two().max(256), i.salt);
                        i.shrunk = i.old.capacity() * 8;
                        self.mem.used += i.shrunk;
                    }
                    i.peak = i.count;
                    if i.shrunk > 0 && !i.old.is_empty() {
                        i.move_some(&self.strings, TABLE_CHUNK);
                        done += TABLE_CHUNK / 4;
                        continue;
                    }
                    self.mem.used -= std::mem::take(&mut i.shrunk).min(self.mem.used);
                    self.phase = Phase::Pause;
                    self.estimate = self.mem.live();
                    return (true, done, false);
                }
            };
            if idx >= len {
                // The slab's free tail is cut a piece at a time, with looks at the stop
                // between them; `usize::MAX` marks the cut under way (and keeps what the
                // script makes meanwhile white: this slab is swept).
                let (cut, more) = self.trim_piece(slab, idx == usize::MAX);
                cut_all += cut;
                self.sweep = if more { (slab, usize::MAX) } else { (slab + 1, 0) };
                continue;
            }
            // Work counts objects, not free slots: the same program costs the same fuel
            // however the slabs were left by earlier runs. Runs of free slots are skipped
            // (S5GC-2).
            match self.sweep_one(slab, idx) {
                0 => self.sweep.1 = self.next_used(slab, idx),
                w => {
                    done += w;
                    self.sweep.1 += 1;
                }
            }
        }
        (false, done, false)
    }

    /// `Slab::trim_piece` of slab number `slab`, `TABLE_CHUNK` slots at most.
    fn trim_piece(&mut self, slab: u8, all: bool) -> (usize, bool) {
        let m = &mut self.mem;
        match slab {
            0 => self.strings.trim_piece(m, all, TABLE_CHUNK),
            1 => self.tables.trim_piece(m, all, TABLE_CHUNK),
            2 => self.funcs.trim_piece(m, all, TABLE_CHUNK),
            3 => self.upvals.trim_piece(m, all, TABLE_CHUNK),
            4 => self.uds.trim_piece(m, all, TABLE_CHUNK),
            5 => self.threads.trim_piece(m, all, TABLE_CHUNK),
            _ => self.protos.trim_piece(m, all, TABLE_CHUNK),
        }
    }

    /// `Slab::next_used` of slab number `slab`.
    fn next_used(&self, slab: u8, from: usize) -> usize {
        match slab {
            0 => self.strings.next_used(from),
            1 => self.tables.next_used(from),
            2 => self.funcs.next_used(from),
            3 => self.upvals.next_used(from),
            4 => self.uds.next_used(from),
            5 => self.threads.next_used(from),
            _ => self.protos.next_used(from),
        }
    }

    /// Sweeps one slot: the work done (0 when it was free; an interned string's removal
    /// from the interner walks its run, S5GC-3).
    fn sweep_one(&mut self, slab: u8, i: usize) -> usize {
        macro_rules! sweep {
            ($s:expr, $size:expr) => {{
                if $s.color.get(i) == Some(&FREE) {
                    return 0;
                }
                match $s.color.get(i).copied() {
                    Some(WHITE) => {
                        let size = $s.at(i).map($size).unwrap_or(0);
                        self.mem.release(size);
                        $s.free_slot(i, &mut self.mem);
                    }
                    Some(GRAY) | Some(BLACK) => {
                        if let Some(c) = $s.color.get_mut(i) {
                            *c = WHITE;
                        }
                    }
                    _ => {}
                }
            }};
        }
        let mut work = 1;
        match slab {
            0 => {
                if self.strings.color.get(i) == Some(&WHITE) && self.strings.at(i).is_some_and(|s| s.interned) {
                    work += self.unintern(i as u32);
                }
                sweep!(self.strings, |s: &LStr| str_mem(s.bytes.len()))
            }
            1 => {
                // A dead table's buffers go to the pool (after its size is released).
                if self.tables.color.get(i) == Some(&WHITE) {
                    let size = self.tables.at(i).map_or(0, Table::mem_size);
                    self.mem.release(size);
                    if let Some(t) = self.tables.at_mut(i) {
                        if t.partial != 0 {
                            crate::table::drop_partial(&mut self.mem, std::mem::take(&mut t.partial));
                        }
                        self.mem.pool_give(t);
                    }
                    self.tables.free_slot(i, &mut self.mem);
                } else {
                    sweep!(self.tables, |t: &Table| t.mem_size())
                }
            }
            2 => sweep!(self.funcs, |f: &Func| FUNC_BASE
                + match f {
                    Func::Lua { upvals, .. } => boxed(upvals.len() * 8),
                    Func::Native { upvals, .. } => boxed(upvals.len() * 16),
                    Func::Host { bytes, .. } => *bytes,
                }),
            3 => sweep!(self.upvals, |_: &Upval| UPVAL_SIZE),
            4 => {
                // A userdata the host asked about is reported (its key queued; nothing runs).
                if self.uds.color.get(i) == Some(&WHITE)
                    && let Some(key) = self.uds.at(i).and_then(|u| u.notify)
                {
                    self.collected.push(key);
                }
                sweep!(self.uds, |u: &UserData| UD_BASE + u.size)
            }
            5 => sweep!(self.threads, |t| t.mem_size()),
            6 => sweep!(self.protos, |p: &Proto| p.mem_size()),
            _ => {}
        }
        work
    }

    /// Gives back the stack of coroutines that are not running: a suspended one keeps only
    /// its top frame's `frame_size` (its register window, 256 slots, is made again when it
    /// is resumed), a dead one nothing. Parked coroutines then cost about what C Lua's do,
    /// while a running frame keeps its full window (no bounds checks in the fast loop).
    pub fn shrink_parked(&mut self) {
        let Heap { threads, protos, mem, .. } = self;
        // (Skipping free slots: S5GC-2.)
        let mut next = threads.next_used(0);
        while next < threads.slots() {
            let i = next;
            next = threads.next_used(i + 1);
            let c = threads.color.get(i).copied();
            let (Some(t), Some(c)) = (threads.at_mut(i), c) else { break };
            if c == FREE || t.fresh {
                continue;
            }
            shrink_frames(&mut t.frames, mem);
            if t.status == CoStatus::Dead && t.frames.capacity() > 0 && t.frames.is_empty() {
                // Its frame records too (S5GC-7).
                mem.release(t.frames.capacity() * FRAME_BYTES);
                t.frames = Vec::new();
            }
            let want = match parked_live(t, protos) {
                Some(0) => 0,
                Some(live) => live + crate::vm::EXTRA_STACK,
                None => continue,
            };
            let cap = t.stack.capacity();
            if cap < want * 2 + 16 {
                continue;
            }
            // S6GC-H2: into a fresh allocation of exactly that size, not `shrink_to`: the
            // allocator shrinks in place and leaves the window's tail as a hole too small
            // for the next coroutine's window, so parked coroutines kept ~5x the memory cap
            // resident. A copy frees the whole old chunk for the next window to reuse.
            let keep = want.min(t.stack.len());
            let mut fresh = Vec::with_capacity(want);
            fresh.extend_from_slice(t.stack.get(..keep).unwrap_or_default());
            t.stack = fresh;
            mem.release(cap.saturating_sub(t.stack.capacity()) * SLOT_SIZE);
        }
    }

    /// Cuts every free slot at the end of each slab (after a full collection: SEC-M5).
    /// A piece at a time, with a look at `stop` between pieces: true when it said to stop
    /// (SEC7-H3: a tail under a quarter of its slab was cut in one go, 12 to 16 ms).
    pub fn trim_slabs(&mut self, stop: &Stop<'_>) -> bool {
        for slab in 0..7 {
            while self.trim_piece(slab, true).1 {
                if stop.interrupted() || stop.past_deadline() {
                    return true;
                }
            }
        }
        false
    }

    /// Work units a step does for the debt accumulated.
    pub fn step_budget(&self) -> usize {
        (self.mem.debt.max(STEP_SIZE).saturating_mul(self.stepmul as isize) / 800).max(64) as usize
    }
}

/// Where the live part of a coroutine's stack ends while it is not running (C Lua's
/// `L->top` for it); None for a thread whose whole stack counts (running, normal, fresh).
///
/// A suspended one needs its top frame's registers, the slot its yield's results go to,
/// and its open upvalues and pending to-be-closed variables; a dead one only the last two
/// (S5B-SOAK-1: one that died with an error keeps its pending to-be-closed variables for
/// `coroutine.close`, as Lua 5.4 does not unwind it). Varargs sit below their frame's base.
pub(crate) fn parked_live(t: &ThreadState, protos: &Slab<Proto>) -> Option<usize> {
    if t.fresh {
        return None;
    }
    let mut live = match t.status {
        CoStatus::Dead => 0,
        CoStatus::Suspended => {
            let f = t.frames.last()?;
            let p = protos.get(f.proto)?;
            let mut live = f.base + p.frame_size();
            if let Some(y) = t.yield_pending {
                live = live.max(y.func + 1);
            }
            live
        }
        _ => return None,
    };
    if let Some(&(i, _)) = t.open_upvals.last() {
        live = live.max(i + 1);
    }
    if let Some(&i) = t.tbc.last() {
        live = live.max(i + 1);
    }
    Some(live)
}

/// Everything a thread's state references, of its stack the first `live` slots; returns
/// how many stack slots, frames and open upvalues it walked (all of its stack counts, so
/// the work is the same whatever part is live). S5B-WALK-1 (found again by the cost fuzzer as S5B-COST-1): the work
/// counted was only the objects found, so a stack of a million integers (a vararg frame, a suspended coroutine) was walked twice a cycle
/// for one unit, and with a short pause (a cycle every few kilobytes allocated) a script
/// ran at 100x to 500x plain code per unit of fuel, growing with the stack it kept.
fn collect_thread(t: &ThreadState, live: usize, todo: &mut Vec<Value>, protos: &mut Vec<u32>, ups: &mut Vec<u32>) -> usize {
    let stack = t.stack.get(..live.min(t.stack.len())).unwrap_or_default();
    todo.extend(stack.iter().copied().filter(|v| v.is_collectable()));
    for f in &t.frames {
        todo.push(Value::Func(f.closure));
        protos.push(f.proto);
        if let Some(h) = f.handler {
            todo.push(h);
        }
    }
    ups.extend(t.open_upvals.iter().map(|(_, u)| *u));
    if let Some(r) = t.resumer {
        todo.push(Value::Thread(r));
    }
    if let Some(e) = t.error {
        todo.push(e);
    }
    if let Some((_, _, f)) = t.died_in {
        todo.push(f);
    }
    t.stack.len() + t.frames.len() + t.open_upvals.len()
}

/// Collector work an ephemeron entry costs in the atomic step: its key's list in
/// `eph_waiting` looked up and extended, and the lookup when its key is traversed.
///
/// S5B-COST-3: an entry was one unit of work like a strong table's, but the atomic step
/// hashes its key into `eph_waiting` (SipHash, so a script cannot aim keys at one bucket)
/// once to wait and once more when the key is traversed, each a cache miss in a big map:
/// `collectgarbage('step')` over a weak-keyed table ran at 5x plain code per unit of fuel
/// at 100,000 entries and 13x at 800,000 (a strong table 5x at 800,000). Charged by entry,
/// waiting or not: which entries wait depends on the order the hash parts are walked, so
/// on the seed, and fuel must not.
const EPH_ENTRY_WORK: usize = 12;

/// The most slots of one table a traversal or a weak table's clearing does in one go
/// (S6GC-2e): a table of 800,000 entries took 30 to 50 ms whole.
const TABLE_CHUNK: usize = 4096;
/// The table slots a step of an incremental cycle scans at most (`Heap::step_slots`).
pub(crate) const STEP_SLOTS: usize = 16 * TABLE_CHUNK;
/// Interner slots past which a sweep gives most of them back when an eighth or fewer are
/// in use (`sweep_step`).
const SHRINK_FROM: usize = 1 << 12;

/// Collector work for reaching an object at all, on top of its slots (S6GC-2), by the
/// heap's size when the cycle starts (`obj_work`): a list of small tables linked in an
/// order unrelated to their slots costs a cache and TLB miss per link, and at `1 + slots`
/// work marking 2^19 of them ran at 20x plain code per unit of fuel (30x at 2^21, growing
/// with the heap), while a small heap in the caches costs a fraction of that. Follows the
/// count of object slots only, so fuel stays independent of the hash seed and the layout.
/// (The top bucket was 16, set against plain code that the fast loop has since made faster
/// (about 12 ns a unit here): `sweep6::s6gc_2_marking_scattered_objects_is_paid` then sat
/// at 3 to 4.5x and failed its 5x one run in three. At 20 it is 1.6 to 2.3x.)
fn obj_work(slots: usize) -> usize {
    match slots {
        0..=0x7fff => 2,
        0x8000..=0x1_ffff => 4,
        0x2_0000..=0x3_ffff => 8,
        _ => 20,
    }
}

/// Entries of an ephemeron table the atomic phase traverses in one go (S6GC-2e-R: 4,096
/// of them were 300,000 units, 3 to 5 ms without a look at the deadline).
const EPH_CHUNK: usize = 256;

/// Slots of a table collected into `trav_vals` at a time (S6GC-M1).
const TRAV_CHUNK: usize = 4096;

/// Bytes of capacity a collector scratch buffer keeps past a cycle (S6GC-M1): the gray
/// list and the ephemeron lists grow with the heap and are not charged, so past this they
/// are given back when the cycle ends instead of being kept at their peak.
const SCRATCH_KEEP: usize = 64 * 1024;

/// `v`, emptied, given back when its capacity is past `SCRATCH_KEEP`.
fn trim_vec<T>(v: &mut Vec<T>) {
    v.clear();
    if v.capacity() * std::mem::size_of::<T>() > SCRATCH_KEEP {
        *v = Vec::new();
    }
}

/// Seed number `n` of a state made from `base` (its string, table and random seeds).
pub(crate) fn mix_seed(base: u64, n: u64) -> u64 {
    let mut h = base ^ n.wrapping_mul(0x9E37_79B9_7F4A_7C15);
    h ^= h >> 33;
    h = h.wrapping_mul(0xff51_afd7_ed55_8ccd);
    h ^= h >> 33;
    h = h.wrapping_mul(0xc4ce_b9fe_1a85_ec53);
    h ^ (h >> 33)
}

/// Seed number `n` of a state made from `base`, for a seed a script can read back (the
/// random generator's): `mix_seed` is a bijection a script can run backwards to `base`
/// and so to the hash seeds (S5GC-4). Two independent mixes folded through a 128-bit
/// product have no such inverse.
pub(crate) fn oneway_seed(base: u64, n: u64) -> u64 {
    fold_mul(mix_seed(base, n), mix_seed(base, n ^ 0x5a5a_0000) | 1)
}

/// A process-unique seed for string hashing, so hash-flooding inputs differ per heap.
pub(crate) fn random_seed() -> u64 {
    use std::hash::{BuildHasher, Hasher};
    let mut h = std::collections::hash_map::RandomState::new().build_hasher();
    h.write_u64(0x5eed);
    h.finish()
}

#[cfg(test)]
mod tests {
    use crate::{Libs, Limits, Lua};

    /// SEC-M11: under a seed a script knows (the reference says to keep a fixed seed secret;
    /// S5B-OPEN-2 bounds what aimed keys cost), two long strings of one hash and length that
    /// differ only at their end. Looking the uninterned one up compared it with the interned
    /// one to that end at every lookup (S6T-KEY's settling records a twin only when equal),
    /// free: 4 MiB a lookup for a unit or two. The bytes compared on a miss are charged now.
    #[test]
    fn a_long_key_compared_and_missed_pays_for_the_bytes() {
        // Past `COPY_PIECE` (settled a piece at a time) and under it (the interner's lookup).
        for len in [4 << 20, (1 << 20) - 64, 64 << 10] {
            long_key_miss(len);
        }
    }

    fn long_key_miss(len: usize) {
        let mut fuels = Vec::new();
        for (fast, get) in [(true, "t[b]"), (false, "t[b]"), (true, "rawget(t, b)"), (false, "rawget(t, b)")] {
            fuels.push(long_key_miss_in(len, fast, get));
        }
        assert!(fuels[0] == fuels[1] && fuels[2] == fuels[3], "{len}: fast and full differ: {fuels:?}");
    }

    fn long_key_miss_in(len: usize, fast: bool, get: &str) -> u64 {
        let mut lua = Lua::new(Limits::default().memory(256 << 20).fuel(1 << 40).hash_seed(0x5eed), Libs::ALL);
        lua.set_fast_loop(fast);
        let seed = lua.heap.seed;
        let fill = vec![b'x'; len - 16];
        let hp = super::hash_words(seed, super::hash_start(seed, len), &fill);
        let (k1, k2) = (seed ^ 0xa076_1d64_78bd_642f, seed.rotate_left(32) ^ 0xe703_7ed1_a0b4_28db);
        let make = |i: u64| {
            let w1 = 0x4141_4141_0000_0000 | i;
            let h1 = super::fold_mul(hp ^ w1 ^ k1, k2);
            let w2 = h1 ^ 0x1234_5678_9abc_def0;
            let mut b = fill.clone();
            b.extend_from_slice(&w1.to_le_bytes());
            b.extend_from_slice(&w2.to_le_bytes());
            b
        };
        let (a, b) = (make(1), make(2));
        assert_eq!(super::hash_bytes(seed, &a), super::hash_bytes(seed, &b), "one hash");
        let f = lua.load(format!("local t, a, b = ... t[a] = true local n = 0 for i = 1, 200 do if {get} then n = n + 1 end end return n"), "=t").unwrap();
        let t = lua.create_table().unwrap();
        let sa = lua.create_string(&a).unwrap();
        let sb = lua.create_string(&b).unwrap();
        let before = lua.fuel_left();
        let r = lua.call(f.value(), &[crate::Value::Table(t), sa, sb]).unwrap();
        assert_eq!(r, [crate::Value::Int(0)]);
        let per_lookup = (before - lua.fuel_left()) / 200;
        assert!(per_lookup >= (len / 1024) as u64, "{get}, fast {fast}: {per_lookup} fuel a lookup comparing {len} bytes");
        per_lookup
    }

    /// SEC4-10: a slot cut off by `trim` and made again never gets back a generation a
    /// stale handle holds, even when generations wrap past `u32::MAX` (the old floor
    /// saturated there, so every slot made again had the same generation).
    #[test]
    fn trimmed_slots_never_reuse_a_generation() {
        let mut s: super::Slab<u8> = super::Slab::new(u32::MAX - 1);
        let mut m = super::Mem::new(usize::MAX, 0);
        let n = 4096;
        let mut stale = Vec::new();
        for round in 0..4 {
            let handles: Vec<(u32, u32)> = (0..n).map(|_| s.alloc(round, &mut m, |_| super::WHITE)).collect();
            for &(i, _) in handles.iter().rev() {
                s.free_slot(i as usize, &mut m);
            }
            s.trim(&mut m, round % 2 == 0);
            assert!(s.slots() < n, "trimmed");
            // SEC-M5: nothing of the cut slots stays (their generations neither).
            assert_eq!((s.gens.len(), m.slack, m.used), (s.slots(), 0, 0), "round {round}");
            stale.extend(handles);
            for &(i, g) in &stale {
                assert!(!s.live(i, g), "round {round}: stale handle ({i}, {g}) live after trim");
            }
        }
        // Made again after the trims: no stale handle matches a new object.
        let fresh: Vec<(u32, u32)> = (0..n).map(|_| s.alloc(9, &mut m, |_| super::WHITE)).collect();
        for &(i, g) in &stale {
            assert!(!fresh.contains(&(i, g)), "stale ({i}, {g}) matches a new object");
        }
    }

    /// SEC-M5: past `FLOOR_LIMIT` the cut slots' generations are kept, and counted, and
    /// still no stale handle matches; a slot made again takes its generation out of the
    /// account.
    #[test]
    fn cut_generations_kept_past_the_floor_limit_are_counted() {
        let mut s: super::Slab<u8> = super::Slab::new(7);
        s.floor = s.origin.wrapping_add(super::FLOOR_LIMIT - 3);
        let mut m = super::Mem::new(usize::MAX, 0);
        let n = 3000;
        let mut stale = Vec::new();
        for round in 0..6 {
            let handles: Vec<(u32, u32)> = (0..n).map(|_| s.alloc(1, &mut m, |_| super::WHITE)).collect();
            for &(i, _) in &handles {
                s.free_slot(i as usize, &mut m);
            }
            s.trim(&mut m, true);
            assert_eq!(s.slots(), 0, "round {round}");
            let kept = s.gens.len();
            assert_eq!((m.slack, m.used), (kept * super::GEN_BYTES, kept * super::GEN_BYTES), "round {round}");
            stale.extend(handles);
            for &(i, g) in &stale {
                assert!(!s.live(i, g), "round {round}: stale handle ({i}, {g}) live");
            }
        }
        assert_eq!(s.gens.len(), n, "the cut generations are kept");
        let fresh: Vec<(u32, u32)> = (0..n).map(|_| s.alloc(9, &mut m, |_| super::WHITE)).collect();
        assert_eq!((m.slack, m.used), (0, 0));
        for &(i, g) in &stale {
            assert!(!fresh.contains(&(i, g)), "stale ({i}, {g}) matches a new object");
        }
    }

    /// S6T-SLAB: an `Ahead` holds what a `Vec` given the same pushes, writes, cuts and
    /// shrinks holds, across many growths past `AHEAD_FROM`.
    #[test]
    fn ahead_holds_what_a_vec_holds() {
        let mut a: super::Ahead<u32> = super::Ahead::new();
        let mut v: Vec<u32> = Vec::new();
        let mut x = 0x9e37_79b9_7f4a_7c15u64;
        let mut rnd = || {
            x ^= x << 13;
            x ^= x >> 7;
            x ^= x << 17;
            x
        };
        for round in 0..60 {
            for _ in 0..(rnd() % 40_000) {
                let n = rnd() as u32;
                a.push(n);
                v.push(n);
            }
            for _ in 0..(rnd() % 2000) {
                let i = (rnd() % (v.len() as u64 + 1)) as usize;
                let n = rnd() as u32;
                a.set(i, n);
                if let Some(y) = v.get_mut(i) {
                    *y = n;
                }
            }
            match rnd() % 8 {
                0 => {
                    let n = (rnd() % (v.len() as u64 + 1)) as usize;
                    a.truncate(n);
                    v.truncate(n);
                }
                1 => {
                    let n = v.len() + v.len() / 2;
                    a.shrink_to(n);
                }
                _ => {}
            }
            assert_eq!(&*a, &v[..], "round {round}");
        }
    }

    /// S6T-SLAB (high): a slab's per-slot vectors grew by doubling, so the push that made a
    /// slab of 1M strings one bigger copied their generations and free-list positions at
    /// once (5 ms, past the host's interrupt). Now, whenever a push finds one full, its copy
    /// of twice the room lacks at most a few elements (no push copies more than a stretch),
    /// and through allocations, frees and cuts every live handle stays live, every free
    /// slot's position is right, and the account holds the free slots' bytes.
    #[test]
    fn slab_slots_grow_a_stretch_at_a_time() {
        let mut s: super::Slab<u8> = super::Slab::new(1);
        let mut m = super::Mem::new(usize::MAX, 0);
        let mut x = 0x2545_f491_4f6c_dd1du64;
        let mut rnd = || {
            x ^= x << 13;
            x ^= x >> 7;
            x ^= x << 17;
            x
        };
        let mut live: Vec<(u32, u32)> = Vec::new();
        let mut growths = 0;
        for round in 0..24 {
            for _ in 0..(rnd() % 60_000) {
                for v in [&s.gens, &s.free_at] {
                    if v.len() == v.capacity() && v.capacity() >= super::AHEAD_FROM {
                        assert!(
                            v.next.len() + super::AHEAD_EVERY > v.len(),
                            "round {round}: a full vector's copy lacks {} elements",
                            v.len() - v.next.len()
                        );
                        growths += 1;
                    }
                }
                live.push(s.alloc(0, &mut m, |_| super::WHITE));
            }
            for _ in 0..(rnd() % (live.len() as u64 / 2 + 1)) {
                let k = (rnd() % live.len() as u64) as usize;
                let (i, _) = live.swap_remove(k);
                s.free_slot(i as usize, &mut m);
            }
            if round % 4 == 3 {
                s.trim(&mut m, round % 8 == 7);
            }
            for &(i, g) in &live {
                assert!(s.live(i, g), "round {round}: ({i}, {g}) live");
            }
            for (pos, &i) in s.free.iter().enumerate() {
                assert_eq!(s.free_at.get(i as usize), Some(&(pos as u32)), "round {round}: free_at");
            }
            assert_eq!(
                m.slack,
                s.free.len() * s.slot_bytes + (s.gens.len() - s.slots()) * super::GEN_BYTES,
                "round {round}: slack"
            );
        }
        assert!(growths >= 2, "{growths} growths past AHEAD_FROM");
    }

    /// S5GC-2: `next_used` finds what a slot-by-slot walk finds, through allocations,
    /// frees and trims.
    #[test]
    fn next_used_skips_exactly_the_free_slots() {
        let mut s: super::Slab<u8> = super::Slab::new(1);
        let mut m = super::Mem::new(usize::MAX, 0);
        let mut x = 0x2545_f491_4f6c_dd1du64;
        let mut rnd = || {
            x ^= x << 13;
            x ^= x >> 7;
            x ^= x << 17;
            x
        };
        for round in 0..40 {
            for _ in 0..(rnd() % 9000) {
                s.alloc(0, &mut m, |_| super::WHITE);
            }
            let len = s.slots();
            for _ in 0..(rnd() % (len as u64 + 1)) {
                let i = (rnd() % len as u64) as usize;
                if s.color.get(i) != Some(&super::FREE) {
                    s.free_slot(i, &mut m);
                }
            }
            if round % 3 == 0 {
                s.trim(&mut m, round % 2 == 0);
            }
            let len = s.slots();
            // S5B-WALK-2: the end of the used slots, from the bitmap.
            let end = (0..len).rev().find(|&i| s.color.get(i) != Some(&super::FREE)).map_or(0, |i| i + 1);
            assert_eq!(s.end_of_used(), end, "round {round}");
            // SEC-M5: the account holds exactly the free slots' bytes.
            assert_eq!(
                m.slack,
                s.free.len() * s.slot_bytes + (s.gens.len() - s.slots()) * super::GEN_BYTES,
                "round {round}: slack"
            );
            for (pos, &i) in s.free.iter().enumerate() {
                assert_eq!(s.free_at.get(i as usize), Some(&(pos as u32)), "round {round}: free_at");
            }
            if round % 6 == 0 {
                assert_eq!(s.end_of_used(), len, "round {round}: trim cuts every free slot at the end");
            }
            assert_eq!(s.free.len(), (0..len).filter(|&i| s.color.get(i) == Some(&super::FREE)).count(), "round {round}");
            for from in (0..=len + 70).step_by(1 + (rnd() % 37) as usize) {
                let want = (from..len).find(|&i| s.color.get(i) != Some(&super::FREE)).unwrap_or(len);
                assert_eq!(s.next_used(from), want, "round {round} from {from}");
            }
        }
    }

    /// Dead tables' buffers are reused, and a full collection empties the pool, so the
    /// process's memory returns too; the account returns to its baseline throughout.
    #[test]
    fn table_buffer_pool_fills_and_empties() {
        let mut lua = Lua::new(Limits::default(), Libs::ALL);
        lua.exec("local x = 0 for i = 1, 3 do x = x + i end", "=t").unwrap();
        lua.collect_garbage().unwrap();
        let base = lua.memory_used();
        // Tables that die, then incremental steps until a cycle ends: their buffers are kept.
        lua.exec("keep = {} for i = 1, 2000 do keep[i] = {i, i, {x = i}} end keep = nil", "=t").unwrap();
        lua.exec("for _ = 1, 2 do repeat until collectgarbage('step') end", "=t").unwrap();
        assert!(lua.heap.mem.pool.held() > 0);
        lua.collect_garbage().unwrap();
        assert_eq!(lua.heap.mem.pool.held(), 0);
        assert_eq!(lua.memory_used(), base);
    }

    /// The pool's bytes count as used (not as live, and not in `memory_used`), and a charge
    /// gives them back before it is refused: a charge of exactly the room left without the
    /// pool succeeds, emptying it, and one byte more is refused, as with no pool.
    #[test]
    fn table_buffer_pool_is_charged_and_given_back() {
        let mut lua = Lua::new(Limits::default().memory(4 << 20), Libs::ALL);
        lua.exec("for r = 1, 3 do local t = {} for i = 1, 3000 do t[i] = {i, i} end end", "=t").unwrap();
        lua.exec("for _ = 1, 2 do repeat until collectgarbage('step') end", "=t").unwrap();
        let pooled = lua.heap.mem.pool.bytes;
        assert!(pooled > 0);
        assert_eq!(lua.heap.mem.used, lua.memory_used() + pooled);
        let room = lua.heap.mem.cap + lua.heap.mem.headroom - lua.memory_used();
        assert!(!lua.heap.mem.fits(room + 1));
        assert_eq!(lua.heap.mem.pool.bytes, 0);
        lua.exec("for _ = 1, 2 do repeat until collectgarbage('step') end", "=t").unwrap();
        lua.exec("local t = {} for i = 1, 3000 do t[i] = {i, i} end t = nil for _ = 1, 2 do repeat until collectgarbage('step') end", "=t").unwrap();
        assert!(lua.heap.mem.pool.bytes > 0);
        let room = lua.heap.mem.cap + lua.heap.mem.headroom - lua.memory_used();
        assert!(lua.heap.mem.charge(room).is_ok());
        assert_eq!(lua.heap.mem.pool.bytes, 0);
        assert!(lua.heap.mem.charge(1).is_err());
    }

    /// The pool is bounded: `POOL_BYTES`, or 1/16 of the memory limit when that is less
    /// (SEC3-16), and its bytes count as used.
    #[test]
    fn table_buffer_pool_is_bounded() {
        for (cap, most) in [(512 << 20, super::POOL_BYTES), (256 << 10, (256 << 10) / 16)] {
            let mut lua = Lua::new(Limits::default().memory(cap), Libs::ALL);
            for _ in 0..20 {
                lua.exec("local t = {} for i = 1, 400 do t[i] = {1, 2, 3, x = 1, y = 2, z = 3} end", "=t").unwrap();
                lua.exec("for _ = 1, 2 do repeat until collectgarbage('step') end", "=t").unwrap();
                assert!(lua.heap.mem.pool.bytes <= most, "{} > {most}", lua.heap.mem.pool.bytes);
                assert!(lua.heap.mem.slack >= lua.heap.mem.pool.bytes);
            }
            assert!(lua.heap.mem.pool.held() > 0);
        }
    }
}

/// The hasher of `Heap::eph_waiting` (keys are slab indices): one multiply and a final mix,
/// keyed by the state's seed so a script cannot aim indices at one bucket. SipHash was a
/// fifth of the atomic step's instructions over a big ephemeron table.
#[derive(Clone, Copy)]
pub(crate) struct EphHash(u64);

impl std::hash::BuildHasher for EphHash {
    type Hasher = EphHasher;
    fn build_hasher(&self) -> EphHasher {
        EphHasher(self.0)
    }
}

pub(crate) struct EphHasher(u64);

impl std::hash::Hasher for EphHasher {
    fn write(&mut self, bytes: &[u8]) {
        for &b in bytes {
            self.write_u8(b);
        }
    }
    fn write_u8(&mut self, n: u8) {
        self.0 = (self.0 ^ u64::from(n)).wrapping_mul(0x9e37_79b9_7f4a_7c15);
    }
    fn write_u32(&mut self, n: u32) {
        self.0 = (self.0 ^ u64::from(n)).wrapping_mul(0x9e37_79b9_7f4a_7c15);
    }
    fn finish(&self) -> u64 {
        mix_seed(self.0, 1)
    }
}

/// Where a suspended atomic phase stands (`Heap::atomic`): still marking (ephemerons
/// waiting for their keys), or clearing weak tables from the one at an index, from a slot.
pub(crate) enum AtomicStage {
    Mark,
    Clear(Vec<(u32, bool, bool)>, usize, usize),
}

/// What stops the collector's work part-way, looked at every `ATOMIC_CHECK` units of it:
/// the host's interrupt, and the deadline of its time limit (S6GC-2e-R: the atomic phase
/// and a long marking step read neither clock nor deadline, so a call ran 3 to 7 ms past
/// its time limit between two reads of it).
pub(crate) struct Stop<'a> {
    pub interrupt: &'a std::sync::atomic::AtomicBool,
    pub deadline: Option<(std::time::Duration, &'a crate::Clock)>,
}

impl Stop<'_> {
    fn interrupted(&self) -> bool {
        self.interrupt.load(std::sync::atomic::Ordering::Relaxed)
    }

    fn past_deadline(&self) -> bool {
        self.deadline.is_some_and(|(at, clock)| clock() >= at)
    }
}

/// How an atomic phase step ended, with the work it did.
pub(crate) enum Atomic {
    /// Marking is over: the sweep starts.
    Done(usize),
    /// Stopped at its allowance; resumed by the next call (`Heap::atomic`).
    Suspended(usize),
    /// Stopped at the time limit's deadline: suspended as at the allowance, the call stops
    /// on its time limit.
    TimedOut(usize),
    /// The host's interrupt: the cycle was abandoned.
    Interrupted(usize),
    /// Paused at the end of the host's poll (`Heap::poll_over`), its counters kept
    /// (`atomic_saved`): nothing charged; it goes on as it stood (SEC7-G1).
    Paused,
}